#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef long double LD;
typedef pair<LD, LD> PLDLD;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define CLR(a) memset((a), 0 ,sizeof(a))
#define ALL(a) a.begin(),a.end()
LL MOD = 1e9+7;
LL INF = 1e9;
double EPS=1.0e-9;

int main()
{
    int n,x;
    cin>>n>>x;
    
    LL sum=0;
    
    if(x<n-x)
    {
        x=n-x;
    }
    int t=__gcd(n,x);
    n/=t;x/=t;
    
    sum+=n;
    int tmp=n;
    n=x;
    x=tmp-x;
    while(x>1)
    {
        sum+=(n/x)*2*x;
        
        tmp=n;
        n=x;
        x=tmp-x;
    }
    sum+=(n/x)*2*x;
    cout<<(sum-1)*t<<endl;
}