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 product(long long p, long long q, long long mod) {
  if (q == 0) return 0;
  if (q & 1) return (product(p, q - 1, mod) + p) % mod;
  return (product(p, q >> 1, mod) << 1) % mod;
}
long long bigmod(long long x, long long y, long long mod) {
  if (y == 0) {
    return 1;
  }
  if (y & 1) return product(bigmod(x, y - 1, mod), x, mod);
  long long m = bigmod(x, y >> 1, mod);
  return product(m, m, mod);
}
vector<long long> divisors, divisors2, power;
vector<pair<long long, long long>> primes;
map<long long, long long> phi;
void factorize(long long m) {
  for (long long i = 2; i * i <= m; i++) {
    long long cnt = 0;
    while (m % i == 0) {
      m /= i;
      cnt++;
    }
    if (cnt) {
      primes.push_back(make_pair(i, cnt));
    }
  }
  if (m > 1) {
    primes.push_back(make_pair(m, 1));
  }
}
void calc_phi(long long idx, long long curval, long long curphi) {
  if (idx == primes.size()) {
    phi[curval] = curphi;
    divisors.push_back(curval);
    return;
  }
  for (int i = 0; i <= primes[idx].second; i++) {
    if (i == 0)
      calc_phi(idx + 1, curval, curphi);
    else
      calc_phi(idx + 1, curval, curphi * (primes[idx].first - 1));
    curval *= primes[idx].first;
    if (i > 0) curphi *= primes[idx].first;
  }
}
long long ans = 0;
void solve(long long idx, long long curval, long long cnt) {
  if (idx == primes.size()) {
    if (curval == 1) return;
    ans += phi[curval] / divisors2[cnt];
    return;
  }
  for (int i = 0; i <= primes[idx].second; i++) {
    while (curval > 1 && power[cnt] % curval != 1) {
      cnt++;
    }
    solve(idx + 1, curval, cnt);
    curval *= primes[idx].first;
  }
}
int main() {
  long long m, x, i, j, k, y;
  cin >> m >> x;
  factorize(m);
  calc_phi(0, 1, 1);
  long long now = phi[m];
  for (i = 1; i * i <= now; i++) {
    if (now % i == 0) {
      divisors2.push_back(i);
      if (i * i != now) divisors2.push_back(now / i);
    }
  }
  sort(divisors.begin(), divisors.end());
  sort(divisors2.begin(), divisors2.end());
  for (i = 0; i < divisors2.size(); i++) {
    power.push_back(bigmod(x, divisors2[i], m));
  }
  solve(0, 1, 0);
  cout << ans + 1 << endl;
  return 0;
}
