#include <iostream>
using namespace std;
#define int long long

int n, x;

int length(int a, int b) {
	if (b == 0) return 0;
	return b * (a / b) + length(b, a % b);
}

signed main() {
	cin >> n >> x;
	cout << 3 * length(n - x, x) << endl;
	return 0;
}