#include<iostream>
#include<cstdio>
using namespace std;
long long n,k;
long long gcd(long long x,long long y)
{
	if(y==0LL)return x;
	return gcd(y,x%y); 
}
int main()
{
	scanf("%lld%lld",&n,&k);
	printf("%lld",3LL*(n-gcd(n,k)));
	return 0;
}