#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
ll n,m;
ll gcd(ll x,ll y){return !y?x:gcd(y,x%y);}
int main(){
	scanf("%lld%lld",&n,&m);printf("%lld\n",3ll*(n-gcd(n,m)));
}
