args = [ int(x) for x in raw_input().split() ]
l0 = args[0]
x0 = args[1]

def get_length(l, x):
    if l % x == 0:
        part = l * 2 - x
        # print l, x, part
        return part
    part = int(l / x) * 2 * x
    # print l, x, part
    return part + get_length(x, l % x)

length = l0 + get_length(l0 - x0, x0)

print length
