#include<stdio.h>

long long gcd(long long a,long long b){
    while(b ^= a ^= b ^= a%=b);
    return a;
}

long long main(){
	long long n,x;
	long long i,j,g;

	scanf("%lld%lld",&n,&x);

	g = gcd(n,x);
	n /= g;
	x /= g;

	prlong longf("%lld\n",(n-1)*3*g);

	return 0;
}