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<unsigned long long, int>> factor(unsigned long long n) {
  vector<pair<unsigned long long, int>> out;
  unsigned long long m = n;
  unsigned long long p = 2;
  while (m > 1 && p * p <= m) {
    if (m % p == 0) {
      int count = 0;
      while (m % p == 0) {
        m /= p;
        count++;
      }
      out.push_back(make_pair(p, count));
    }
    p++;
  }
  if (m > 1) {
    out.push_back(make_pair(m, 1));
  }
  return out;
}
vector<unsigned long long> get_divs(
    vector<pair<unsigned long long, int>> &prexp, int ind) {
  if (ind == prexp.size()) {
    return vector<unsigned long long>(1, 1);
  } else {
    vector<unsigned long long> out;
    for (int i = 0; i <= prexp[ind].second; i++) {
      for (unsigned long long j : get_divs(prexp, ind + 1)) {
        out.push_back(j * pow(prexp[ind].first, i));
      }
    }
    return out;
  }
}
unsigned long long pow(unsigned long long p, int exp) {
  if (exp == 0) return 1;
  if (exp == 1) return p;
  if (exp % 2 == 0) return pow(p * p, exp / 2);
  return p * pow(p * p, (exp - 1) / 2);
}
unsigned long long modpow(unsigned long long p, unsigned long long mod,
                          unsigned long long exp) {
  if (exp == 0) return 1;
  if (exp == 1) return p % mod;
  p = p % mod;
  unsigned long long MOD = 100000000;
  unsigned long long a = p / MOD;
  unsigned long long b = p % MOD;
  unsigned long long x = (a * a) % mod;
  for (int i = 0; i < 8; i++) {
    x = (x * 100) % mod;
  }
  unsigned long long y = (2 * a * b) % mod;
  for (int i = 0; i < 4; i++) {
    y = (y * 100) % mod;
  }
  unsigned long long sq = x + y + (b * b) % mod;
  if (exp % 2 == 0) return modpow(sq % mod, mod, exp / 2);
  unsigned long long out1 = modpow(sq % mod, mod, (exp - 1) / 2);
  unsigned long long c = out1 / MOD;
  unsigned long long d = out1 % MOD;
  x = (a * c) % mod;
  for (int i = 0; i < 8; i++) {
    x = (x * 100) % mod;
  }
  y = (a * d + b * c) % mod;
  for (int i = 0; i < 4; i++) {
    y = (y * 100) % mod;
  }
  unsigned long long newsq = x + y + (b * d) % mod;
  return (newsq) % mod;
}
vector<unsigned long long> getexps(unsigned long long p, unsigned long long x,
                                   int alph) {
  vector<unsigned long long> v = {1};
  vector<pair<unsigned long long, int>> vv = factor(p - 1);
  for (int i = 1; i <= alph; i++) {
    vector<pair<unsigned long long, int>> newvv(vv.begin(), vv.end());
    if (i > 1) newvv.push_back(make_pair(p, i - 1));
    unsigned long long order = -1;
    for (unsigned long long j : get_divs(newvv, 0)) {
      if (modpow(x, pow(p, i), j) == 1) {
        order = j;
        break;
      }
    }
    v.push_back(order);
  }
  return v;
}
vector<vector<pair<unsigned long long, int>>> get_cool_divs(
    vector<pair<unsigned long long, int>> &prexp, int ind) {
  if (ind == -1) {
    return vector<vector<pair<unsigned long long, int>>>(
        1, vector<pair<unsigned long long, int>>());
  } else {
    vector<vector<pair<unsigned long long, int>>> out;
    for (int i = 0; i <= prexp[ind].second; i++) {
      for (auto v : get_cool_divs(prexp, ind - 1)) {
        v.push_back(make_pair(prexp[ind].first, i));
        out.push_back(v);
      }
    }
    return out;
  }
}
unsigned long long gcd(unsigned long long a, unsigned long long b) {
  unsigned long long aa = a;
  unsigned long long bb = b;
  while (bb > 0) {
    unsigned long long temp = aa;
    aa = bb;
    bb = temp % bb;
  }
  return aa;
}
int main() {
  unsigned long long m, x;
  scanf("%lld %lld", &m, &x);
  vector<pair<unsigned long long, int>> prexp = factor(m);
  unsigned long long out = 0;
  vector<vector<unsigned long long>> a_ij;
  for (int i = 0; i < prexp.size(); i++) {
    int alph = prexp[i].second;
    unsigned long long p = prexp[i].first;
    a_ij.push_back(getexps(p, x, alph));
  }
  for (auto v : get_cool_divs(prexp, prexp.size() - 1)) {
    unsigned long long lcd = 1;
    for (int i = 0; i < v.size(); i++) {
      if (a_ij[i][v[i].second] == 0) {
        continue;
      }
      unsigned long long gg = gcd(a_ij[i][v[i].second], lcd);
      lcd = lcd * a_ij[i][v[i].second] / gg;
    }
    unsigned long long total = 1;
    for (auto p : v) {
      if (p.second >= 1) total *= pow(p.first, p.second - 1) * (p.first - 1);
    }
    out += total / lcd;
  }
  printf("%lld", out);
  return 0;
}