/*
          ,     \    /      ,
         / \    )\__/(     / \
        /   \  (_\  /_)   /   \
   ____/_____\__\@  @/___/_____\____
  |             |\../|              |
  |              \VV/               |
  |        ------hoi-------         |
  |_________________________________|
   |    /\ /      \\       \ /\    |
   |  /   V        ))       V   \  |
   |/     `       //        '     \|
   `              V                '
*/

#include <bits/stdc++.h>

#define ull unsigned long long
#define ll long long
#define pb push_back
#define f first
#define se second
#define mp make_pair
#define Speedforce boost();

using namespace std;

void boost(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int N = 1e6;
const int inf = 1e9 + 7;

ll n, x, ans;

ll rec(ll a, ll b){
    if(b % a == 0){
        return 2 * b - a;
    }
    return rec(b % a, a) + b / a * a * 2
}

int main() {
    Speedforce
    cin >> n >> x;
    cout << rec(x, n);
}