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;
bitset<10000005> isPrime;
vector<int> primes;
long long fiA[10000005];
long long ordA1[10000005], ordA2[10000005];
long long tot = 0, cont = 0;
long long m, x;
void criba() {
  isPrime.set();
  isPrime[0] = isPrime[1] = 0;
  for (long long i = 2; i < 10000005; ++i)
    if (isPrime[i]) {
      for (long long j = i * i; j < 10000005; j += i) isPrime[j] = false;
      primes.push_back(i);
    }
}
long long mulmod(long long a, long long b, long long c) {
  long long res = 0;
  while (a != 0) {
    if (a & 1) res = (res + b) % c;
    a >>= 1LL;
    b = (b << 1) % c;
  }
  return res;
}
long long fastPow(long long a, long long b, long long mod) {
  long long res = 1;
  while (b != 0) {
    if (b & 1LL) res = mulmod(res, a, mod), b--;
    a = mulmod(a, a, mod);
    b >>= 1;
  }
  return res;
}
long long fiF(long long N) {
  long long PF_idx = 0, PF = primes[PF_idx], ans = N;
  while (PF_idx < (int)primes.size() && PF * PF <= N) {
    if (N % PF == 0) ans -= ans / PF;
    while (N % PF == 0) N /= PF;
    PF = primes[++PF_idx];
  }
  if (N != 1) ans -= ans / N;
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  criba();
  cont = tot = 0;
  cin >> m >> x;
  set<long long> div, dd;
  long long num = fiF(m);
  for (long long i = 1; i * i <= num; ++i) {
    if (num % i != 0) continue;
    div.insert(i);
    div.insert(num / i);
  }
  set<long long>::iterator it;
  int pos = 0;
  long long re;
  for (it = div.begin(), pos = 0; it != div.end(); ++it, pos++) {
    re = fastPow(x, *it, m);
    ordA1[pos] = *it;
    ordA2[pos] = (re) ? re : m;
  }
  long long d;
  num = m;
  for (long long i = 1; i * i <= num; ++i) {
    if (num % i != 0) continue;
    dd.insert(i);
    dd.insert(num / i);
  }
  for (it = dd.begin(), pos = 0; it != dd.end(); ++it, ++pos) {
    d = *it;
    for (int j = 0; j < div.size(); ++j)
      if (ordA2[j] % d == 1) {
        tot += (fiF(d) / ordA1[j]);
        break;
      }
  }
  ++tot;
  cout << tot << "\n";
}