#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,x;
ll get(ll x,ll y){
	ll a=min(x,y),b=max(x,y);
	if (a==0)return -b;
	return get(a,b%a)+2*a*(b/a);
}
signed main(){
	scanf("%lld%lld",&n,&x);
	printf("%lld\n",get(x,n-x)+n);
} 