#include <bits/stdc++.h>
#include <ext/algorithm>
#include <ext/numeric>

using namespace std;
using namespace __gnu_cxx;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

/**************************************************************/

	int N, X, length;
	cin >> N >> X;

	if(N%2 == 0 && X == N/2)	//equilateral triangle (side=X)
		length = 3*X;
	else//isosceles trapezoid (largest_base=N-X, legs=X, shortest_base=N-2X)
	{
		if(X > N/2)	//symmetrical solutions
			X -= N/2;

		length = X + N;	//X + (N-X) + X
		int tlb = N-X, tl = X;	//tlb = trapezoid_largest_base, tl = trapezoid_leg,
		int tsb = tlb -tl, rest = tsb % tl;	// tsb = trapezoid_shortest_base

		while(rest != 0)
		{
			length+=  2*tl*(tsb/tl) + tl+rest;
			tlb = tl; tl = rest;
			tsb = tlb - tl;	rest = tsb % tl;
		}
		length+= 2*tsb;
	}
	cout << length << endl;
}