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;
template <typename T>
ostream &operator<<(ostream &out, const vector<T> &V) {
  if (!V.empty()) {
    out << "[";
    for (auto v : V) out << v << ", ";
    out << "\b\b]";
  } else {
    out << "[]";
  }
  return out;
}
template <typename T>
void __exit(T message) {
  cout << message << endl;
  exit(0);
}
inline void get_prime_divisors(long long n, vector<long long> &Div) {
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      long long k = 0;
      for (; n % i == 0; k++) n /= i;
      Div.push_back(i);
    }
  }
  if (n > 1) Div.push_back(n);
}
long long first(long long n, vector<long long> &Div) {
  long long fi_n = 1;
  for (long long i : Div) {
    if (n % i == 0) {
      long long k = 0, pq = 1;
      for (; n % i == 0; k++, pq *= i) n /= i;
      fi_n *= (pq / i * (i - 1));
    }
  }
  if (n > 1) {
    fi_n *= (n - 1);
  }
  return fi_n;
}
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
long long russian_multiply(long long a, long long b, long long &q) {
  long long result = 0, one = 1ll;
  for (int i = 63; i >= 0; i--) {
    result *= 2;
    if (b & (one << i)) result += a;
    while (result >= q) result %= q;
  }
  return result;
}
long long fast_exp(long long a, long long k, long long &q) {
  if (k == 0) return 1;
  long long aa = russian_multiply(a, a, q);
  return (k % 2 == 0) ? fast_exp(aa, k / 2, q)
                      : russian_multiply(a, fast_exp(aa, (k - 1) / 2, q), q);
}
long long find_min_k(long long k, long long x, long long m,
                     vector<long long> &Div) {
  long long kk, val;
  for (long long d : Div) {
    kk = k;
    while (kk % d == 0) kk /= d;
    val = fast_exp(x, kk, m);
    while (val != 1 && val != 0) {
      kk *= d;
      val = fast_exp(val, d, m);
    }
    k = kk;
  }
  return k;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  long long m, x;
  cin >> m >> x;
  vector<long long> Div_m, Div_fi;
  get_prime_divisors(m, Div_m);
  long long ans = 0, fi_m = first(m, Div_m);
  get_prime_divisors(fi_m, Div_fi);
  for (long long d = 1; d * d <= m; d++) {
    if (m % d == 0) {
      long long fi_d = first(d, Div_m);
      ans += fi_d / find_min_k(fi_d, x, d, Div_fi);
      if (d != (m / d)) {
        long long fi_m_by_d = first(m / d, Div_m);
        ans += fi_m_by_d / find_min_k(fi_m_by_d, x, (m / d), Div_fi);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
