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>
using namespace std;
int runs, p[10101010], phi[10101010], mu[10101010];
void ppm() {
  phi[1] = mu[1] = 1;
  for (int i = 2; i < 10101010; i++) {
    if (!phi[i]) p[++runs] = i, phi[i] = i - 1, mu[i] = -1;
    for (int j = 1, k; j <= runs && p[j] * i < 10101010; j++) {
      k = p[j] * i;
      if (i % p[j] == 0) {
        phi[k] = phi[i] * p[j];
        break;
      }
      phi[k] = phi[i] * (p[j] - 1);
      mu[k] = -mu[i];
    }
  }
}
void calc(long long m, vector<long long> &v, vector<int> &vv) {
  long long tmp = m;
  for (int i = 1; i <= runs; i++)
    if (tmp % p[i] == 0) {
      v.push_back(p[i]);
      int t = 0;
      while (tmp % p[i] == 0) {
        tmp /= p[i];
        t++;
      }
      vv.push_back(t);
    }
  if (tmp > 1) {
    v.push_back(tmp);
    vv.push_back(1);
  }
}
long long calc_phi(vector<long long> &v, vector<int> &vv, long long &now) {
  long long rlt = 1;
  now = 1;
  for (int i = 0; i < v.size(); i++)
    if (vv[i]) {
      long long p = v[i];
      rlt *= (p - 1);
      now *= p;
      for (int j = 1; j < vv[i]; j++) rlt *= p, now *= p;
    }
  return rlt;
}
vector<long long> v;
vector<int> vv;
int e;
long long h[101010], g[101010];
long long m, x, n;
int sz;
long long ans = 0;
vector<int> w;
long long mod_mul(long long x, long long y, long long mod) {
  if (x == 0 || y == 0) return 0;
  long long ret = 0;
  while (x) {
    ret = (ret + (x & 1) * y) % mod;
    y = (y << 1) % mod;
    x >>= 1;
  }
  return ret;
}
long long expmod(long long a, long long b, long long mod) {
  a %= mod;
  if (mod <= 3e9) {
    long long rlt = 1;
    while (b) {
      if (b & 1) rlt = a * rlt % mod;
      a = a * a % mod;
      b >>= 1;
    }
    return rlt;
  }
  long long rlt = 1;
  while (b) {
    if (b & 1) rlt = mod_mul(a, rlt, mod);
    a = mod_mul(a, a, mod);
    b >>= 1;
  }
  return rlt;
}
void dfs(int i, long long ph, long long now) {
  if (i == sz) {
    long long rlt = ph;
    for (int i = 1; i <= e; i++) {
      if (now == 1 || (rlt % h[i] == 0 && g[i] % now == 1)) {
        ans += rlt / h[i];
        return;
      }
    }
    return;
  }
  w[i] = 0, dfs(i + 1, ph, now);
  ph *= v[i] - 1, now *= v[i];
  for (w[i] = 1; w[i] <= vv[i]; w[i]++) {
    dfs(i + 1, ph, now);
    ph *= v[i];
    now *= v[i];
  }
}
int main() {
  ppm();
  scanf("%I64d %I64d", &m, &x);
  calc(m, v, vv);
  long long tmp;
  n = calc_phi(v, vv, tmp);
  assert(tmp == m);
  for (int i = 1; 1ll * i * i <= n; i++)
    if (n % i == 0) {
      h[++e] = i;
      h[++e] = n / i;
    }
  sort(h + 1, h + e + 1);
  for (int i = 1; i <= e; i++) g[i] = expmod(x, h[i], m);
  sz = v.size();
  w.resize(sz, 0);
  dfs(0, 1, 1);
  printf("%I64d\n", ans);
  return 0;
}