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