#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N, X;
  cin >> N >> X;
  ll res = N;
  ll a = min(N - X, X), b = max(X, N - X);
  while (true) {
    if (b % a == 0) {
      res += a * 3;
      break;
    } else {
      res += 2 * a * (b / a);
      ll t = a;
      a = b %= a;
      b = t;
    }
  }
  cout << res << endl;
}
