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>
const int N = 10000054;
long long n, x, ans;
int omega;
int pn = 0, c[N], p[666666];
std::pair<long long, int> ft[54], fg[54][54];
std::map<long long, int> D;
long long Power(long long a, long long n, long long c = 1) {
  for (; n; n >>= 1, a *= a)
    if (n & 1) c *= a;
  return c;
}
long long MulMod(long long a, long long b, long long m) {
  return (__int128)a * b % m;
}
long long PowerMod(long long a, long long n, long long m) {
  if (!n || a == 1) return 1ll;
  long long s = PowerMod(a, n >> 1, m);
  s = MulMod(s, s, m);
  return n & 1 ? MulMod(s, a, m) : s;
}
void sieve(int n) {
  int i, j, v;
  memset(c, -1, sizeof c);
  for (i = 2; i <= n; ++i) {
    if (!~c[i]) p[pn] = i, c[i] = pn++;
    for (j = 0; (v = i * p[j]) <= n && j <= c[i]; ++j) c[v] = j;
  }
}
void factor(long long n) {
  int i = 0, j;
  for (omega = 0; n != 1;) {
    if (n >= N) {
      for (; (long long)p[i] * p[i] <= n && n % p[i]; ++i)
        ;
      if (n % p[i]) {
        ft[omega++] = std::pair<long long, int>(n, 1);
        return;
      }
    } else
      i = c[n];
    for (j = 0; !(n % p[i]); n /= p[i], ++j)
      ;
    ft[omega++] = std::pair<long long, int>(p[i], j);
  }
}
void refactor(long long n, std::pair<long long, int> *out) {
  int i = 0, j;
  for (; n != 1;) {
    if (n >= N) {
      for (; (long long)p[i] * p[i] <= n && n % p[i]; ++i)
        ;
      if (n % p[i]) {
        *out++ = std::pair<long long, int>(n, 1);
        return;
      }
    } else
      i = c[n];
    for (j = 0; !(n % p[i]); n /= p[i], ++j)
      ;
    *out++ = std::pair<long long, int>(p[i], j);
  }
}
inline void push(long long p, int a) {
  if (!(D[p] += a)) D.erase(p);
}
inline void modify(std::pair<long long, int> *seq, int coef) {
  for (; seq->first; ++seq) push(seq->first, seq->second * coef);
}
long long MultiplicativeOrder(long long x, long long n, long long phi_n) {
  if (n == 1) return 1;
  long long partial, R, ret = 1;
  x %= n;
  for (const std::pair<long long, int> &d : D) {
    partial = phi_n / Power(d.first, d.second);
    for (R = PowerMod(x, partial, n); R != 1; ret *= d.first)
      R = PowerMod(R, d.first, n);
  }
  return ret;
}
void dfs(int dep, long long v, long long phi) {
  if (dep >= omega) {
    ans += phi / MultiplicativeOrder(x, v, phi);
    return;
  }
  for (int i = 0; i <= ft[dep].second; ++i)
    dfs(dep + 1, v, phi), v *= ft[dep].first, phi *= ft[dep].first - !i,
        i ? push(ft[dep].first, 1) : modify(fg[dep], 1);
  push(ft[dep].first, -ft[dep].second), modify(fg[dep], -1);
}
int main() {
  sieve(N - 1);
  scanf("%lld%lld", &n, &x), factor(n);
  for (int i = 0; i < omega; ++i) refactor(ft[i].first - 1, fg[i]);
  dfs(0, 1, 1), printf("%lld\n", ans);
  return 0;
}