#include <bits/stdc++.h>

#define endl "\n"
using namespace std;
typedef long long int ll;


int main() {
    ios_base::sync_with_stdio(NULL);
    cin.tie(false);

/**************************************************************/

    ll N, X, length;
    //cin >> N >> X;



    if (N % 2 == 0 && X == N / 2)    //equilateral triangle (side=X)
        length = 3 * X;
    else//isosceles trapezoid (largest_base=N-X, legs=X, shortest_base=N-2X)
    {
        if (X > N / 2)    //symmetrical solutions
            X -= N / 2;

        length = X + N;    //X + (N-X) + X
        ll tlb = N - X, tl = X;    //tlb = trapezoid_largest_base, tl = trapezoid_leg,
        ll tsb = tlb - tl, rest = tsb % tl;    // tsb = trapezoid_shortest_base

        while (rest != 0) {
            length += 2 * tl * (tsb / tl) + tl + rest;
            tlb = tl;
            tl = rest;
            tsb = tlb - tl;
            rest = tsb % tl;
        }
        length += 2 * tsb;
    }
    cout << length << endl;
}