n = map(int, raw_input().split())
x = n[1]
n = n[0]
x = min(x, n-x)
tpl = x
tps = n - 2*x
l = x + n - tps
while tps != 0:
    l += tpl * 3  # tpl/tps * 3*tps -tps
    ntps = tpl % tps
    tpl = tps
    tps = ntps
print l