def slab(base, side):
  if side == 0:
    return 0
  if base < side:
    #print(' + side -> base = %d' % (side))
    return side + slab(side, base)
  d = base % side
  #print('base %d, side %d, d %d' % (base, side, d))
  y = 2 * (base - d)
  if d == 0:
    y -= side
  #print(' + %d' % (y))
  if d != 0:
    y += slab(side, d)
  return y

n, x = map(int, raw_input().split())
#print(' + x = %d' % x)
result = x + slab(x, n - x)
print(result)