#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, X;
  cin >> N >> X;
  ll ans = 0;
  ans += X;
  ll d = N - X;
  bool flag = true;
  while (d > 0) {
    ll cnt = N / d;
    if (flag)
      ans += 2 * d * cnt - d;
    else
      ans += 2 * d * cnt;
    ll tmp = d;
    d = N % d;
    N = tmp;
    flag ^= 1;
  }
  cout << ans << endl;
  return 0;
}
