include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

struct fast_io {
        fast_io(){
                std::cin.tie(nullptr);
                std::ios::sync_with_stdio(false);
        };
} fio;

signed main(){
        cout<<fixed<<setprecision(10);

        int N, X;
        int a, b;
        int ans = 0;

        cin>>N>>X;

        a = X;
        b = N - X;

        ans += X;
        ans += N - X;

        while(true){
          if(a < b) swap(a, b);
          // cout<<"a = "<<a<<" b = "<<b<<" ans = "<<ans<<endl;
          ans += b;

            ans += 2 * (a/b - 1) * b;

          if(a%b == 0) {
            // ans += 2 * (a/b - 1) * b;
            cout<<ans<<endl;
            return 0;
          }
          ans += b;

          a = a % b;

        }

        return 0;
}
