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;
long long m, x;
vector<pair<long long, int>> fact;
vector<pair<long long, long long>> d;
vector<pair<long long, int>> g;
vector<pair<long long, int>> r;
long long mulmod(long long a, long long b, long long m) {
  long double x;
  long long c;
  long long r;
  if (a >= m) a %= m;
  if (b >= m) b %= m;
  x = a;
  c = x * b / m;
  r = (a * b - c * m) % m;
  return r < 0 ? r + m : r;
}
long long powerx(long long a, long long mod) {
  if (a == 0) return 1 % mod;
  long long t = powerx(a / 2, mod);
  t = mulmod(t, t, mod);
  if (a % 2) t = mulmod(t, x, mod);
  return t;
}
void backtrack(int u, int v, long long s, int mu = 1) {
  if (u == fact.size()) {
    if (mu) r.push_back({s, mu});
    g.push_back({s, mu});
  } else {
    backtrack(u + 1, 0, s, mu);
    if (v < fact[u].second) {
      if (v)
        backtrack(u, v + 1, s * fact[u].first, 0);
      else
        backtrack(u, v + 1, s * fact[u].first, -mu);
    }
  }
}
int main() {
  cin >> m >> x;
  long long ans = 0;
  long long phi = m;
  long long temp = m;
  for (long long i = 2; i * i <= temp; i++) {
    if (temp % i == 0) {
      fact.push_back({i, 0});
      while (temp % i == 0) {
        temp /= i;
        fact.back().second++;
      }
    }
  }
  if (temp > 1) {
    fact.push_back({temp, 1});
  }
  for (auto &x : fact) {
    phi /= x.first;
    phi *= x.first - 1;
  }
  long long sz = phi;
  long long val = 1;
  for (long long i = 1; i * i <= phi; i++) {
    if (phi % i == 0) {
      d.push_back({i, i});
      d.push_back({phi / i, phi / i});
    }
  }
  sort(d.begin(), d.end());
  d.erase(unique(d.begin(), d.end()), d.end());
  for (auto &x : d) x.second = powerx(x.first, m);
  backtrack(0, 0, 1);
  sort(g.begin(), g.end());
  sort(r.begin(), r.end());
  for (auto &x : g) {
    long long cnt = 0;
    for (auto y : r) {
      if (x.first > m / y.first) break;
      y.first *= x.first;
      if (m % y.first) continue;
      cnt += (m / y.first) * y.second;
    }
    for (auto sz : d) {
      if ((sz.second - 1) % (m / x.first) == 0) {
        ans += cnt / sz.first;
        break;
      }
    }
  }
  cout << ans << '\n';
}