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 m, r;
vector<long long> phis;
vector<long long> orders;
long long gcd(long long a, long long b) {
  if (a < b) {
    long long temp = a;
    a = b;
    b = temp;
  }
  while (b != 0) {
    long long temp = a % b;
    a = b;
    b = temp;
  }
  return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
vector<long long> factor(long long m) {
  vector<long long> fs = {1};
  while (m % 2 == 0) {
    m /= 2;
    fs.push_back(fs.back() * 2);
  }
  for (int i = 3; m != 1 && i < 10000000; i += 2) {
    int cnt = 0;
    while (m % i == 0) {
      m /= i;
      cnt++;
    }
    int l = fs.size();
    for (int j = 0; j < cnt * l; j++) fs.push_back(fs[j] * i);
  }
  if (m != 1) {
    int l = fs.size();
    for (int i = 0; i < l; i++) fs.push_back(fs[i] * m);
  }
  return fs;
}
inline long long mult_mod(long long a, long long b, long long m) {
  long long a1 = a & 0xFFFF;
  a1 = a1 * b % m;
  long long a2 = (a >> 16) & 0xFFFF;
  a2 = a2 * b % m;
  for (int i = 0; i < 16; i++) {
    a2 += a2;
    a2 -= (a2 >= m) * m;
  }
  long long a3 = (a >> 32) & 0xFFFF;
  a3 = a3 * b % m;
  for (int i = 0; i < 32; i++) {
    a3 += a3;
    a3 -= (a3 >= m) * m;
  }
  return (a1 + a2 + a3) % m;
}
long long pow_mod(long long a, long long b, long long m) {
  long long overflow = 1;
  while (b > 1) {
    if (b % 2 == 1) {
      overflow = mult_mod(overflow, a, m);
    }
    a = mult_mod(a, a, m);
    b /= 2;
  }
  long long r = mult_mod(a, overflow, m);
  return r;
}
void update_orders(long long p, int pow) {
  vector<long long> p_factors = factor(p - 1);
  sort(p_factors.begin(), p_factors.end());
  vector<long long> ords;
  for (long long x : p_factors) {
    if (pow_mod(r, x, p) == 1) {
      ords.push_back(x);
      break;
    }
  }
  long long mod = p * p;
  for (int i = 1; i < pow; i++) {
    long long rpow = pow_mod(r, ords.back(), mod);
    int l = p_factors.size();
    for (int i = 0; i < l; i++) p_factors.push_back(p_factors[i] * p);
    sort(p_factors.begin(), p_factors.end());
    for (long long x : p_factors) {
      if (pow_mod(rpow, x, mod) == 1) {
        ords.push_back(x * ords.back());
        break;
      }
    }
    mod *= p;
  }
  int l = orders.size();
  for (int i = 0; i < pow; i++) {
    for (int j = 0; j < l; j++) {
      orders.push_back(lcm(orders[j], ords[i]));
    }
  }
}
void update_phis(long long p, int pow) {
  long long phi = p - 1;
  int l = phis.size();
  for (int i = 0; i < pow; i++) {
    for (int j = 0; j < l; j++) phis.push_back(phis[j] * phi);
    phi *= p;
  }
}
int main() {
  scanf("%lld%lld", &m, &r);
  phis.push_back(1);
  orders.push_back(1);
  int cnt = 0;
  while (m % 2 == 0) {
    m /= 2;
    cnt++;
  }
  if (cnt > 0) {
    update_orders(2, cnt);
    update_phis(2, cnt);
  }
  for (int i = 3; m != 1 && i < 10000000; i += 2) {
    int cnt = 0;
    while (m % i == 0) {
      m /= i;
      cnt++;
    }
    if (cnt > 0) {
      update_orders(i, cnt);
      update_phis(i, cnt);
    }
  }
  if (m != 1) {
    update_orders(m, 1);
    update_phis(m, 1);
  }
  long long ans = 0;
  for (int i = 0; i < phis.size(); i++) {
    ans += phis[i] / orders[i];
  }
  printf("%lld\n", ans);
}
