Problem Name: Problem A: Luck Manipulator
Problem Description: Nathan O. Davis is playing a game and struggling to get a very rare item. This rare item can be obtained by aligning special symbols on the slot machine of the casino. The slot machine has N reels, and pressing the button once stops the leftmost reel currently rotating. Therefore, to obtain this rare item, it is necessary to press the button N times. Also, in order to stop it with a special symbol, it is necessary to press the button at a certain timing. However, because the timing to press this button is very severe, it was difficult to succeed. Therefore, Nathan decided to use a memory viewer to analyze the game while checking the memory value during the game.
As a result of Nathan's analysis, he discovered that in order to stop the special symbol and stop the reel, it is necessary for the "random number" written to memory address 007E0D1F when the button is pressed to be a certain value. The value of the random number changes every 1 frame by the linear congruential method, and it was also found that the judgment of whether the button was pressed is performed once every 1 frame. Here, the linear congruential method is one of the methods for generating pseudo-random numbers, and the value is determined by the following equation.
x'
= (
A
×
x
+
B
) mod
C
Here,
x
is the current value of the random number,
x'
is the next value of the random number, and
A
,
B
,
C
are some constants. Also,
y
mod
z
represents the remainder when
y
is divided by
z
.
For example, suppose a slot machine with 2 reels has
A
= 5,
B
= 7,
C
= 11, and the initial value of the "random number" is 10. Then, suppose that the condition for stopping the reel with a special symbol is that the left reel is 2 and the right reel is 4. At this time, since the value of the random number in the first frame is (5 × 10 + 7) mod 11 = 2, the left reel will stop with a special symbol if the button is pressed in the first frame. At the second frame, the value of the random number is (5 × 2 + 7) mod 11 = 6, so pressing the button in the second frame will not stop the right reel with a special symbol. In the next third frame, the value of the random number is 4 because (5 × 6 + 7) mod 11 = 4, so if you press the button in the third frame, the right reel will stop with a special symbol. Therefore, all reels can be stopped with special symbols in the shortest 3 frames, and rare items can be obtained.
Your job is to write a program that finds the shortest number of frames required to stop all reels with special symbols and help Nathan get the rare item.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
N
A
B
C
X
Y
1
Y
2
...
Y
N
The first line contains five integers
N
(1 ≤
N
≤ 100),
A
,
B
(0 ≤
A
,
B
≤ 10,000),
C
(1 ≤
C
≤ 10,000), and
X
(0 ≤
X
<
C
) separated by a single space. Here,
X
represents the initial value of the random number. In the following line,
N
integers
Y
1
,
Y
2
, ...,
Y
N
(0 ≤
Y
i
≤ 10,000) are given separated by a single space. These represent the conditions for stopping the reel with a specific symbol, and the
i
-th number
Y
i
means that in order to stop the
i
-th reel with a special symbol, the value of the random number must be
Y
i
from the left.
The end of the input is indicated by a line containing five 0s separated by a single space.
Output
For each dataset, output the minimum number of frames required to stop all reels with special symbols on one line. If it is not possible to stop within 10,000 frames, output -1 instead of the number of frames. Do not include extra spaces or line breaks in the output.
Sample Input
1 5 7 11 10
10
2 5 7 11 10
2 4
2 1 1 256 0
128 255
2 0 0 1 0
1234 5678
2 1 1 100 0