#include <bits/stdc++.h>
long long int g(long long int n, long long int m) {
  if (n == 0 || m == 0) return 0;
  if (n > m) return 2 * m * (n / m) + g(n % m, m);
  if (n < m) {
    if (m % n == 0)
      return m + 2 * n * (m / n) - n;
    else
      return m + 2 * n * (m / n) + m % n + g(n - m % n, m % n);
  }
  return 2 * n;
}
signed main(void) {
  long long int N, X;
  scanf("%lld", &N);
  scanf("%lld", &X);
  if (2 * X == N)
    printf("%lld\n", 3 * X);
  else if (2 * X < N)
    printf("%lld\n", N + X + g(N - 2 * X, X));
  else
    printf("%lld\n", X + 2 * (N - X) * (X / (N - X)) + X % (N - X) +
                         g(X - X % (N - X), X % (N - X)));
  return 0;
}
