import sys input = sys.stdin.readline A,B,C,D,K = map(int,input().split()) print((A*B+C-D)%K)