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;
const long long maxn = 100000 + 10;
long long m, s, prim[maxn], prime[maxn], yinm[maxn];
int pnum = 0, penum = 0, ynum = 0;
void prime1(long long x, long long p[], int& num) {
  num = 0;
  for (long long i = 2; i <= sqrt(x + 1); i++) {
    if (x % i == 0) {
      p[++num] = i;
      while (x % i == 0) x /= i;
    }
  }
  if (x != 1) p[++num] = x;
  sort(p + 1, p + num + 1);
}
void yin(long long x, long long p[], int& num) {
  num = 0;
  for (long long i = 1; i <= sqrt(x + 1); i++) {
    if (x % i == 0) {
      p[++num] = i;
      if (i * i != x) p[++num] = x / i;
    }
  }
  sort(p + 1, p + num + 1);
}
long long phi(long long x) {
  for (int i = 1; i <= pnum; i++)
    if (x % prim[i] == 0) x = x / prim[i] * (prim[i] - 1ll);
  return x;
}
long long mul(long long x, long long y, long long t) {
  long long m = (long long)((long double)(x)*y / t);
  long long ans = x * y - m * t;
  while (ans < 0) ans += t;
  while (ans >= t) ans -= t;
  return ans;
}
long long qpow(long long x, long long y, long long t) {
  x %= t;
  long long ans = 1;
  while (y) {
    if ((long long)y & 1) ans = mul(ans, x, t);
    y = (long long)y >> 1;
    x = mul(x, x, t);
  }
  return ans;
}
int main() {
  cin >> m >> s;
  prime1(m, prim, pnum);
  yin(m, yinm, ynum);
  long long e = phi(m), ans = 0;
  if (e > 1)
    prime1(e, prime, penum);
  else
    prime[++penum] = 1;
  for (int i = 1; i <= ynum; i++) {
    long long p = phi(yinm[i]), q = p;
    if (q > 1) {
      for (int j = 1; j <= penum; j++)
        while (q % prime[j] == 0 && qpow(s, q / prime[j], yinm[i]) == 1)
          q /= prime[j];
    }
    ans += p / q;
  }
  cout << ans << endl;
  return 0;
}