#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
  ll n,x;
  cin>>n>>x;
  ll ans=n,a=n-x;
  ll px=x;
  while(x%a) ans+=2*(a*(x/a)),px=x,x=a,a=px%a;
  ans+=2*(a*(x/a))-a;
  cout <<ans<<endl;
  return 0;
}
