import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines N, S, X, Y, Z = map(int, readline().split()) N = 2 * 10**6 A = [0] * N A[0] = S&1 x = S for i in range(1, N): x = (x * X + Y) % Z A[i] = x&1 A = int(''.join(map(str, A)), 2)