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 N = 10000010;
long long p, s;
vector<pair<long long, long long> > v;
vector<long long> dsr;
long long las;
long long pw1[N + 1], pw2[N + 1];
long long qpow(long long x) {
  return (__int128_t)pw2[x / (las + 1)] * pw1[x % (las + 1)] % p;
}
long long ans;
void dfs(long long x = 0, long long val = 1, long long phi = 1) {
  if (x == v.size()) {
    long long ord;
    for (long long i = 0; i < dsr.size(); i++) {
      if (qpow(dsr[i]) % val == 1 % val) {
        ord = dsr[i];
        break;
      }
    }
    ans += phi / ord;
    return;
  }
  for (long long i = 0, now = 1; i <= v[x].second; i++, now *= v[x].first)
    dfs(x + 1, val * now,
        i == 0 ? phi : phi * (now / v[x].first * (v[x].first - 1)));
}
signed main() {
  cin >> p >> s;
  long long cpy = p;
  long long phip = p;
  for (long long i = 2; i * i <= p; i++)
    if (p % i == 0) {
      v.push_back(make_pair(i, 0));
      phip = phip / i * (i - 1);
      while (p % i == 0) v.back().second++, p /= i;
    }
  if (p > 1) v.push_back(make_pair(p, 1)), phip = phip / p * (p - 1);
  p = cpy;
  for (long long i = 1; i * i <= phip; i++) {
    las = i;
    if (phip % i == 0) dsr.push_back(i);
  }
  for (long long i = las; i; i--)
    if (phip % i == 0 && phip / i != i) dsr.push_back(phip / i);
  pw1[0] = pw2[0] = 1;
  for (long long i = 1; i <= las + 1; i++)
    pw1[i] = (__int128_t)pw1[i - 1] * s % p;
  for (long long i = 1; i <= las + 1; i++)
    pw2[i] = (__int128_t)pw2[i - 1] * pw1[las + 1] % p;
  dfs();
  cout << ans;
  return 0;
}