#include <bits/stdc++.h>
using namespace std;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }
int main() {
  long long N, X;
  cin >> N >> X;
  long long res = N;
  long long a = max(N - X, X), b = min(N - X, X);
  while (b) {
    long long r = a % b;
    res += (b * 2);
    if (r == 0) res -= b;
    a = b, b = r;
  }
  cout << res << endl;
}
