#include <iostream>
using namespace std;
typedef long long ll;

ll N,X;

ll gcd(ll n, ll m){
	if(n%m==0) return m;
	else return gcd(m,n%m);
}

int main(){
	cin >> N >> X;
	cout << 3*(N-gcd(N,X)) << endl;
}