#include<iostream>
using namespace std;
#define ll long long
ll n,x,ans;
ll calc(ll n,ll x)
{
	if(!x)return -n;
	ll d=n%x;
	return calc(x,d)+(n/x)*2*x;
}
int main()
{
	cin>>n>>x;ans=n;
	ans+=calc(n-x,x);
	cout<<ans<<endl;
	return 0;
}
