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