#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N, X;
  cin >> N >> X;
  long long A = X;
  long long B = N - X;
  if (A == B)
    cout << (A * 3) << "\n";
  else if (A < B) {
    cerr << (B) << "\n";
    long long kotae = N;
    while (true) {
      kotae += (B / A) * 2 * A;
      cerr << (kotae) << "\n";
      if (B % A == 0) {
        kotae -= A;
        cout << (kotae) << "\n";
        return 0;
      }
      B = B % A;
      swap(A, B);
    }
  } else {
    long long kotae = A - B + (A / B) * 2 * B;
    if (A % B == 0) {
      kotae -= B;
      cout << (kotae) << "\n";
      return 0;
    }
    A %= B;
    while (true) {
      kotae += (B / A) * 2 * A;
      if (B % A == 0) {
        kotae -= A;
        cout << (kotae) << "\n";
        return 0;
      }
      B = B % A;
      swap(A, B);
    }
  }
  return 0;
}
