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

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

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