#include <iostream>

using namespace std;

long long gcd(long long a, long long b)
{
	return b == 0 ? a : gcd(b, a % b);
}

int main()
{
	long long N, X;
	cin >> N >> X;
	long long d = gcd(N, X);
	N /= d;
	X /= d;
	cout << 3 * (N - 1) * d << endl;
}
