#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
  long long N, X;
  scanf("%lld%lld", &N, &X);
  if (N == 2 * X) {
    printf("%lld\n", 3 * X);
    return 0;
  }
  if (2 * X > N) X = N - X;
  long long A = N - 2 * X, B = X, ans = N + X;
  while (true) {
    if (A <= B) {
      ans += 3 * B;
      long long nB = B % A;
      long long nA = A - nB;
      if (B % A == 0) {
        ans -= A;
        break;
      }
      ans -= nB;
      B = nB;
      A = nA;
    } else {
      long long cnt = A / B;
      ans += 2 * cnt * B;
      A -= cnt * B;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
