#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
using vi = vector<int>;
ll n, x;
ll solve(ll n, ll x) {
  if (x * 2 == n) return 2 * x;
  ll t = n - x;
  ll s = x / t;
  if (s == 0)
    return solve(n - x, n - 2 * x) + n;
  else
    return solve(n - s * t, x - s * t) + 2 * s * t;
}
int main() {
  cin >> n >> x;
  cout << solve(n, x) + x << endl;
}
