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;
vector<pair<long long, int>> factor(long long N) {
  vector<pair<long long, int>> ans;
  for (long long p = 2; p * p <= N; ++p)
    if (N % p == 0) {
      int time = 0;
      while (N % p == 0) N /= p, ++time;
      ans.emplace_back(p, time);
    }
  if (N != 1) ans.emplace_back(N, 1);
  return ans;
}
vector<pair<long long, int>> factor_with_primes(long long N,
                                                vector<long long> &P) {
  vector<pair<long long, int>> ans;
  for (long long pr : P) {
    int time = 0;
    while (N % pr == 0) ++time, N /= pr;
    if (time) ans.emplace_back(pr, time);
  }
  assert(N == 1);
  return ans;
}
long long my_pow(long long p, int t) {
  long long ans = 1;
  for (long long cur = p; t; t >>= 1, cur *= cur)
    if (t & 1) ans *= cur;
  return ans;
}
long long LLmul(long long a, long long b, long long mod) {
  return (a * b - (long long)((long double)a * b / mod) * mod) % mod;
}
long long powmod(long long x, long long p, long long mod) {
  long long s = 1, m = x % mod;
  for (; p; m = LLmul(m, m, mod), p >>= 1)
    if (p & 1) s = LLmul(s, m, mod);
  return s;
}
vector<long long> get_primes(long long N) {
  vector<long long> ans;
  for (long long p = 2; p * p <= N; ++p)
    if (N % p == 0) {
      ans.push_back(p);
      while (N % p == 0) N /= p;
    }
  if (N != 1) ans.push_back(N);
  return ans;
}
vector<long long> m_primes;
long long solve(long long a, long long m) {
  if (m == 1) return 1;
  auto fac = factor_with_primes(m, m_primes);
  long long phi = accumulate(fac.begin(), fac.end(), 1ll,
                             [=](long long a, pair<long long, int> p_r) {
                               return a * (my_pow(p_r.first, p_r.second) -
                                           my_pow(p_r.first, p_r.second - 1));
                             });
  auto phi_fac = factor(phi);
  long long ans = phi;
  for (auto P : phi_fac) {
    long long p = P.first, t = P.second;
    for (int i = 0; i < t; ++i) {
      if (powmod(a, ans / p, m) == 1)
        ans /= p;
      else
        break;
    }
  }
  return phi / ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long m, x;
  cin >> m >> x;
  long long ans = 0;
  m_primes = get_primes(m);
  for (long long g = 1; g * g <= m; ++g) {
    if (m % g == 0) {
      ans += solve(x % (m / g), m / g);
      if (g * g != m) ans += solve(x % g, g);
    }
  }
  cout << ans << '\n';
}
