#include <bits/stdc++.h>

using namespace std;

int main () {	
	long long n, x;
	cin >> n >> x;
	long long ans = 0;
	while (x != 0) {
		ans += ((n - x) / x) * 3 * x;
		n = (n % x + x);
		x = n - x;
	}
	cout << ans;
	return 0;
}