#include <bits/stdc++.h>
using namespace std;
long f(long a, long b) { return a != b ? (2 * b + f(b, a - b)) : a; }
int main() {
  long N, X;
  cin >> N >> X;
  long sum = N;
  long a, b;
  a = max(N - X, X);
  b = min(N - X, X);
  while (b > 0) {
    sum += 2 * b * static_cast<long>(a / b);
    long tmp = a % b;
    if (tmp > b) {
      a = tmp;
    } else {
      a = b;
      b = tmp;
    }
  }
  sum -= 1;
  cout << sum << endl;
  return 0;
}
