#include<bits/stdc++.h>
using namespace std;
using LL = long long;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    LL N, X;
    cin >> N >> X;
    cout << (N - gcd(N, X)) * 3;
    return 0; 
}