#include <bits/stdc++.h>
using namespace std;
long long n, x;
long long ans, gd;
inline long long gcd(long long x, long long y) {
  if (y == 0) return x;
  if (x < y) swap(x, y);
  long long re = gcd(y, x % y);
  ans += 2 * (x - x % y) / re;
  return re;
}
int main() {
  scanf("%lld%lld", &n, &x);
  ans = n;
  gd = gcd(n - x, x);
  ans -= gd;
  printf("%lld\n", ans);
  return 0;
}
