#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > merge(
    const vector<pair<long long, long long> > &a,
    const vector<pair<long long, long long> > &b) {
  vector<pair<long long, long long> > ans;
  int u = 0, v = 0;
  while (u < a.size() && v < b.size()) {
    if (a[u].first < b[v].first)
      ans.push_back(a[u++]);
    else if (a[u].first > b[v].first)
      ans.push_back(b[v++]);
    else {
      ans.emplace_back(a[u].first, a[u].second + b[v].second);
      u++;
      v++;
    }
  }
  while (u < a.size()) ans.push_back(a[u++]);
  while (v < b.size()) ans.push_back(b[v++]);
  return ans;
}
template <typename T>
void read(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f *= -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  x *= f;
}
long long x, m, ans, cx, mod;
int apow[20];
long long mul2(long long a, long long b) {
  long long c = a;
  a = 0;
  while (b) {
    if (b & 1) a = (a + c) % mod;
    c = (c + c) % mod;
    b >>= 1;
  }
  return a;
}
long long mul(long long a, long long b) {
  long long m = (long long)((long double)a * b / mod);
  long long rem = a * b - m * mod;
  while (rem < 0) rem += mod;
  while (rem >= mod) rem -= mod;
  return rem;
}
long long pow(long long a, long long b) {
  long long c = a;
  a = 1;
  while (b) {
    if (b & 1) a = mul(a, c);
    c = mul(c, c);
    b >>= 1;
  }
  return a;
}
vector<pair<long long, long long> > fac;
vector<pair<long long, long long> > facs[20];
vector<pair<long long, long long> > cur;
void ins(long long a, long long b, vector<pair<long long, long long> > &cur) {
  for (int i = 0; i < cur.size(); i++) {
    if (cur[i].first == a) {
      cur[i].second += b;
      return;
    }
  }
  cur.push_back(make_pair(a, b));
}
void cal(long long d) {
  long long phi = d, ord = 1;
  mod = d;
  cx = x % d;
  cur = fac;
  for (int i = 0; i < cur.size(); i++) {
    cur[i].second = max(apow[i] - 1, 0);
  }
  for (int i = 0; i < cur.size(); i++) {
    if (apow[i]) {
      phi = phi - phi / fac[i].first;
      cur = merge(cur, facs[i]);
    }
  }
  ord = phi;
  for (int i = 0; i < cur.size(); i++)
    while (ord % cur[i].first == 0 && pow(cx, ord / cur[i].first) == 1)
      ord /= cur[i].first;
  ans += phi / ord;
}
void sol(long long cu, long long depth) {
  if (depth == fac.size()) {
    cal(cu);
    return;
  }
  for (int i = 0; i <= fac[depth].second; i++) {
    apow[depth] = i;
    sol(cu, depth + 1);
    if (i != fac[depth].second) cu *= fac[depth].first;
  }
}
void factorize(long long m, vector<pair<long long, long long> > &fac) {
  for (long long i = 2; i * i <= m; i++) {
    if (m % i == 0) {
      fac.push_back(make_pair(i, 0));
      while (m % i == 0) {
        m /= i;
        fac[fac.size() - 1].second += 1;
      }
    }
  }
  if (m > 1) fac.push_back(make_pair(m, 1));
}
int main() {
  read(m);
  read(x);
  factorize(m, fac);
  for (int i = 0; i < fac.size(); i++) factorize(fac[i].first - 1, facs[i]);
  sol(1, 0);
  cout << ans << endl;
  return 0;
}
