#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;

ll gcd(ll a, ll b){
    return a%b?gcd(b,a%b):b;
}

int main(){
    ll n, x; cin >> n >> x;
    cout << 3 * (n-gcd(n,x));
}

