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

int n, x;

int gcd(int a, int b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}

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