
#include<cstdio>
#include<iostream>
using namespace std;
#define ll long long 
ll ans=0;
ll gcdgcd(ll x, ll n) {
    if(n % x == 0)
        return n * 2 - x;
    ans = n / x * x * 2 + gcdgcd(n % x, x);
    return ans;
}
int main() {
	ll n,x;
	cin>>n>>x;
	cout<<gcdgcd(x,n-x)+n;
}