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