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.
#include <bits/stdc++.h>
using namespace std;
inline long long mul(long long a, long long b, long long c) {
  long long res = (a * b - (long long)((double)a * b / c + 0.1) * c) % c;
  return res < 0 ? res + c : res;
}
inline long long qp(long long a, long long b, long long mod) {
  a %= mod;
  long long tmp = a, res = 1;
  while (b) {
    if (b & 1) res = mul(res, tmp, mod);
    tmp = mul(tmp, tmp, mod);
    b >>= 1;
  }
  return res;
}
long long m, sqrtm, x, factor[200], fact[100000], f[100000], prim[100000];
int n = 0, ptop = 0, tol = 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, long long *prim, int &ptop) {
  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;
}
inline 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)
    calprim(Eulerm, factor, tol);
  else
    factor[++tol] = 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 &&
               qp(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, prim, ptop);
  printf("%I64d\n", cal(m));
}
