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;
void Freopen() {
  freopen(
      "title"
      ".in",
      "r", stdin);
  freopen(
      "title"
      ".out",
      "w", stdout);
}
long long read() {
  long long g = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || '9' < ch) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    g = g * 10 + ch - '0';
    ch = getchar();
  }
  return g * f;
}
const long long N = 100000;
long long a[N], ce[N], p[N], cnt, n, m, x, tot, ans, f[N], l[N];
map<long long, long long> id;
long long ksc(long long x, long long y, long long mod) {
  x %= mod, y %= mod;
  return ((x * y -
           (long long)((long long)((long double)x / mod * y + 1e-3) * mod)) %
              mod +
          mod) %
         mod;
}
long long ksm(long long x, long long y, long long mod) {
  long long re = 1;
  while (y) {
    if (y & 1) re = ksc(re, x, mod);
    x = ksc(x, x, mod);
    y >>= 1;
  }
  return re;
}
bool mr(long long n) {
  if (n == 2) return true;
  if (n < 2 || !(n & 1)) return false;
  long long m = n - 1, k = 0, cnt = 10;
  while (!(m & 1)) k++, m >>= 1;
  for (long long i = 1; i <= cnt; i++) {
    long long a = rand() % (n - 1) + 1, x = ksm(a, m, n), y = 0;
    for (long long j = 1; j <= k; j++) {
      y = ksc(x, x, n);
      if (y == 1 && x != 1 && x != n - 1) return false;
      x = y;
    }
    if (y != 1) return false;
  }
  return true;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long rho(long long n, long long c) {
  long long x, y, d, i = 1, k = 2;
  x = y = rand() % (n - 1) + 1;
  while (true) {
    i++;
    x = (ksc(x, x, n) + c) % n;
    d = gcd((y - x + n) % n, n);
    if (d != 1 && d != n) return d;
    if (y == x) return n;
    if (i == k) y = x, k <<= 1;
  }
}
void find(long long n, long long k) {
  if (n == 1) return;
  if (mr(n)) return ce[++tot] = n, void();
  long long p = n;
  while (p >= n) p = rho(p, k--);
  find(p, k), find(n / p, k);
}
signed main() {
  m = read(), x = read();
  for (long long i = 1; i * i <= m; i++)
    if (!(m % i)) {
      a[++n] = i;
      if (m / i != i) a[++n] = m / i;
    }
  sort(a + 1, a + n + 1);
  for (long long i = (1); i <= (n); i++) id[a[i]] = i;
  long long re = m;
  for (long long i = 2; i * i <= re; i++)
    if (!(re % i)) {
      p[++cnt] = i;
      while (!(re % i)) re /= i;
    }
  if (re > 1) p[++cnt] = re;
  for (long long i = (1); i <= (n); i++) f[i] = m / a[i] - 1;
  for (long long j = (1); j <= (cnt); j++)
    for (long long i = (1); i <= (n); i++)
      if (!(a[i] % p[j])) f[id[a[i] / p[j]]] -= f[i];
  for (long long i = (n); i >= (1); i--) {
    l[i] = a[i];
    for (long long j = (1); j <= (cnt); j++)
      if (!(l[i] % p[j])) l[i] = l[i] / p[j] * (p[j] - 1);
    tot = 0, find(l[i], 10007), sort(ce + 1, ce + tot + 1);
    tot = unique(ce + 1, ce + tot + 1) - ce - 1;
    for (long long j = (1); j <= (tot); j++)
      while (!(l[i] % ce[j]) && ksm(x, l[i] / ce[j], a[i]) == 1) l[i] /= ce[j];
  }
  long long ans = 1;
  for (long long i = (1); i <= (n - 1); i++) ans += f[i] / l[id[m / a[i]]];
  return cout << ans, signed();
}