#include <bits/stdc++.h>
using namespace std;
long long N, X, a, b, t, r, ans;
int main() {
  cin >> N >> X;
  a = X, b = N - X;
  ans += a;
  bool f = false;
  while (1) {
    t = 2 * a / b;
    r = a % b;
    ans += b * t;
    if (f) ans -= b;
    if (r == 0) {
      break;
    }
    a = b;
    b = r;
    f = true;
  }
  cout << ans << endl;
  return 0;
}
