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.
#include <bits/stdc++.h>
using namespace std;
long long p[100010], num[100010], swp[100010], n;
int tot, tot2, cnt;
void pre_work(long long x) {
  for (long long i = 1; i * i <= x; i++)
    if (x % i == 0) {
      num[++tot] = i;
      if (i * i != x) num[++tot] = x / i;
    }
  sort(num + 1, num + tot + 1);
}
void work(long long x, long long p[], int &tot) {
  for (long long i = 2; i * i <= x; i++)
    if (x % i == 0) {
      p[++tot] = i;
      while (x % i == 0) x /= i;
    }
  if (x > 1) p[++tot] = x;
}
long long euler(long long x) {
  long long res = x;
  for (int i = 1; i <= cnt; i++)
    if (res % p[i] == 0) res -= res / p[i];
  return res;
}
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 ksm(long long a, long long b, long long mod) {
  long long ans = 1;
  for (; b; b >>= 1) {
    if (b & 1) ans = mul(ans, a, mod);
    a = mul(a, a, mod);
  }
  return ans;
}
int main() {
  long long x;
  scanf("%lld%lld", &n, &x);
  pre_work(n);
  work(n, p, cnt);
  long long ans = 0, er = euler(n);
  if (er > 1)
    work(er, swp, tot2);
  else
    swp[++tot2] = 1;
  for (int i = 1; i <= tot; i++) {
    long long u, er1;
    u = er1 = euler(num[i]);
    if (u > 1)
      for (int j = 1; j <= tot2; j++)
        while (u % swp[j] == 0 && ksm(x, u / swp[j], num[i]) == 1) u /= swp[j];
    ans += er1 / u;
  }
  printf("%lld\n", ans);
  return 0;
}
