#include <iostream>
#include <algorithm>

using namespace std;

int main(void)
{
	int N, X;
	cin >> N >> X;
	int ans = N;
	int a = max(N - X, X);
	int b = min(N - X, X);
	int q;
	while(b)
	{
		q = a % b;
		ans += (a - q) * 2;
		a = b;
		b = q;
	}
	ans -= a;
	cout << ans << endl;
	return 0;
