#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int N;
  long long int X, p, pl, c = 0, sum = 0;
  cin >> N >> X;
  long long int S, L;
  sum += N;
  if (X < N - X) {
    S = X;
    L = N - X;
  } else {
    S = N - X;
    L = X;
  }
  if (X == 1) {
    sum = 0;
    sum += (N - X) * 3;
    cout << sum << endl;
    return 0;
  }
  if (N - X == 1) {
    sum = 0;
    sum += X * 3;
    cout << sum << endl;
    return 0;
  }
  while (S != 1) {
    if (L % S == 0 && c % 2 == 0) {
      sum += 2 * ((L / S) * S) - S;
      break;
    } else if (L % S == 0) {
      sum += 2 * (L / S) * S;
      break;
    } else {
      sum += 2 * (L / S) * S;
    }
    p = L - S * (L / S);
    pl = L;
    L = S;
    S = p;
    c++;
  }
  if (S == 1 && c % 2 == 0) {
    sum += L * 2 - S;
  } else if (S == 1) {
    sum += L * 2 - S;
  }
  cout << sum << endl;
  return 0;
}
