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, phi;
vector<long long> d, p, _, f;
vector<int> c;
vector<long long> getd(long long m) {
  vector<long long> r;
  for (int i = 1; (long long)i * i <= m; ++i)
    if (m % i == 0) {
      r.emplace_back(i);
      if (m / i != i) r.emplace_back(m / i);
    }
  sort(r.begin(), r.end());
  return r;
}
vector<long long> getp(long long m) {
  vector<long long> p;
  for (int i = 2; (long long)i * i <= m; ++i)
    if (m % i == 0) {
      p.emplace_back(i);
      while (m % i == 0) m /= i;
    }
  if (m > 1) p.emplace_back(m);
  return p;
}
inline long long mul(long long a, long long b, const long long& md) {
  long long c = a * b - (long long)((long double)a / md * b + .5) * md;
  return (c >> 63 & md) + c;
}
inline long long pow(long long a, long long b, const long long& md) {
  long long res = 1;
  for (; b; b >>= 1, a = mul(a, a, md))
    if (b & 1) res = mul(res, a, md);
  return res;
}
long long a(long long m, long long md) {
  assert(pow(x % md, m, md) == 1 % md);
  for (long long i : _)
    while (m % i == 0 && pow(x % md, m / i, md) == 1 % md) m /= i;
  return m;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> m >> x;
  d = getd(m), p = getp(m);
  phi = m;
  for (long long i : p) phi = phi / i * (i - 1);
  for (long long v : p) {
    int ct = 0;
    long long k = m;
    while (k % v == 0) k /= v, ++ct;
    c.emplace_back(ct);
  }
  _ = getp(phi);
  for (long long v : d) {
    long long x = 1;
    for (int i = 0; i < (int)p.size(); ++i) {
      int ct = c[i];
      long long nw = v;
      while (nw % p[i] == 0) nw /= p[i], --ct;
      for (int j = 1; j < ct; ++j) x *= p[i];
      if (ct) x *= p[i] - 1;
    }
    f.emplace_back(x);
  }
  long long ans = 0;
  for (int i = 0; i < (int)d.size(); ++i) ans += f[i] / a(f[i], m / d[i]);
  cout << ans << '\n';
  return 0;
}