#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
const int mod = 1e9 + 7;
const long double pi = 3.141592653589793238462643383279502884197;
const long double eps = 1e-7;
ll N;
ll X;
ll path = 0;
ll lside, sside;
int main() {
  cin >> N >> X;
  if (X < N - X) {
    X = N - X;
  }
  lside = X;
  sside = N - X;
  path += N;
  while (sside != 0) {
    if (lside % sside == 0)
      path += (2 * (lside / sside) - 1) * sside;
    else
      path += (2 * (lside / sside)) * sside;
    int buf_sside = sside;
    sside = lside % sside;
    lside = buf_sside;
  }
  cout << path << endl;
  return 0;
}
