#include <bits/stdc++.h>
using namespace std;
int main() {
  int64_t N, x;
  cin >> N >> x;
  int64_t ans = (N - x);
  while (x >= 1LL) {
    ans += x * (2 * (N / x) - 1LL);
    int64_t t = N;
    N = x;
    x = t % x;
  }
  cout << ans << endl;
  return 0;
}
