#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
const double eps = 1e-12;
template <typename T>
inline void read(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}
long long n, x;
long long ans = 0;
int Calc(int a, int b) {
  if (!a) return b;
  ans += b / a * (2 * a);
  return Calc(b % a, a);
}
signed main() {
  read(n);
  read(x);
  ans = n;
  long long a = x, b = n - x;
  ans -= Calc(a, b);
  printf("%lld\n", ans);
  return 0;
}
