#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using vll = vector<ll>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
ll n, x;
ll f(ll l, ll w) {
  if (w == 1) {
    return 2 * l - 1;
  }
  return 2 * (l / w) * w + f(w, l % w);
}
void func() {
  cin >> n >> x;
  cout << n + f(n - x, x) << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  ;
  cout.precision(32);
  ll t = 1;
  for (ll i = 1; i <= t; i++) {
    func();
  }
}
