#include <cstdio>  
typedef long long lint;  
  
lint n, x;  
  
lint g(lint x, lint y){ return y ? g(y, x%y) : x; }  
  
int main(){  
scanf("%lld %lld",&n,&x);  
printf("%lld\n", (n - g(n, x)) * 3);  
}  
