#include<cstdio>
long long gcd(long long a,long long b){
  if(b%a==0)return a;
  else return gcd(b%a,a);
}
int main(void){
  long long n,x,g;
  scanf("%lld%lld",&n,&x);
  g=gcd(x,n);
  printf("%lld\n",(n-g)*3);
  return 0;
}
         
         