Problem: The campus has m rooms numbered from 0 to m - 1. Also the x-mouse lives in the campus. The x-mouse is not just a mouse: each second x-mouse moves from room i to the room i ⋅ x mod{m} (in fact, it teleports from one room to another since it doesn't visit any intermediate room). Starting position of the x-mouse is unknown.

You are responsible to catch the x-mouse in the campus, so you are guessing about minimum possible number of traps (one trap in one room) you need to place. You are sure that if the x-mouse enters a trapped room, it immediately gets caught.

And the only observation you made is GCD (x, m) = 1.

Input

The only line contains two integers m and x (2 ≤ m ≤ 10^{14}, 1 ≤ x < m, GCD (x, m) = 1) — the number of rooms and the parameter of x-mouse. 

Output

Print the only integer — minimum number of traps you need to install to catch the x-mouse.

Examples

Input

4 3


Output

3


Input

5 2


Output

2

Note

In the first example you can, for example, put traps in rooms 0, 2, 3. If the x-mouse starts in one of this rooms it will be caught immediately. If x-mouse starts in the 1-st rooms then it will move to the room 3, where it will be caught.

In the second example you can put one trap in room 0 and one trap in any other room since x-mouse will visit all rooms 1..m-1 if it will start in any of these rooms.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int S = 8;
inline long long mult_mod(long long a, long long b, long long c) {
  a %= c;
  b %= c;
  long long res = (a * b - (long long)((double)a * b / c + 0.1) * c) % c;
  if (res < 0) res += c;
  return res;
}
long long pow_mod(long long x, long long n, long long mod) {
  if (n == 1) return x % mod;
  x %= mod;
  long long tmp = x;
  long long ret = 1;
  while (n) {
    if (n & 1) ret = mult_mod(ret, tmp, mod);
    tmp = mult_mod(tmp, tmp, mod);
    n >>= 1;
  }
  return ret;
}
bool check(long long a, long long n, long long x, long long t) {
  long long ret = pow_mod(a, x, n);
  long long last = ret;
  for (int i = 1; i <= t; i++) {
    ret = mult_mod(ret, ret, n);
    if (ret == 1 && last != 1 && last != n - 1) return true;
    last = ret;
  }
  if (ret != 1) return true;
  return false;
}
bool Miller_Rabin(long long n) {
  if (n < 2) return false;
  if (n == 2) return true;
  if ((n & 1) == 0) return false;
  long long x = n - 1;
  long long t = 0;
  while ((x & 1) == 0) {
    x >>= 1;
    t++;
  }
  for (int i = 0; i < S; i++) {
    long long a = rand() % (n - 1) + 1;
    if (check(a, n, x, t)) return false;
  }
  return true;
}
long long factor[100];
int tol;
long long gcd(long long a, long long b) {
  if (a == 0) return 1;
  if (a < 0) return gcd(-a, b);
  while (b) {
    long long t = a % b;
    a = b;
    b = t;
  }
  return a;
}
long long Pollard_rho(long long x, long long c) {
  long long i = 1, k = 2;
  long long x0 = rand() % x;
  long long y = x0;
  while (1) {
    i++;
    x0 = (mult_mod(x0, x0, x) + c) % x;
    long long d = gcd(y - x0, x);
    if (d != 1 && d != x) return d;
    if (y == x0) return x;
    if (i == k) {
      y = x0;
      k += k;
    }
  }
}
void findfac(long long n) {
  if (Miller_Rabin(n)) {
    factor[++tol] = n;
    return;
  }
  long long p = n;
  while (p >= n) p = Pollard_rho(p, rand() % (n - 1) + 1);
  findfac(p);
  findfac(n / p);
}
long long m, sqrtm, x, fact[100000], f[100000], prim[100000];
int n = 0, ptop = 0;
inline void calfact(long long m) {
  for (long long i = 1, mi; i <= sqrtm; i++) {
    mi = m / i;
    if (i * mi == m) {
      if (i != mi) fact[++n] = mi;
      fact[++n] = i;
    }
  }
  sort(fact + 1, fact + 1 + n);
}
inline void calprim(long long m) {
  for (long long i = 2; i <= sqrtm; i++) {
    if (m % i == 0) {
      prim[++ptop] = i;
      while (m % i == 0) m /= i;
    }
  }
  if (m > 1) prim[++ptop] = m;
  return;
}
long long Euler(long long n) {
  long long phi = n;
  for (int i = 1; i <= ptop; i++) {
    if (phi % prim[i] == 0) phi -= phi / prim[i];
  }
  return phi;
}
inline long long cal(long long m) {
  long long res = 0, tmpres, trueres, Eulerm = Euler(m);
  if (Eulerm > 1)
    findfac(Euler(m));
  else
    factor[++tol] = 1;
  sort(factor + 1, factor + 1 + tol);
  tol = unique(factor + 1, factor + 1 + tol) - factor - 1;
  for (int i = 1; i <= n; i++) {
    tmpres = trueres = Euler(fact[i]);
    if (tmpres > 1) {
      for (int j = 1; j <= tol; j++) {
        while (tmpres % factor[j] == 0 &&
               pow_mod(x, tmpres / factor[j], fact[i]) == 1)
          tmpres /= factor[j];
      }
    }
    res += trueres / tmpres;
  }
  return res;
}
int main() {
  scanf("%I64d%I64d", &m, &x);
  sqrtm = sqrt(m + 1);
  calfact(m);
  calprim(m);
  printf("%I64d\n", cal(m));
}