# include <cstdio>
# include <iostream>
# define ll long long

using namespace std;

ll n,k;

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

int main()
{
    scanf("%lld%lld",&n,&k);
    printf("%lld",(n-gcd(n,k))*3);
}