a, b, c, d, m = map(int, input().split()) lower = a + c upper = b + d if lower // m == upper // m: print(upper % m) else: print(m-1)