2797.Mike and Frog

Time Limit: 1s Memory Limit: 256MB

Mike has a frog and a flower. His frog is named Xaniar and his flower is named Abol. Initially(at time 0), height of Xaniar is h1 and height of Abol is h2. Each second, Mike waters Abol and Xaniar. 2797_1.png So, if height of Xaniar is h1 and height of Abol is h2, after one second height of Xaniar will become 2797_2.png and height of Abol will become 2797_3.png where x1,y1,x2 and y2 are some integer numbers and 2797_4.png denotes the remainder of a modulo b.Mike is a competitive programmer fan. He wants to know the minimum time it takes until height of Xania is a1 and height of Abol is a2.Mike has asked you for your help. Calculate the minimum time or say it will never happen.

Input Format(From the terminal/stdin)

Input The first line of input contains integer m (2 \le m \le 106).The second line of input contains integers h1 and a1 (0 \le h1,a1 \lt m).The third line of input contains integers x1 and y1 (0 \le x1,y1 \lt m).The fourth line of input contains integers h2 and a2 (0 \le h2,a2 \lt m).The fifth line of input contains integers x2 and y2 (0 \le x2,y2 \lt m).It is guaranteed that h1 \neq a1 and h2 \neq a2.

Output Format(To the terminal/stdout)

Output Print the minimum number of seconds until Xaniar reaches height a1 and Abol reaches height a2 or print -1 otherwise.

Sample Input 1

Copy
5
4 2
1 1
0 1
2 3
 \n
 · \n
 · \n
 · \n
 · \n

Sample Output 1

Copy
3
 \n

Sample Input 2

Copy
1023
1 2
1 0
1 2
1 1
    \n
 · \n
 · \n
 · \n
 · \n

Sample Output 2

Copy
-1
  \n

Hints

In the first sample, heights sequences are following:Xaniar: 2797_5.pngAbol: 2797_6.png

Submit

请先 登录

© 2025 FAQs