#include<stdio.h>
long gcd2(long a, long b){
	long t;
	if(a<b){
		t = a;
		a = b;
		b = t;
	}
	r = a % b;
	while(r!=0){
		a = b;
		b = r;
		r = a % b;
	}
	return b;
}
int n, x;
int main(void){
  scanf("%ld%ld");
  printf("%ld\n", (n-*gcd2(n,x))*3);
}
