Codeforces Round 305 (Div. 2)
C
Mike and Frog
Codeforces
未评级
原题
← BD →
翻译中…

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 $*h*_{1}$ and height of Abol is $*h*_{2}$. Each second, Mike waters Abol and Xaniar.

翻译中…

image

翻译中…

So, if height of Xaniar is $*h*_{1}$ and height of Abol is $*h*_{2}$, after one second height of Xaniar will become image and height of Abol will become image where $*x*_{1}, *y*_{1}, *x*_{2}$ and $*y*_{2}$ are some integer numbers and image 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 $*a*_{1}$ and height of Abol is $*a*_{2}$.

翻译中…

Mike has asked you for your help. Calculate the minimum time or say it will never happen.

翻译中…

Input

翻译中…

The first line of input contains integer $*m*$ ($2 ≤ *m* ≤ 10^{6}$).

翻译中…

The second line of input contains integers $*h*_{1}$ and $*a*_{1}$ ($0 ≤ *h*_{1}, *a*_{1} < *m*$).

翻译中…

The third line of input contains integers $*x*_{1}$ and $*y*_{1}$ ($0 ≤ *x*_{1}, *y*_{1} < *m*$).

翻译中…

The fourth line of input contains integers $*h*_{2}$ and $*a*_{2}$ ($0 ≤ *h*_{2}, *a*_{2} < *m*$).

翻译中…

The fifth line of input contains integers $*x*_{2}$ and $*y*_{2}$ ($0 ≤ *x*_{2}, *y*_{2} < *m*$).

翻译中…

It is guaranteed that $*h*_{1} ≠ *a*_{1}$ and $*h*_{2} ≠ *a*_{2}$.

翻译中…

Output

翻译中…

Print the minimum number of seconds until Xaniar reaches height $*a*_{1}$ and Abol reaches height $*a*_{2}$ or print -1 otherwise.

翻译中…

Note

翻译中…

In the first sample, heights sequences are following:

翻译中…

Xaniar: image

翻译中…

Abol: image

翻译中…

样例

Input #1
5
4 2
1 1
0 1
2 3
Output #1
3
InputCopy OutputCopy
Input #2
1023
1 2
1 0
1 2
1 1
Output #2
-1
InputCopy OutputCopy
题目信息
题号
548C
时间限制
1 s
内存限制
256 MB

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。