#include<cstdio>
using namespace std;

long gcd(long a,long b){
  if(b==0)
    return a;
  else
    return gcd(b,a%b);
}
int main(){
  long n,x;
  scanf("%ld %ld",&n,&x);
  printf("%ld\n",3*(n-gcd(n,x)));
}