logo AlgoBeat OnlineJudge
登录 注册

#206137. [NEERC 2015] Generators

内存限制:256 MiB 时间限制:1000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

Little Roman is studying linear congruential generators -- one of the oldest and best known pseudorandom number generator algorithms. Linear congruential generator (LCG) starts with a non-negative integer number also known as seed and produces an infinite sequence of non-negative integer numbers which are given by the following recurrence relation:

mod

here a , , and are non-negative integer numbers and .

Roman is curious about relations between sequences generated by different LCGs. In particular, he has different LCGs with parameters and for , where the j-th LCG is generating a sequence He wants to pick one number from each of the sequences generated by each LCG so that the sum of the numbers is the maximum one, but is not divisible by the given integer number .

Formally, Roman wants to find integer numbers for to maximize to constraint that mod .

输入格式

The first line of the input file contains two integer numbers and The following lines describe LCGs. Each line contains four integer numbers and .

输出格式

If Roman's problem has a solution, then write on the first line of the output file a single integer -- the maximum sum not divisible by , followed on the next line by integer numbers specifying some solution with this sum.

Otherwise, write to the output file a single line with the number .

样例

样例输入 1

2 3
1 1 1 6
2 4 0 5

样例输出 1

8
4 1

样例输入 2

2 2
0 7 2 8
2 5 0 6

样例输出 2

-1

数据范围与提示

Time limit: 1 s, Memory limit: 256 MB.