P6979 [NEERC 2015] Generators

Description

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 $x_{0}$ also known as seed and produces an infinite sequence of non-negative integer numbers $x_{i} (0 \le x_{i} < c)$ which are given by the following recurrence relation: $x_{i+1} = (ax_{i} + b)$ mod $c$ here a , $b$ , and $c$ are non-negative integer numbers and $0 \le x_{0} < c$ . Roman is curious about relations between sequences generated by different LCGs. In particular, he has $n$ different LCGs with parameters $a^{(j)}, b^{(j)},$ and $c^{(j)}$ for $1 \le j \le n$ , where the j-th LCG is generating a sequence $x_{i}^{(j)}.$ 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 $k$ . Formally, Roman wants to find integer numbers $t_{j} \ge 0$ for $1 \le j \le n$ to maximize $s = Σ^{n}_{j=1} x_{t_{j}}^{(j)}  subject$ to constraint that $s$ mod $k ≠ 0$ .

Input Format

The first line of the input file contains two integer numbers $n$ and $k (1 \le n \le 10 000 , 1 \le k \le 10^{9}).$ The following $n$ lines describe LCGs. Each line contains four integer numbers $x_{0}^{(j)}, a^{(j)}, b^{(j)},$ and $c^{(j)} (0 \le a^{(j)}, b^{(j)} \le 1000 , 0 \le x_{0}^{(j)} < c^{(j)} \le 1000)$ .

Output Format

If Roman's problem has a solution, then write on the first line of the output file a single integer $s$ -- the maximum sum not divisible by $k$ , followed on the next line by $n$ integer numbers $t_{j} (0 \le t_{j} \le 10^{9})$ specifying some solution with this sum. Otherwise, write to the output file a single line with the number $−1$ .

Explanation/Hint

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