#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,x;
inline ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    cin>>n>>x;
    cout<<3*(n-gcd(n,x));
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,x;
inline ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    cin>>n>>x;
    cout<<3*(n-gcd(n,x));
    return 0;
}
Submission