#include <bits/stdc++.h>
long long n, x, a, b, c, ans;
int main() {
  scanf("%lld%lld", &n, &x);
  if (x < n - x) {
    ans = n;
    a = n - x;
    b = x;
  } else {
    ans = x;
    a = x;
    b = n - x;
  }
  while (true) {
    if (a % b == 0) {
      ans += (2 * a / b - 1) * b;
      break;
    } else {
      ans += 2 * (a / b) * b;
      c = a % b;
      a = b;
      b = c;
    }
  }
  printf("%lld\n", ans);
}
