#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n,x,ans;

ll gcd(ll a,ll b){
	if(b==0) return a;
	return gcd(b,a%b);
}

int main()
{
	cin>>n>>x;
	cout<<(n-gcd(n,x))*3<<endl;
	return 0;
}