#include <bits/stdc++.h>
template <typename T>
inline T read() {
  register T x = 0;
  register short f = 1;
  register char c = getchar();
  for (; !isdigit(c); f = c == '-' ? -1 : 1, c = getchar())
    ;
  for (; isdigit(c); x = x * 10 + (c ^ 48), c = getchar())
    ;
  return x * f;
}
inline void swap(long long& a, long long& b) {
  a ^= b;
  b ^= a;
  a ^= b;
}
int main() {
  register long long n = read<long long>(), x = read<long long>(), ans = n,
                     a = (x > n - x ? x : n - x), b = n - a;
  while (b) ans += ((a / b) * b << 1) - !(a % b), a %= b, swap(a, b);
  printf("%lld\n", ans);
  return 0;
}
