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;
long long qmul(long long n, long long k, long long mod) {
  long long res = 0;
  while (k) {
    if (k & 1) res = (res + n) % mod;
    n = (n + n) % mod;
    k >>= 1;
  }
  return res;
}
inline long long mul(long long a, long long b, long long c) {
  long long t = (long double)(a)*b / c, s = a * b - c * t;
  s %= c;
  if (s < 0) s += c;
  return s;
}
long long qpow(long long n, long long k, long long mod) {
  long long res = 1;
  while (k) {
    if (k & 1) res = mul(res, n, mod);
    n = mul(n, n, mod);
    k >>= 1;
  }
  return res;
}
map<long long, vector<long long> > mp;
vector<long long> prime_factorize(long long m) {
  if (mp.count(m)) return mp[m];
  vector<long long> pri;
  for (long long i = 2; i * i <= m; i++) {
    if (m % i == 0) {
      pri.push_back(i);
      while (m % i == 0) m /= i;
    }
  }
  if (m != 1) pri.push_back(m);
  sort(pri.begin(), pri.end());
  return mp[m] = pri;
}
vector<long long> merge(vector<long long> a, vector<long long> b) {
  if (a.size() < b.size()) {
    for (long long i : a) b.push_back(i);
    return b;
  }
  for (long long i : b) a.push_back(i);
  return a;
}
vector<long long> pri, fac, phi;
vector<vector<long long> > pri_fac_phi;
void dfs(long long f, long long p, vector<long long> v, int pos, long long m) {
  if (pos >= pri.size()) {
    fac.push_back(f), phi.push_back(p), pri_fac_phi.push_back(v);
    return;
  }
  dfs(f, p, v, pos + 1, m);
  for (f *= pri[pos], p *= pri[pos] - 1,
       v = merge(v, prime_factorize(pri[pos] - 1));
       f <= m && m % f == 0;
       f *= pri[pos], p *= pri[pos], v = merge(v, prime_factorize(pri[pos])))
    dfs(f, p, v, pos + 1, m);
}
int main() {
  long long m, x;
  while (cin >> m >> x) {
    pri = prime_factorize(m);
    fac.clear(), phi.clear();
    dfs(1, 1, vector<long long>(), 0, m);
    long long ans = 0;
    for (int i = 1; i < fac.size(); i++) {
      long long d = phi[i];
      for (long long p : pri_fac_phi[i]) {
        while (d % p == 0 && qpow(x % fac[i], d / p, fac[i]) == 1) d /= p;
      }
      ans += phi[i] / d;
    }
    cout << ans + 1 << endl;
  }
}
