import sys readline=sys.stdin.readline write=sys.stdout.write a,b,c,d,k=map(int,readline().split()) ans=(a*b+c-d)%k print(ans)