#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using vi = vector<ll>;
const int maxn = 5050, mod = 1e9 + 7;
ll solve(ll n, ll x) {
  if (x == 0 || x == n) return 0;
  ll l = n - x;
  ll cnt = x / l;
  ll ans = 3 * l * cnt;
  x %= l;
  if (x) ans += l + 2 * x;
  return ans + solve(n - x, (l - x) % l) - ((l - x) % l);
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  ll n, x;
  cin >> n >> x;
  cout << solve(n, x);
}
