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;
const int MAXF = 20;
const long long B = 1e4;
long long M, X;
vector<pair<long long, int> > v;
vector<pair<long long, long long> > vv[MAXF];
long long gcf(long long x, long long y) {
  if (y == 0) return x;
  return gcf(y, x % y);
}
long long mult(long long x, long long y, long long m) {
  if (y < B) return (x * y) % m;
  return (mult(x, y / B, m) * B + (x * (y % B))) % m;
}
long long mpow(long long e, long long m) {
  long long cpow = X, res = 1;
  while (e) {
    if (e & 1) res = mult(res, cpow, m);
    cpow = mult(cpow, cpow, m);
    e /= 2;
  }
  return res;
}
vector<pair<long long, int> > factorize(long long c) {
  vector<pair<long long, int> > res;
  for (int i = 2; i * (long long)i <= c; i++) {
    if (c % i == 0) {
      res.push_back(make_pair(i, 0));
      while (c % i == 0) {
        res.back().second++;
        c /= i;
      }
    }
  }
  if (c > 1) {
    res.push_back(make_pair(c, 1));
  }
  return res;
}
long long ans;
void dfs(int cloc, long long cord, long long ctot) {
  if (cloc == v.size()) {
    ans += ctot / cord;
    return;
  }
  for (int i = 0; i < vv[cloc].size(); i++) {
    long long nord = vv[cloc][i].first, ntot = vv[cloc][i].second;
    dfs(cloc + 1, cord / gcf(cord, nord) * nord, ctot * ntot);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> M >> X;
  v = factorize(M);
  for (int i = 0; i < v.size(); i++) {
    long long p = v[i].first;
    int c = v[i].second;
    long long cp = p, ccnt = p - 1;
    for (int j = 0; j <= c; j++) {
      if (!j) {
        vv[i].push_back(make_pair(1, 1));
        continue;
      }
      auto pf = factorize(ccnt);
      long long ctot = ccnt;
      for (int k = 0; k < pf.size(); k++) {
        long long cprime = pf[k].first;
        while (ctot % cprime == 0) {
          if (mpow(ctot / cprime, cp) == 1)
            ctot /= cprime;
          else
            break;
        }
      }
      vv[i].push_back(make_pair(ctot, ccnt));
      ccnt *= p;
      cp *= p;
    }
  }
  dfs(0, 1, 1);
  cout << ans << "\n";
  return 0;
}
