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;
const int maxn = 200009;
const int MOD = 1e9 + 7;
long long m, x;
void gao(long long M, vector<pair<long long, long long>> &res) {
  for (long long i = 2; i * i <= M; ++i) {
    if (M % i == 0) {
      res.emplace_back(i, 0);
      while (M % i == 0) {
        M /= i;
        res.back().second++;
      }
    }
  }
  if (M != 1) res.emplace_back(M, 1);
}
vector<pair<long long, long long>> Prm;
vector<vector<pair<long long, long long>>> V;
const long long mask = (1ll << 30) - 1;
long long mul(long long a, long long b, long long mod) {
  a %= mod;
  b %= mod;
  long long res = (long double)a * b / mod + 0.5;
  res = a * b - mod * res;
  return (res % mod + mod) % mod;
}
long long pw(long long a, long long b, long long mod) {
  long long res = 1;
  while (b) {
    if (b & 1ll) res = mul(res, a, mod);
    a = mul(a, a, mod);
    b >>= 1;
  }
  return res;
}
long long cal(long long M) {
  long long P = M, N = M;
  vector<pair<long long, long long>> Q;
  for (int i = 0; i < ((int)(Prm).size()); ++i) {
    if (N % Prm[i].first == 0) {
      long long cnt = 0;
      while (N % Prm[i].first == 0) {
        N /= Prm[i].first;
        cnt++;
      }
      P = P / Prm[i].first * (Prm[i].first - 1);
      if (cnt - 1) Q.emplace_back(Prm[i].first, cnt - 1);
      for (int j = 0; j < ((int)(V[i]).size()); ++j) Q.push_back(V[i][j]);
    }
  }
  sort(Q.begin(), Q.end());
  vector<pair<long long, long long>> QQ;
  for (int i = 0; i < ((int)(Q).size());) {
    int j = i, cnt = 0;
    while (i < ((int)(Q).size()) && Q[i].first == Q[j].first)
      cnt += Q[i].second, ++i;
    QQ.emplace_back(Q[j].first, cnt);
  }
  long long QQQ = P;
  for (int i = 0; i < ((int)(QQ).size()); ++i) {
    for (int j = 1; j <= QQ[i].second; ++j) {
      if (pw(x, QQQ / QQ[i].first, M) == 1) QQQ /= QQ[i].first;
    }
  }
  return P / QQQ;
}
int main() {
  ;
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  cin >> m >> x;
  gao(m, Prm);
  V.assign(((int)(Prm).size()), {});
  for (int i = 0; i < ((int)(Prm).size()); ++i) gao(Prm[i].first - 1, V[i]);
  long long res = 0;
  for (long long i = 1; i * i <= m; ++i) {
    if (m % i == 0) {
      long long j = m / i;
      res += cal(i);
      if (i != j) res += cal(j);
    }
  }
  cout << res << endl;
  return 0;
}