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>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
using namespace std;
const double eps = 1e-10, pi = 3.1415926535898;
const int mod = 1e9 + 7, maxn = 2e4 + 10;
int ind, inp;
long long n, m, k, x, y, ans;
vector<long long> v;
stack<long long> s;
struct P {
  long long x, y;
} p[maxn], pp[maxn];
vector<P> vp;
long long get_euler(long long n) {
  long long cnt = 1;
  for (long long i = 2; i * i <= n; ++i) {
    if (n % i) continue;
    n /= i;
    long long x = i - 1;
    y = 1;
    while (n % i == 0) {
      n /= i;
      x *= i;
      ++y;
    }
    cnt *= x;
    vp.push_back({i, y});
  }
  if (n > 1) {
    cnt *= n - 1;
    vp.push_back({n, 1});
  }
  return cnt;
}
long long mul(long long x, long long k, long long mod) {
  long long cnt = 0;
  x %= mod;
  while (k) {
    if (k & 1) cnt += x;
    if (cnt >= mod) cnt -= mod;
    x *= 2;
    if (x >= mod) x -= mod;
    k >>= 1;
  }
  return cnt;
}
long long qpow(long long x, long long k, long long mod) {
  long long cnt = 1;
  while (k) {
    if (k & 1) cnt = mul(cnt, x, mod);
    x = mul(x, x, mod);
    k >>= 1;
  }
  return cnt;
}
void dfs(int q) {
  if (q == ind) {
    long long cnt = m, z = 1;
    for (int i = 0; i < ind; ++i) {
      if (p[i].y == 0) {
        cnt /= p[i].x - 1;
        for (int j = 2; j <= vp[i].y; ++j) {
          cnt /= p[i].x;
        }
      } else {
        for (int j = vp[i].y; j > p[i].y; --j) {
          cnt /= p[i].x;
        }
      }
      for (int j = 1; j <= p[i].y; ++j) {
        z *= p[i].x;
      }
    }
    z = n / z;
    if (z == n) {
      ++ans;
      return;
    }
    y = 0;
    for (int j = 1; j <= inp; ++j) {
      y = pp[j].x;
      if (pp[j].y % (n / z) == 1) {
        break;
      }
    }
    ans += cnt / y;
    return;
  }
  for (int i = 0; i <= vp[q].y; ++i) {
    p[q] = {vp[q].x, i};
    dfs(q + 1);
  }
  return;
}
int main(int argc, char const *argv[]) {
  scanf("%lld %lld", &n, &x);
  m = get_euler(n);
  ind = vp.size();
  for (long long i = 1; i * i <= m; ++i) {
    if (m % i == 0) {
      v.push_back(i);
      if (i * i < m) {
        s.push(m / i);
      }
    }
  }
  while (!s.empty()) {
    v.push_back(s.top());
    s.pop();
  }
  for (long long i : v) {
    pp[++inp] = {i, qpow(x, i, n)};
  }
  dfs(0);
  printf("%lld\n", ans);
  return 0;
}