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;
const int divMax = 25;
long long X, M, ans;
vector<pair<long long, int> > Primes, listOfPrimes[divMax];
int currentExp[divMax];
map<long long, int> Factorization;
int prime;
long long currDiv = 1;
inline void Add(long long& x, long long y, long long mod) {
  x += y;
  if (x >= mod) x -= mod;
}
long long multiply(long long a, long long b, long long mod) {
  if (a < b) swap(a, b);
  long long sol = 0;
  while (b) {
    if (b % 2 == 1) Add(sol, a, mod);
    Add(a, a, mod);
    b /= 2;
  }
  return sol;
}
long long power(long long a, long long b, long long mod) {
  long long sol = 1;
  while (b) {
    if (b % 2 == 1) sol = multiply(sol, a, mod);
    a = multiply(a, a, mod);
    b /= 2;
  }
  return sol;
}
void factorize(long long M, vector<pair<long long, int> >& Prime) {
  long long i = 2, nb = M;
  while (i * i <= M && i <= nb) {
    int exp = 0;
    while (nb % i == 0) {
      nb /= i;
      ++exp;
    }
    if (exp != 0) Prime.push_back(make_pair(i, exp));
    ++i;
  }
  if (nb != 1) Prime.push_back(make_pair(nb, 1));
}
void precalcListOfPrimes() {
  factorize(M, Primes);
  for (int i = 0; i < Primes.size(); i++) {
    factorize(Primes[i].first - 1, listOfPrimes[i]);
  }
}
void solveForDiv(long long div) {
  long long phi = div;
  Factorization.clear();
  for (int i = 0; i < Primes.size(); i++) {
    if (currentExp[i] != 0) {
      phi /= Primes[i].first;
      Factorization[Primes[i].first] += currentExp[i] - 1;
      phi *= (Primes[i].first - 1);
      for (int j = 0; j < listOfPrimes[i].size(); j++) {
        long long prm = listOfPrimes[i][j].first,
                  exp = listOfPrimes[i][j].second;
        Factorization[prm] += exp;
      }
    }
  }
  long long newX = X % div, res = phi;
  for (auto it = Factorization.begin(); it != Factorization.end();
       it = next(it)) {
    int limit = it->second;
    long long prime = it->first;
    for (int j = limit - 1; j >= 0; j--) {
      res /= prime;
      if (power(newX, res, div) != 1) {
        res *= prime;
        break;
      }
    }
  }
  ans += phi / res;
}
void browseDivs(int k) {
  if (k == Primes.size()) {
    solveForDiv(currDiv);
    return;
  }
  long long pw = 1;
  for (int i = 0; i <= Primes[k].second; i++) {
    currentExp[k] = i;
    currDiv *= pw;
    browseDivs(k + 1);
    currDiv /= pw;
    if (i < Primes[k].second) pw *= Primes[k].first;
  }
}
int main() {
  cin >> M >> X;
  precalcListOfPrimes();
  browseDivs(0);
  cout << ans << "\n";
  return 0;
}