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>
long long qmul(long long x, long long y, long long m) {
  return (x * y - (long long)((long double)x / m * y) * m + m) % m;
}
long long mpow(long long b, long long p, long long m) {
  long long r;
  for (r = 1; p; p >>= 1, b = qmul(b, b, m))
    if (p & 1) r = qmul(r, b, m);
  return r;
}
std::map<long long, long long> phi, pw;
long long p[3700];
int a[3700], c;
void divide(long long m) {
  for (int i = 1; i <= c; i++) {
    while (m % p[i] == 0) m /= p[i];
  }
  long long sq = (long long)sqrt(m);
  for (int i = 2; i <= sq; i++)
    if (m % i == 0) {
      p[++c] = i;
      while (m % i == 0) m /= i, a[c]++;
    }
  if (m != 1) p[++c] = m, a[c] = 1;
}
void dfs(int d, long long n, long long k) {
  if (d > c) {
    phi[n] = k;
    return;
  }
  dfs(d + 1, n, k);
  k *= (p[d] - 1), n *= p[d];
  for (int i = 1; i <= a[d]; i++, n *= p[d], k *= p[d]) dfs(d + 1, n, k);
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  long long m, x;
  scanf("%lld%lld", &m, &x);
  divide(m), dfs(1, 1, 1);
  int c1 = c;
  for (int i = 1; i <= c1; i++) divide(p[i] - 1);
  long long ans = 0;
  for (auto it = phi.rbegin(); it != phi.rend(); it++) {
    long long s = it->first, t = it->second, k = t;
    for (int i = 1; i <= c1; i++)
      if (s <= m / p[i] && phi.count(s * p[i])) k = gcd(k, phi[s * p[i]]);
    for (int i = 1; i <= c; i++) {
      while (k % p[i] == 0) {
        if (!pw.count(k / p[i])) pw[k / p[i]] = mpow(x, k / p[i], m);
        if (pw[k / p[i]] % s == 1)
          k /= p[i];
        else
          break;
      }
    }
    ans += t / k, phi[it->first] = k;
  }
  printf("%lld\n", ans);
}
