#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
ll n,k;
ll gcd(ll a,ll b){return (a%b==0)?b:gcd(b,a%b);}

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