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