#include <bits/stdc++.h>
using namespace std;
long solve(long N, long X) {
  long ans = N;
  long nX = N - X;
  long big = max(X, nX);
  long small = min(X, nX);
  X = big;
  nX = small;
  while (X % nX) {
    ans += nX * (X / nX * 2);
    long temp = X % nX;
    X = nX;
    nX = temp;
  }
  return ans + X / nX * 2 - 1;
}
int main() {
  long N;
  long X;
  cin >> N >> X;
  long ans = solve(N, X);
  cout << ans << endl;
  return 0;
}
