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 int addmod(long long int X, long long int Y, long long int mod) {
  return (X += Y) < mod ? X : X - mod;
}
long long int mulmod(long long int X, long long int Y, long long int mod) {
  long long int R = 0, W = X;
  while (1) {
    if (Y & 1) R = addmod(R, W, mod);
    if (Y >>= 1)
      W = addmod(W, W, mod);
    else
      break;
  }
  return R;
}
bool pw(long long int X, long long int Y, long long int mod) {
  long long int R = 1, W = X;
  while (1) {
    if (Y & 1) R = mulmod(R, W, mod);
    if (Y >>= 1)
      W = mulmod(W, W, mod);
    else
      break;
  }
  return R == 1;
}
long long int N, X;
vector<pair<long long int, int> > fac;
vector<vector<long long int> > e;
int L;
long long int res;
long long int gcd(long long int X, long long int Y) {
  return Y ? gcd(Y, X % Y) : X;
}
long long int LCM(long long int X, long long int Y) {
  return X / gcd(X, Y) * Y;
}
void dfs(int i, long long int cur, long long int lcm) {
  if (i == L) {
    res += cur / lcm;
    return;
  }
  dfs(i + 1, cur, lcm);
  long long int P = fac[i].first;
  cur *= (P - 1);
  dfs(i + 1, cur, LCM(lcm, e[i][1]));
  for (int j = 2; j <= fac[i].second; j++) {
    cur *= P;
    dfs(i + 1, cur, LCM(lcm, e[i][j]));
  }
}
int main() {
  cin >> N >> X;
  for (long long int i = 2; i * i <= N; i++)
    if (N % i == 0) {
      int c = 0;
      while (N % i == 0) {
        N /= i;
        c++;
      }
      fac.push_back(make_pair(i, c));
    }
  if (N > 1) fac.push_back(make_pair(N, 1));
  L = fac.size();
  e.resize(L);
  for (int i = 0; i < L; i++) {
    long long int P = fac[i].first, E = fac[i].second;
    e[i].resize(E + 1);
    long long int X2 = X % P, T = 1;
    bool ff = false;
    for (; T * T <= P - 1; T++)
      if ((P - 1) % T == 0)
        if (pw(X2, T, P)) {
          e[i][1] = T;
          ff = true;
          break;
        }
    if (!ff) {
      for (; T >= 1; T--)
        if ((P - 1) % T == 0) {
          if (pw(X2, (P - 1) / T, P)) {
            e[i][1] = (P - 1) / T;
            ff = true;
            break;
          }
        }
    }
    long long int PP = P;
    for (int j = 2; j <= E; j++) {
      PP *= P;
      if (pw(X % PP, e[i][j - 1], PP))
        e[i][j] = e[i][j - 1];
      else
        e[i][j] = e[i][j - 1] * P;
    }
  }
  dfs(0, 1, 1);
  cout << res << endl;
  return 0;
}
