#include<bits/stdc++.h>
using namespace std;
#define nn 65536*4
#define ll long long
int main()
{
	ll n,x;cin>>n>>x;
	ll a=x,b=n-x,ans=n;
	while(a&&b)
	{
		if(a<b) swap(a,b);
		ll t=a/b;
		ans+=t*b*2;
		a%=b;
	}
	cout<<ans-a-b;
	return 0;  
}