k, t = [int(i) for i in input().split()] r = t % (2*k) if (r > k): r = 2*k - r print(r)