#include <bits/stdc++.h>
using namespace std;
long f(long a, long b) {
  if (b < a) swap(a, b);
  if (a == 0) return 0;
  return 2 * a + f(a, b - a);
}
int main() {
  long N, X;
  cin >> N >> X;
  long ans = N;
  long a = N - X, b = X;
  cout << N + f(N - X, X) - 1 << endl;
}
