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;
const int N = 1e6;
template <typename T>
void read(T &x) {
  bool neg = false;
  unsigned char c = getchar();
  for (; (c ^ 48) > 9; c = getchar())
    if (c == '-') neg = true;
  for (x = 0; (c ^ 48) < 10; c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48);
  if (neg) x = -x;
}
int n, n1, n2;
long long m, x, phim, pdm[N], dm[N], dphim[N], tmp[N], ans, powx[N];
long long phi(long long x) {
  long long ans = x;
  for (int i = 0, i_end = n; i < i_end; ++i)
    if (x % pdm[i] == 0) {
      while (x % pdm[i] == 0) x /= pdm[i];
      ans -= ans / pdm[i];
    }
  return ans;
}
long long mul(long long a, long long b) {
  long long c = 0;
  while (b) {
    (c += (b & 0xffff) * a) %= m;
    b >>= 16;
    (a <<= 16) %= m;
  }
  return c;
}
long long pow(long long a, long long b) {
  long long c = 1;
  while (b) {
    if (b & 1) c = mul(c, a);
    a = mul(a, a);
    b >>= 1;
  }
  return c;
}
void getprimedivisor(long long m) {
  for (int i = 2; (long long)i * i <= m; ++i)
    if (m % i == 0) {
      while (m % i == 0) m /= i;
      pdm[n++] = i;
    }
  if (m != 1) pdm[n++] = m;
}
int getdivisor(long long x, long long *d, long long *dd) {
  int n = 0;
  for (int i = 1; (long long)i * i <= x; ++i)
    if (x % i == 0) d[n] = i, dd[n++] = x / i;
  for (int i = n - 1; ~i; --i)
    if (d[n - 1] != dd[i]) d[n++] = dd[i];
  return n;
}
int main() {
  read(m);
  read(x);
  getprimedivisor(m);
  n1 = getdivisor(m, dm, tmp);
  phim = phi(m);
  n2 = getdivisor(phim, dphim, tmp);
  for (int j = 0, j_end = n2; j < j_end; ++j) powx[j] = pow(x, dphim[j]);
  ++ans;
  for (int i = 0, i_end = n1 - 1; i < i_end; ++i) {
    long long x = dm[i], f = phi(m / x), p = 0;
    for (int j = 0, j_end = n2; j < j_end; ++j)
      if (mul(powx[j], x) == x) {
        p = dphim[j];
        break;
      }
    ans += f / p;
  }
  printf("%lld\n", ans);
  return 0;
}
