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;
vector<pair<long long, long long> > merge(
    const vector<pair<long long, long long> > &a,
    const vector<pair<long long, long long> > &b) {
  vector<pair<long long, long long> > ans;
  int u = 0, v = 0;
  while (u < a.size() && v < b.size()) {
    if (a[u].first < b[v].first)
      ans.push_back(a[u++]);
    else if (a[u].first > b[v].first)
      ans.push_back(b[v++]);
    else {
      ans.emplace_back(a[u].first, a[u].second + b[v].second);
      u++;
      v++;
    }
  }
  while (u < a.size()) ans.push_back(a[u++]);
  while (v < b.size()) ans.push_back(b[v++]);
  return ans;
}
template <typename T>
void read(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f *= -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  x *= f;
}
long long x, m, ans, cx, mod;
int apow[20];
long long mul2(long long a, long long b) {
  long long c = a;
  a = 0;
  while (b) {
    if (b & 1) a = (a + c) % mod;
    c = (c + c) % mod;
    b >>= 1;
  }
  return a;
}
long long mul(long long a, long long b) {
  long long m = (long long)((long double)a * b / mod);
  long long rem = a * b - m * mod;
  while (rem < 0) rem += mod;
  while (rem >= mod) rem -= mod;
  return rem;
}
long long pow(long long a, long long b) {
  long long c = a;
  a = 1;
  while (b) {
    if (b & 1) a = mul(a, c);
    c = mul(c, c);
    b >>= 1;
  }
  return a;
}
vector<pair<long long, long long> > fac;
vector<pair<long long, long long> > facs[20];
vector<pair<long long, long long> > cur;
void ins(long long a, long long b, vector<pair<long long, long long> > &cur) {
  for (int i = 0; i < cur.size(); i++) {
    if (cur[i].first == a) {
      cur[i].second += b;
      return;
    }
  }
  cur.push_back(make_pair(a, b));
}
void cal(long long d) {
  long long phi = d, ord = 1;
  mod = d;
  cx = x % d;
  cur = fac;
  for (int i = 0; i < cur.size(); i++) {
    cur[i].second = max(apow[i] - 1, 0);
  }
  for (int i = 0; i < cur.size(); i++) {
    if (apow[i]) {
      phi = phi - phi / fac[i].first;
      cur = merge(cur, facs[i]);
    }
  }
  ord = phi;
  for (int i = 0; i < cur.size(); i++)
    while (ord % cur[i].first == 0 && pow(cx, ord / cur[i].first) == 1)
      ord /= cur[i].first;
  ans += phi / ord;
}
void sol(long long cu, long long depth) {
  if (depth == fac.size()) {
    cal(cu);
    return;
  }
  for (int i = 0; i <= fac[depth].second; i++) {
    apow[depth] = i;
    sol(cu, depth + 1);
    if (i != fac[depth].second) cu *= fac[depth].first;
  }
}
void factorize(long long m, vector<pair<long long, long long> > &fac) {
  for (long long i = 2; i * i <= m; i++) {
    if (m % i == 0) {
      fac.push_back(make_pair(i, 0));
      while (m % i == 0) {
        m /= i;
        fac[fac.size() - 1].second += 1;
      }
    }
  }
  if (m > 1) fac.push_back(make_pair(m, 1));
}
int main() {
  read(m);
  read(x);
  factorize(m, fac);
  for (int i = 0; i < fac.size(); i++) factorize(fac[i].first - 1, facs[i]);
  sol(1, 0);
  cout << ans << endl;
  return 0;
}