#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;
  ans += N - X;
  ll cnt = (N - X) / X;
  ans += 2 * X * cnt;
  ll d = (N - X) % X;
  while (d != 0) {
    ans += 2 * d * (X / d) - d;
    ll tmp = d;
    d = X % d;
    X = d;
  }
  cout << ans << endl;
  return 0;
}
