#include <bits/stdc++.h>
using namespace std;
long long solve(long long& a, long long& b) {
  long long ret = 0;
  if (a == b) {
    ret = a;
    a = 0;
    b = 0;
    return ret;
  }
  if (b > a) {
    if (b % a == 0) {
      ret = (b / a) * 2LL * a - a;
      a = 0;
      b = 0;
      return ret;
    } else {
      ret = (b / a) * 2LL * a;
      b = b % a;
      return ret;
    }
  } else if (b < a) {
    if (a % b == 0) {
      ret = (a / b) * 2LL * b - b;
      a = 0;
      b = 0;
      return ret;
    } else {
      ret = (a / b) * 2LL * b;
      a = a % b;
      return ret;
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  long long N, X;
  cin >> N >> X;
  if (N % 2 == 0) {
    long long ret = X + (N - X);
    long long a = X, b = N - X;
    while (true) {
      long long tmp = solve(a, b);
      if (tmp == 0) break;
      ret += tmp;
    }
    cout << ret << endl;
  } else {
    cout << 3 * (N - 1) << endl;
  }
  return 0;
}
