#include <bits/stdc++.h>
using namespace std;
long dist(long a, long b) {
  long m = min(a, b), M = max(a, b);
  if (m == 0) {
    return -1;
  } else {
    return 2 * (M / m) * m + dist(m, M % m);
  }
}
int main() {
  long N, X;
  cin >> N >> X;
  cout << N + dist(X, N - X) << endl;
}
