#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long int linf = 1LL << 50;
long long int calc(long long int x, long long int y) {
  if (x < y) swap(x, y);
  if (x % y == 0) return 2 * (x / y) - 1;
  return x + 3 * y + calc(y, x - y);
}
int main(int argc, char const* argv[]) {
  long long int n, x;
  cin >> n >> x;
  cout << calc(x, n - x) << endl;
  return 0;
}
