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>
const int maxn = 107;
inline long long gcd(long long x, long long y) {
  return !y ? x : gcd(y, x % y);
}
inline long long times(long long a, long long b, long long P) {
  a %= P;
  b %= P;
  long long ret = 0;
  while (b) {
    if (b & 1) {
      ret += a;
      if (ret >= P) ret -= P;
    }
    a += a;
    if (a >= P) a -= P;
    b >>= 1;
  }
  return ret;
}
inline long long power(long long a, long long b, long long P) {
  a %= P;
  long long c = 1;
  for (; b; b >>= 1, a = times(a, a, P))
    if (b & 1) c = times(c, a, P);
  return c;
}
void findfac(long long v, long long res[], int &cnt) {
  for (long long i = 2; i * i <= v; ++i) {
    while ((v % i) == 0) {
      res[cnt++] = i;
      v /= i;
    }
  }
  if (v != 1) res[cnt++] = v;
}
long long M, X;
long long facm[maxn], facr[maxn][maxn];
int pm[maxn], pr[maxn], cntm = 0, cntr[maxn];
long long ans = 0;
void dfs(int p, long long u, long long phi, long long tp) {
  if (p == cntm) {
    if (u != 1) ans += phi / tp;
    return;
  }
  dfs(p + 1, u, phi, tp);
  if (u != 1) assert(power(X, tp, u) == 1);
  u *= facm[p];
  phi *= facm[p] - 1;
  long long tt = tp * (facm[p] - 1);
  for (int i = 0; i < cntr[p]; ++i) {
    if (power(X, tt / facr[p][i], u) == 1) tt /= facr[p][i];
  }
  dfs(p + 1, u, phi, tt);
  for (int i = 2; i <= pm[p]; ++i) {
    u *= facm[p];
    phi *= facm[p];
    if (power(X, tt, u) != 1) tt *= facm[p];
    dfs(p + 1, u, phi, tt);
  }
}
int main() {
  std::cin >> M >> X;
  assert(gcd(M, X) == 1);
  findfac(M, facm, cntm);
  std::sort(facm, facm + cntm);
  int t = 0;
  for (int i = 0; i < cntm; ++i) {
    if (i == 0 || facm[i] != facm[i - 1]) {
      facm[t++] = facm[i];
      pm[t - 1] = 1;
    } else {
      pm[t - 1]++;
    }
  }
  cntm = t;
  for (int i = 0; i < cntm; ++i) {
    findfac(facm[i] - 1, facr[i], cntr[i]);
    std::sort(facr[i], facr[i] + cntr[i]);
  }
  dfs(0, 1, 1, 1);
  ++ans;
  std::cout << ans << std::endl;
}