#include <bits/stdc++.h>
int64_t sum, gcd;
void count(int n, int x) {
  if (x == 0) {
    gcd = n;
    return;
  } else
    sum += (n / x) * 2 * x;
  count(x, n % x);
}
int main() {
  int64_t n, x;
  scanf("%lld%lld", &n, &x);
  count(n - x, x);
  printf("%lld\n", sum + n - gcd);
  return 0;
}
