#include <bits/stdc++.h>
using namespace std;
long long N, X;
long long f(long long a, long long b) {
  if (a <= 0 || b <= 0) return 0;
  if (a % b == 0) return ((a + b) / b - 1) * 2 * b;
  long long c = (a + b) % b;
  return (a + b) / b * b * 2 + (b - 1) / c * c + f(c - (b % c), b % c);
}
int main() {
  scanf("%lld %lld", &N, &X);
  if (X > N - X)
    printf("%lld", N + X + X / (N - X) * (N - X) +
                       f((N - X) - X % (N - X), X % (N - X)));
  else
    printf("%lld\n", N + X + f(N - 2 * X, X));
  return 0;
}
