tmp = [int(x) for x in raw_input().split(' ')]
N = tmp[0]
X = tmp[1]

if(X > 0.5 * N):
	X = N-X

totaldist = N
step = X
dist = N-X

while(1):
	totaldist += (dist/step) * 2 * step
	distnew = step
	stepnew = dist % step
	if(stepnew == 0):
		totaldist -= step
		break
	dist = distnew
	step = stepnew

print totaldist