#include <bits/stdc++.h>
int Read() {
  int x = 0;
  bool neg = false;
  char c = getchar();
  while (isdigit(c) == false) neg ^= c == '-', c = getchar();
  while (isdigit(c) == true) x = x * 10 + c - '0', c = getchar();
  return neg == true ? -x : x;
}
inline void Read(int &x) { x = Read(); }
inline long long Solve(long long a, long long b) {
  return a == 0 ? -b : (b / a) * (2 * a) + Solve(b % a, a);
}
int main() {
  long long n = Read(), x = Read(), a = x, b = n - x;
  if (a > b) std::swap(a, b);
  long long ans = n + Solve(a, b);
  printf("%lld\n", ans);
  return 0;
}
