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 long long INF64 = 1LL << 60;
const int INF32 = 0x3f3f3f3f;
map<long long, vector<pair<long long, long long>>> dfact;
long long quick_mul(long long a, long long b, long long mod) {
  long long ret = 0;
  while (b) {
    if (b & 1) {
      ret += a;
      if (ret >= mod) ret -= mod;
    }
    a = a + a;
    if (a >= mod) a %= mod;
    b >>= 1;
  }
  return ret;
}
long long quick_pow(long long a, long long b, long long mod) {
  long long ret = 1;
  while (b) {
    if (b & 1) ret = quick_mul(a, ret, mod);
    a = quick_mul(a, a, mod);
    b >>= 1;
  }
  return ret;
}
long long getPw(long long a, long long b) {
  long long ret = 1;
  for (int i = 0; i < b; ++i) ret *= a;
  return ret;
}
long long m, x;
vector<pair<long long, long long>> fact(long long x) {
  if (dfact.count(x)) return dfact[x];
  long long oldx = x;
  vector<pair<long long, long long>> fac;
  for (long long i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      int cnt = 0;
      while (x % i == 0) ++cnt, x /= i;
      fac.emplace_back(i, cnt);
      if (x == 1) break;
    }
  }
  if (x != 1) fac.emplace_back(x, 1LL);
  return dfact[oldx] = fac;
}
long long findOrder(long long x, long long mod,
                    const vector<pair<long long, long long>>& f) {
  long long phi = 1;
  for (int i = 0; i < f.size(); ++i)
    for (int j = 0; j < f[i].second; ++j) phi *= f[i].first;
  if (phi == 1 || x == 0) return 1;
  long long ord = 1;
  for (int i = 0; i < f.size(); ++i) {
    long long basePw = phi;
    for (int k = 0; k < f[i].second; ++k) basePw /= f[i].first;
    long long curV = quick_pow(x, basePw, mod);
    int curPw = -1;
    for (int k = 0; k < f[i].second + 1; ++k)
      if (curV != 1) {
        curPw = k;
        curV = quick_pow(curV, f[i].first, mod);
      }
    ord *= getPw(f[i].first, curPw + 1);
  }
  return ord;
}
vector<pair<long long, long long>> fm;
vector<int> pw;
vector<pair<long long, long long>> merge(
    const vector<pair<long long, long long>>& a,
    const vector<pair<long long, long long>>& b) {
  vector<pair<long long, long long>> ans;
  int u = 0, v = 0;
  while (u < a.size() && v < b.size()) {
    if (a[u].first < b[v].first)
      ans.push_back(a[u++]);
    else if (a[u].first > b[v].first)
      ans.push_back(b[v++]);
    else {
      ans.emplace_back(a[u].first, a[u].second + b[v].second);
      ++u, ++v;
    }
  }
  while (u < a.size()) ans.push_back(a[u++]);
  while (v < b.size()) ans.push_back(b[v++]);
  return ans;
}
long long calc(int pos, long long dv) {
  if (pos >= fm.size()) {
    vector<pair<long long, long long>> cf = fm;
    for (int i = 0; i < fm.size(); ++i) cf[i].second = max(0, pw[i] - 1);
    long long phi = dv;
    for (int i = 0; i < fm.size(); ++i)
      if (pw[i] > 0) {
        cf = merge(cf, fact(fm[i].first - 1));
        phi -= phi / fm[i].first;
      }
    long long k = findOrder(x % dv, dv, cf);
    return phi / k;
  }
  long long ans = 0;
  for (int i = 0; i <= fm[pos].second; ++i) {
    pw[pos] = i;
    ans += calc(pos + 1, dv);
    dv *= fm[pos].first;
  }
  return ans;
}
void solve() {
  fm = fact(m);
  pw.assign(fm.size(), 0);
  long long ans = calc(0, 1);
  cout << ans << endl;
}
int main() {
  cin >> m >> x;
  solve();
}
