n = map(int, raw_input().split())
x = n[1]
n = n[0]
tpl = x
tps = abs(n - 2*x)
# print '55',tps, tpl
if x < float(n)/2:
    l = x + n - tps
else:
    l = 0
while tps != 0:
    # print l
    # print "t", tps, tpl
    l += tpl * 3  # tpl/tps * 3*tps -tps
    ntps = tpl % tps
    tpl = tps
    tps = ntps
print l