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