toys

After throwing a party at his house, Gigel and his kindergarten colleagues must move all the toys from the living room to his room. In order to get from the living room to Gigel's room, the children must pass through a hallway of length L.
Gigel has N friends (which he has numbered from 1 to N) and still has M toys to move. His friends have already begun work and are somewhere between the living room and Gigel's room. Some of them already have a toy to move, and the others are returning to the living room to pick up a new toy.
We will identify the hallway position of a child by the distance they are away from Gigel's room. More specifically, for each child i we will determine two values, di and ti, with meaning: di represents the distance child i is away from Gigel's room and ti=1, if child i is transporting a toy from the living room to Gigel's room, and respectively ti=0 if child i is returning from Gigel's room to the living room with no toy.
Each child takes a toy to Gigel's room, returns to the living room to pick up a new toy and repeats this process until all toys are moved.
Gigel analyzes the configuration of the N friends and notices that d1=S and t1=1 (meaning that the first child is at distance S from Gigel's room and is carrying a toy). For the other children (i=2, 3, ..., N) values di and ti can be determined using the following formulas (values X, Y, Z, V are known):
di = ( X * di-1 + Y * ( i - 1 ) ) % ( L - 1 ) + 1
ti = ( Z * di-1 + V * ( i - 1 ) ) % 2
By a % b we understand the remainder of a's division to b.

Task

Help Gigel determine the minimum amount of time in which all the toys are returned to his room.

Input Data

Line one of input file toys.in contains three positive integers: N, L and M, each separated by a space. The following line contains five positive integers, S,X,Y,Z,V, each separated by a space, with the meaning in the task.

Output Data

File toys.out will contain a single line with the minimum amount of time in which all the toys will be moved to Gigel's room.

Restrictions Example
toys.in toys.out Explanation
5 101 100
84 89 79 17 97
4124 There are 5 children, the length of the hallway is 101, and the number of toys is 100. The initial configuration of the 5 children is:
d1= 84 t1=1
d2=(89*84+79*1)%100+1=56 t2=(17*84+97*1)%2=1
d3=(89*56+79*2)%100+1=43 t3=(17*56+97*2)%2=0
d4=(89*43+79*3)%100+1=65 t4=(17*43+97*3)%2=0
d5=(89*65+79*4)%100+1=2  t5=(17*43+97*4)%2=1

  Time limit: 0.2 seconds/test

Diaconu Adrian Paul
University of Bucharest, Mathematics & IT Department
ditzone@gmail.com