PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000243. Giữ nguyên ước chung lớn nhất (Same GCDs)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Giữ nguyên ước chung lớn nhất (Same GCDs)

Same GCDs

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given positive integers a,ma,m, count integers xx such that 0≤x<m0\le x<m and gcd⁡(a+x,m)=gcd⁡(a,m)\gcd(a+x,m)=\gcd(a,m).

Input

One line contains positive integers a,ma,m in that order.

Output

Print the number of valid integers xx.

Subtasks

  • Subtask 1 (20 points): 1≤a<m≤1001\le a<m\le 100.
  • Subtask 2 (30 points): 1≤a<m≤1000001\le a<m\le 100000.
  • Subtask 3 (50 points): 1≤a<m≤100000000001\le a<m\le 10000000000.

Examples

Example 1

Input

4 9

Output

6

Explanation

For a=4a=4 and m=9m=9, g=gcd⁡(4,9)g=\gcd(4,9) and k=9/gk=9/g. There are φ(k)=6\varphi(k)=6 coprime residues modulo kk.

Example 2

Input

5 10

Output

1

Explanation

For a=5a=5 and m=10m=10, g=gcd⁡(5,10)g=\gcd(5,10) and k=10/gk=10/g. There are φ(k)=1\varphi(k)=1 coprime residues modulo kk.

Example 3

Input

42 9999999967

Output

9999999966

Explanation

For a=42a=42 and m=9999999967m=9999999967, g=gcd⁡(42,9999999967)g=\gcd(42,9999999967) and k=9999999967/gk=9999999967/g. There are φ(k)=9999999966\varphi(k)=9999999966 coprime residues modulo kk.