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