#include<algorithm>
#include<iostream>
using namespace std;
long test(long nokori, long now, long sum){
        long count_node,tmp = 0;
        if(nokori % now != 0){
                count_node  = nokori / now;
                count_node = count_node * 2;
                sum = sum + now * count_node;
                tmp = nokori;
                nokori = now;
                now = tmp % now;
        }else if(nokori % now == 0){
                count_node = nokori / now;
                count_node = count_node * 2 - 1;
                count_node = count_node * now;
                sum = sum + count_node;
                now = 0;
        }
        if(now != 0){
                test(nokori, now, sum);
        }
        return sum;
}

int main(){
        long x,n;
        cin >> n >> x;
        long sum,now,nokori = 0;
        nokori = n - x;
        sum = n;
        now = x;
        sum = test(nokori, now ,sum);
        cout << sum << "\n";
        return 0;
}
~   