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 maxn = 55;
long long m, x, ans;
vector<pair<long long, int>> fac;
vector<long long> G[maxn];
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long add(long long a, long long b, long long p) {
  return (a += b) < p ? a : a - p;
}
long long mul(long long a, long long b, long long p) {
  long long ret = 0, now = a;
  while (b) {
    if (b & 1) ret = add(ret, now, p);
    now = add(now, now, p);
    b >>= 1;
  }
  return ret;
}
bool fast_pow(long long x, long long b, long long p) {
  long long ret = 1, now = x;
  while (b) {
    if (b & 1) ret = mul(ret, now, p);
    now = mul(now, now, p);
    b >>= 1;
  }
  return ret == 1;
}
void dfs(int u, long long now, long long g) {
  if (u == fac.size()) {
    ans += now / g;
    return;
  }
  dfs(u + 1, now, g);
  long long cur = fac[u].first;
  now *= (cur - 1);
  dfs(u + 1, now, lcm(G[u][1], g));
  for (int(i) = (2); (i) <= (fac[u].second); (i)++) {
    now *= cur;
    dfs(u + 1, now, lcm(G[u][i], g));
  }
}
int main() {
  scanf("%lld%lld", &m, &x);
  for (long long i = 2; i * i <= m; i++)
    if (m % i == 0) {
      int cnt = 0;
      while (m % i == 0) {
        m /= i;
        cnt++;
      }
      fac.push_back(make_pair(i, cnt));
    }
  if (m > 1) fac.push_back(make_pair(m, 1));
  for (int(i) = (0); (i) <= ((fac.size()) - 1); (i)++) {
    long long num = fac[i].first, cnt = fac[i].second;
    G[i].resize(cnt + 1);
    long long xnow = x % num, cur = 1;
    bool flag = false;
    for (; cur * cur <= num - 1; cur++)
      if ((num - 1) % cur == 0) {
        if (fast_pow(xnow, cur, num)) {
          G[i][1] = cur;
          flag = true;
          break;
        }
      }
    if (!flag) {
      for (; cur >= 1; cur--)
        if ((num - 1) % cur == 0) {
          if (fast_pow(xnow, (num - 1) / cur, num)) {
            G[i][1] = (num - 1) / cur;
            break;
          }
        }
    }
    long long mod = num;
    for (int(j) = (2); (j) <= (cnt); (j)++) {
      mod *= num;
      if (fast_pow(x % mod, G[i][j - 1], mod))
        G[i][j] = G[i][j - 1];
      else
        G[i][j] = G[i][j - 1] * num;
    }
  }
  dfs(0, 1, 1);
  printf("%lld\n", ans);
  return 0;
}
