#include <bits/stdc++.h>
using namespace std;

long long n, x, ans, prev, cur;

int main() {

	ios_base::sync_with_stdio(0);
	cin.tie(NULL);

	cin >> n >> x;

	x = min(x, n - x);

	ans += x;
	ans += n - x;

	prev = n - x;
	cur = x;

	while(cur > 0) {
		ans += 2 * (prev / cur) * cur;
		prev %= cur;
		swap(prev, cur);
	}

	cout << ans - prev << '\n';
	
	return 0;

}
