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