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, long long int>> pf;
vector<long long int> divIt;
bool stop = false;
long long int m, x;
void increment(long long int index) {
  if (divIt[index] < pf[index].second) {
    divIt[index]++;
  } else {
    if (index == divIt.size() - 1) {
      stop = true;
    } else {
      divIt[index] = 0;
      increment(index + 1);
    }
  }
}
long long mul(long long a, long long b) {
  long long ret = 0;
  while (b) {
    ret = (ret + a * (b & ((1 << 8) - 1))) % m;
    b >>= 8;
    a = (a << 8) % m;
  }
  return ret;
}
long long qp(long long x, long long y, long long m) {
  long long ret = 1;
  while (y) {
    if (y & 1) ret = mul(ret, x) % m;
    x = mul(x, x) % m;
    y >>= 1;
  }
  return ret;
}
int main() {
  cin >> m >> x;
  pair<long long int, long long int> tmp;
  long long int n = m;
  long long int it = 2;
  while (n > 1) {
    tmp.first = it;
    tmp.second = 0;
    if (it * it > n) {
      tmp.first = n;
      tmp.second = 1;
      pf.push_back(tmp);
      break;
    }
    while (n % it == 0) {
      tmp.second++;
      n = n / it;
    }
    if (tmp.second > 0) {
      pf.push_back(tmp);
    }
    it++;
  }
  vector<pair<long long int, long long>> tmpvec;
  vector<vector<pair<long long int, long long>>> pf2;
  for (long long int i = 0; i < pf.size(); i++) {
    tmpvec.clear();
    n = pf[i].first - 1;
    it = 2;
    while (n > 1) {
      tmp.first = it;
      tmp.second = 0;
      if (it * it > n) {
        tmp.first = n;
        tmp.second = 1;
        tmpvec.push_back(tmp);
        break;
      }
      while (n % it == 0) {
        tmp.second++;
        n = n / it;
      }
      if (tmp.second > 0) {
        tmpvec.push_back(tmp);
      }
      it++;
    }
    pf2.push_back(tmpvec);
  }
  for (long long int i = 0; i < pf.size(); i++) {
    divIt.push_back(0);
  }
  vector<pair<long long int, long long>> fipf;
  bool l = true;
  long long int fi;
  long long int order;
  long long int d;
  long long int tmporder;
  long long int ans = 0;
  long long int pow;
  long long int y;
  do {
    d = 1;
    for (long long int i = 0; i < divIt.size(); i++) {
      for (long long int j = 0; j < divIt[i]; j++) {
        d = d * pf[i].first;
      }
    }
    fipf.clear();
    fi = 1;
    for (long long int i = 0; i < divIt.size(); i++) {
      if (divIt[i] != 0) {
        tmp.first = pf[i].first;
        tmp.second = divIt[i] - 1;
        fipf.push_back(tmp);
        for (long long int j = 0; j < pf2[i].size(); j++) {
          l = true;
          for (long long int k = 0; k < fipf.size(); k++) {
            if (pf2[i][j].first == fipf[k].first) {
              fipf[k].second = fipf[k].second + pf2[i][j].second;
              l = false;
              break;
            }
          }
          if (l == true) {
            fipf.push_back(pf2[i][j]);
          }
        }
      }
    }
    for (long long int i = 0; i < fipf.size(); i++) {
      for (long long int j = 1; j <= fipf[i].second; j++) {
        fi = fi * fipf[i].first;
      }
    }
    order = 1;
    for (long long int i = 0; i < fipf.size(); i++) {
      tmporder = fi;
      for (long long int j = 0; j < fipf[i].second; j++) {
        tmporder = tmporder / fipf[i].first;
      }
      y = x;
      y = qp(x % d, tmporder, d);
      for (long long int j = 0; j <= fipf[i].second; j++) {
        if (y == 1) {
          break;
        }
        y = qp(y, fipf[i].first, d);
        order = order * fipf[i].first;
      }
    }
    ans = ans + fi / order;
    increment(0);
  } while (stop == false);
  if (x == 1) {
    ans = m;
  }
  cout << ans;
  return 0;
}
