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