#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 5;
long long read() {
  long long s = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    s = s * 10 + c - '0';
    c = getchar();
  }
  return s * f;
}
long long n, X, tot, cnt1, cnt2, num[N], p1[N], p2[N];
long long Multi(long long x, long long y, long long mod) {
  return ((unsigned long long)x * y -
          (unsigned long long)((long double)x / mod * y) * mod + mod) %
         mod;
}
long long Pow(long long x, long long y, long long mod) {
  long long ans = 1;
  x %= mod;
  while (y) {
    if (y & 1) ans = Multi(ans, x, mod);
    x = Multi(x, x, mod);
    y >>= 1;
  }
  return ans;
}
void Divide(long long n) {
  for (long long i = 1; i * i <= n; i++)
    if (n % i == 0) {
      num[++tot] = i;
      if (i * i != n) num[++tot] = n / i;
    };
  sort(num + 1, num + tot + 1);
}
void GetPrime(long long n, long long p[], long long &tot) {
  for (long long i = 2; i * i <= n; i++)
    if (n % i == 0) {
      p[++tot] = i;
      while (n % i == 0) n /= i;
    }
  if (n != 1) p[++tot] = n;
}
long long Phi(long long n) {
  long long phi = n;
  for (int i = 1; i <= cnt1; i++)
    if (n % p1[i] == 0) phi = phi / p1[i] * (p1[i] - 1);
  return phi;
}
long long Solve(long long n) {
  long long phi = Phi(n), ans = 0;
  if (phi != 1) GetPrime(phi, p2, cnt2);
  for (int i = 1; i <= tot; i++) {
    long long F = Phi(num[i]), L = F;
    for (int j = 1; j <= cnt2; j++) {
      while (L % p2[j] == 0 && Pow(X, L / p2[j], num[i]) == 1) L /= p2[j];
    }
    ans += F / L;
  }
  return ans;
}
int main() {
  n = read();
  X = read();
  Divide(n);
  GetPrime(n, p1, cnt1);
  printf("%lld", Solve(n));
  return 0;
}
