#include <bits/stdc++.h>
using namespace std;
long long N, X;
long long getP(long long A, long long X) {
  long long ret = 0LL;
  while (A != 0) {
    ret += X + (X / A) * 2LL * A;
    long long B = X - (X / A) * A;
    if (B == 0) {
      ret -= A;
      break;
    }
    ret += B;
    X = B;
    A = A - B;
  }
  return ret;
}
int main() {
  cin >> N >> X;
  long long ret;
  if (N == 2LL * X) {
    ret = 3LL * X;
  } else if (N > 2LL * X) {
    ret = X + N;
    long long A = N - 2LL * X;
    ret += getP(A, X);
  } else {
    ret = X;
    long long A = N - X;
    ret += (N / A) * A * 2LL;
    long long B = N - (N / A) * A;
    if (B) {
      ret += A + B;
      ret += getP(A - B, B);
    }
  }
  cout << ret << endl;
}
