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