#include <bits/stdc++.h>
using namespace std;
static const double EPS = 1e-8;
static const double PI = 4.0 * atan(1.0);
static const double PI2 = 8.0 * atan(1.0);
int N, X;
int main(int argc, char **argv) {
  cin >> N >> X;
  int n1 = X, n2 = N - X, res = 0;
  while (n2 != 0) {
    res += (n1 / n2) * 2 * n2;
    swap(n1, n2);
    n2 %= n1;
  }
  res += N - n1;
  cout << res << endl;
  return 0;
}
