#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<(n);i++)

int para(int a,int b){
	int ret=0;
	if(a%b==0)return 2*a-b;
	else return para(a/b*b,b)+para(b,a%b);
}


signed main(){
	int n,x;cin>>n>>x;
	int ret=0
	ret=n+para(max(x,n-x),min(x,n-x));
	cout<<ret<<endl;
}
