CF547A.Mike and Frog

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

So, if height of Xaniar is _h_1 and height of Abol is _h_2, after one second height of Xaniar will become and height of Abol will become where _x_1, _y_1, _x_2 and _y_2 are some integer numbers and 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.

迈克有一只青蛙和一朵花。他的青蛙名叫扎尼亚尔(Xaniar),他的花名叫阿博尔(Abol)。初始时刻(时间 00),扎尼亚尔的高度为 h1h_1,阿博尔的高度为 h2h_2。每一秒,迈克都会给阿博尔和扎尼亚尔浇水。

因此,若当前扎尼亚尔的高度为 h1h_1、阿博尔的高度为 h2h_2,则经过一秒后,扎尼亚尔的高度将变为
,
阿博尔的高度将变为
,
其中 x1, y1, x2x_1,\,y_1,\,x_2 和 y2y_2 均为某些整数,且
表示 aa 对 bb 取模的余数。

迈克是一名竞程编程爱好者。他想知道:最少需要多少时间,才能使扎尼亚尔的高度恰好为 a1a_1,同时阿博尔的高度恰好为 a2a_2。

迈克向你寻求帮助。请计算出最小所需时间;若该状态永远无法达到,请说明这一点。

输入格式

The first line of input contains integer m (2 ≤ m ≤ 106).

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.

输入的第一行包含一个整数 mm(2≤m≤1062 \leq m \leq 10^6)。

输入的第二行包含两个整数 h1h_1 和 a1a_1(0≤h1,a1<m0 \leq h_1, a_1 < m)。

输入的第三行包含两个整数 x1x_1 和 y1y_1(0≤x1,y1<m0 \leq x_1, y_1 < m)。

输入的第四行包含两个整数 h2h_2 和 a2a_2(0≤h2,a2<m0 \leq h_2, a_2 < m)。

输入的第五行包含两个整数 x2x_2 和 y2y_2(0≤x2,y2<m0 \leq x_2, y_2 < m)。

保证 h1≠a1h_1 \neq a_1 且 h2≠a2h_2 \neq a_2。

输出格式

Print the minimum number of seconds until Xaniar reaches height _a_1 and Abol reaches height _a_2 or print -1 otherwise.

输出Xaniar到达高度 a1a_1 且Abol到达高度 a2a_2 所需的最少秒数;若无法实现,则输出 -1。

输入输出样例

  • 输入#1

    5
    4 2
    1 1
    0 1
    2 3

    输出#1

    3
  • 输入#2

    1023
    1 2
    1 0
    1 2
    1 1

    输出#2

    -1

说明/提示

In the first sample, heights sequences are following:

Xaniar:

Abol:

在第一个样例中,高度序列如下:

Xaniar:

Abol:

输入解题思路,AI测评打分。不知道怎么写?

首页