#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;
ll n,x;
ll dfs(ll a,ll b) {
	if(b%a==0) return 2*b-a;
	return b/a*a*2+dfs(b%a,a);
}
int main() {
	cin>>n>>x;
	cout<<dfs(x,n-x)+n;
	return 0;
}