#include <bits/stdc++.h>
using namespace std;
long long calc(long long a, long long b) {
  if (a < b) swap(a, b);
  if (a == b) return a;
  return 2 * b + calc(b, a - b);
}
signed main() {
  long long N, X;
  cin >> N >> X;
  cout << N + calc(X, N - X) << endl;
}
