#include<bits/stdc++.h>
#define LL long long
LL N=0ll,ans=0ll;
inline LL cxsb(LL n,LL x){
	if(n%x==0) return n*2-x;
	else ans=n/x*x*2+cxsb(x,n%x); return ans;
}  
int main(){
	LL n,x;
	std::cin>>n>>x;N=n;
	std::cout<<cxsb(n-x,x)+N<<"\n";
}