if __file__.startswith('/tmp'):
	import sys; sys.stdin = open('input.txt')

n, x = map(int, raw_input().split())

def foo(a, b):
	if a == b: return a
	if a > b: return foo(b, a)
	if a == 0: return -b
	return 2 * a * (b / a) + foo(b % a, a)

print n + foo(x, n - x)