#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
ll n,x;
ll gcd(ll a,ll b)
{
	return a==0?b:gcd(b%a,a);
}
int main()
{
	scanf("%lld %lld",&n,&x);
	printf("%lld\n",3*(n-gcd(n,x)));
}