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