#include <bits/stdc++.h>
#define ll long long
#define uint unsigned long long
#define ls x << 1
#define rs x << 1 | 1
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define X first
#define Y second
#define pcc pair<char, char>
#define vi vector<int>
#define vl vector<ll>
#define rep(i, x, y) for(int i = x; i <= y; i ++)
#define rrep(i, x, y) for(int i = x; i >= y; i --)
#define eps 1e-9
using namespace std;
inline int read()
{
	int x = 0, f = 1; char ch = getchar();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
ll dfs(ll a,ll b)
{
	if(a > b) swap(a, b);
	if(b % a == 0) return (a * ((b / a) * 2 - 1));
	ll res = get(a, b % a) + (a + a) * (b / a);
	return res;
}

ll n,x;
int main(void)
{
	scanf("%lld%lld", &n, &x);
	if(n == x * 2) printf("%lld\n", x * 3);
	else
	{
		ll ans = 0;
		if(x * 2 < n)
		{
			ans = x + (n - x) + x + x;
			ans += dfs(x, n - 2 * x);
		}
		else
		{
			ans = x + n - x;
			ans += dfs(x, n - x);
		}
		printf("%lld\n", ans);
	}
	return 0;
}
