#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
long long n,x;
long long gcd(long long a,long long b){return b?gcd(b,a%b):a;}
int main()
{
	cin>>n>>x;
	cout<<(n-gcd(n,x))*3<<endl;
}