
#define REP(i,n) for (int i=0;i<(n);i++)
#define ll long long

using namespace std;

int dx[] ={0,1,0,-1};
int dy[]= {1,0,-1,0};

int c=0;

long long solve(long long N,long long X){
  if(c==1){
  c++;
  if(2*N%X==0){
    return X*(2*N/X);
  }
  int ans = X*(2*N/X);
  return ans + solve(X,N%X);
  }else if(c%2==0){
  c++;
    if(N%X==0){
       return (N/X)*2*X-X;
    }
    int an = (N/X)*2*X;
    return an+solve(X,N%X); 
  }else if(c%2==1){
      c++;
  if(2*N%X==0){
    return X*(2*N/X)-X;
  }
  int ans = X*(2*N/X);
  return ans + solve(X,N%X);
}
}

int main(){
  long long N,X;
  cin>>N>>X;
  long long ans=X;
  c++;
  ans = ans + solve(X,N-X);
  c++;
  cout<<ans<<endl;
}