#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;
LL gcd(LL a,LL b){
	return (b==0) ? a : gcd(b,a%b);
}

int main(){
	LL n,x;
	cin>>n>>x;
	LL d=gcd(n,x);
	cout<<d*(n/d*3-3)<<endl;
	return 0;
}