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;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar()) {
    if (ch == '-') f = -1;
  }
  for (; isdigit(ch); ch = getchar()) {
    x = x * 10 + ch - 48;
  }
  return x * f;
}
long long quickmul(long long x, long long y, long long mod) {
  x %= mod, y %= mod;
  long long ret = (x * y - (long long)((long double)x / mod * y + 0.5) * mod);
  return ret < 0 ? ret + mod : ret;
}
long long quickpow(long long x, long long y, long long mod) {
  long long cur = x, ret = 1ll;
  for (int i = 0; y; i++) {
    if (y & (1ll << i)) {
      y -= (1ll << i);
      ret = quickmul(ret, cur, mod) % mod;
    }
    cur = quickmul(cur, cur, mod) % mod;
  }
  return ret;
}
vector<pair<long long, int> > pfac;
vector<long long> facphip;
long long p, ax, ans, phip;
long long getphi(long long x) {
  long long ret = x;
  for (int i = 0; i < pfac.size(); i++) {
    if (x % pfac[i].first == 0ll) {
      ret = ret / pfac[i].first * (pfac[i].first - 1ll);
    }
  }
  return ret;
}
long long getind(long long x) {
  long long ret = getphi(x);
  for (int i = 0; i < facphip.size(); i++) {
    while (ret % facphip[i] == 0 && quickpow(ax, ret / facphip[i], x) == 1ll) {
      ret /= facphip[i];
    }
  }
  for (int i = 0; i < pfac.size(); i++) {
    while (ret % pfac[i].first == 0 &&
           quickpow(ax, ret / pfac[i].first, x) == 1ll) {
      ret /= pfac[i].first;
    }
  }
  return ret;
}
void dfs(int pos, long long x) {
  if (pos == pfac.size()) {
    long long tmp = getphi(x) / getind(x);
    ans += tmp;
    return;
  }
  for (int i = 0; i <= pfac[pos].second; i++) {
    dfs(pos + 1, x);
    x *= pfac[pos].first;
  }
}
int main() {
  scanf("%I64d%I64d", &p, &ax);
  long long tmp = p;
  for (long long i = 2ll; i * i <= tmp; i++) {
    if (tmp % i == 0ll) {
      int k = 0;
      while (tmp % i == 0ll) {
        tmp /= i;
        k++;
      }
      pfac.push_back(make_pair(i, k));
    }
  }
  if (tmp > 1) {
    pfac.push_back(make_pair(tmp, 1));
  }
  phip = getphi(p);
  tmp = phip;
  for (long long i = 2ll; i * i <= tmp; i++) {
    if (tmp % i == 0ll) {
      while (tmp % i == 0ll) {
        tmp /= i;
      }
      facphip.push_back(i);
    }
  }
  if (tmp > 1) {
    facphip.push_back(tmp);
  }
  dfs(0, 1ll);
  printf("%I64d\n", ans);
  return 0;
}