a, b, c, d, m = map(int, input().strip().split(' ')) if b + d > m: ans = m - 1 else: ans = (b + d) % m print(ans)