#include <iostream>
using namespace std;

long ref(long i, long j){
    if(i%j==0) return j*(2*i/j-1);
    else return 2*j*(i/j)+ref(j, i%j);
}

int main() {
	long N, X; cin >> N >> X;
	cout << N+ref(X, N-X) << endl;
	return 0;
}
