#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i = (a); i < (b); ++i)
#define REP(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int INF = 1e9;
const double PI = acos(-1.0);

ll func(ll a, ll b, ll sum) {
	ll n = b / a;
	ll ans;
	if(b % a == 0) return (n - 1) * a * 2 + a + sum;
	else ans = n * a * 2 + sum;
	
	return func(__gcd(a, b), a, ans);
}

int main(void) {
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	ll N, X;
	cin >> N >> X;
	
	ll ans = N;
	if(X < N - X) ans += func(X, N - X, 0);
	else ans += func(N - X, X, 0);
	
	cout << ans << "\n";
	
	return 0;
}

