Under construction

Still being built. Things may change

Luck Manipulator

Interview

Time limit8sMemory limit512 MB

Summary
Simulate an LCG frame by frame, advancing a reel whenever the random number matches its target, and report the frame when all N reels stop or -1 after 10000 frames.
Level

Easy3 of 10

Topics
Simulation, Implementation, Math
Solved
No attempts yet

Problem

Nathan O. Davis is playing through a game and struggling to obtain a very rare item. This rare item can be obtained by lining up a special symbol on a casino slot machine. The slot machine has N reels, and pressing the button once stops the leftmost reel that is currently spinning. Therefore, obtaining this rare item requires pressing the button N times. To stop a reel on the special symbol, the button must be pressed at a certain timing. This timing is extremely strict, so Nathan was having trouble getting it right. He decided to use a memory viewer to analyze the game while checking memory values during play.

From his analysis, Nathan found that to stop a reel on the special symbol, the "random number" written at address 007E0D1F in memory must have a particular value at the moment the button is pressed. The random number changes every frame according to a linear congruential generator, and whether the button was pressed is also checked once per frame. The linear congruential generator is a method for producing pseudorandom numbers that determines values with the following formula.

x' = (A × x + B) mod C

Here x is the current random number, x' is the next random number, and A, B, C are constants. Also, y mod z denotes 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 "random number" is 10. Suppose the condition for stopping the reels on the special symbol is 2 for the left reel and 4 for the right reel. The random number in frame 1 is (5 × 10 + 7) mod 11 = 2, so pressing the button in frame 1 stops the left reel on the special symbol. The random number in frame 2 is (5 × 2 + 7) mod 11 = 6, so pressing the button in frame 2 does not stop the right reel on the special symbol. In frame 3 the random number is (5 × 6 + 7) mod 11 = 4, so pressing the button in frame 3 stops the right reel on the special symbol. Thus all reels can be stopped on the special symbol in 3 frames at minimum, and the rare item can be obtained.

Your job is to write a program that finds the earliest frame at which all reels can be stopped on the special symbol, helping Nathan obtain the rare item.

Input

The input consists of multiple data sets. Each data set is given in the following format.

N A B C X
Y1 Y2 ... YN

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 single spaces. X is the initial random number. The next line contains N integers Y1, Y2, ..., Y**N (0 ≤ Y**i ≤ 10,000), separated by single spaces. These represent the conditions for stopping the reels on a particular symbol: the i-th number Y**i means that stopping the i-th reel from the left on the special symbol requires the random number to be Y**i.

The end of the input is indicated by a line containing five zeros separated by spaces.

Output

For each data set, output the minimum number of frames needed to stop on the special symbol, on one line. If the reels cannot be stopped within 10,000 frames, output -1 instead of the frame count. Do not include extra spaces or newlines in the output.

Examples1

  1. Example 1

    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
    99 98
    2 1 1 100 0
    99 99
    2 1 1 10000 0
    1 0
    2 1 1 10000 0
    2 1
    0 0 0 0 0
    
    Expected output
    0
    3
    255
    -1
    198
    199
    10000
    -1