Generators

Time limit1sMemory limit256 MB

Summary
Simulate each bounded generator to find its largest reachable value, then lower one value by the smallest loss that breaks divisibility by k.
Level

Medium4 of 10

Topics
Greedy, Simulation
Solved
No attempts yet

Problem

A linear congruential generator (LCG) starts from a non-negative integer x0x_0, called the seed, and produces an infinite sequence of integers xix_i with 0≤xi<c0 \le x_i < c:

xi+1=(a⋅xi+b) mod cx_{i+1} = (a \cdot x_i + b) \bmod c

Here aa, bb and cc are non-negative integers and 0≤x0<c0 \le x_0 < c.

You are given nn generators. The parameters of generator jj are x0(j)x_0^{(j)}, a(j)a^{(j)}, b(j)b^{(j)} and c(j)c^{(j)}, and it produces the sequence xi(j)x_i^{(j)}. Take one term from each of the nn sequences so that the sum of the taken terms is as large as possible and is not divisible by kk.

Formally, choose integers tj≥0t_j \ge 0 for 1≤j≤n1 \le j \le n that maximize s=∑j=1nxtj(j)s = \sum_{j=1}^{n} x_{t_j}^{(j)} subject to s mod k≠0s \bmod k \neq 0.

Input

The first line contains two integers nn and kk (1≤n≤1041 \le n \le 10^4, 1≤k≤1091 \le k \le 10^9).

Each of the next nn lines describes one generator with four integers x0(j)x_0^{(j)}, a(j)a^{(j)}, b(j)b^{(j)} and c(j)c^{(j)} (0≤a(j),b(j)≤10000 \le a^{(j)}, b^{(j)} \le 1000, 0≤x0(j)<c(j)≤10000 \le x_0^{(j)} < c^{(j)} \le 1000).

Output

If no choice of terms gives a sum that is not divisible by kk, print a single line containing -1.

Otherwise print the maximum sum ss on the first line, and on the second line the nn indices t1,t2,…,tnt_1, t_2, \dots, t_n separated by single spaces (0≤tj≤1090 \le t_j \le 10^9).

Several sets of indices can reach the same maximum sum, so print exactly the one this rule produces. Let mjm_j be the largest value that occurs in sequence jj, and let S=m1+m2+⋯+mnS = m_1 + m_2 + \dots + m_n.

  • If S mod k≠0S \bmod k \neq 0, then s=Ss = S and every generator takes the largest value mjm_j of its own sequence.
  • Otherwise, for each generator jj let pjp_j be the largest value vv occurring in sequence jj with (mj−v) mod k≠0(m_j - v) \bmod k \neq 0, and let dj=mj−pjd_j = m_j - p_j. A generator with no such vv is left out of this step. If no generator has such a vv, print -1. Otherwise let DD be the smallest djd_j and let qq be the smallest index jj with dj=Dd_j = D. Then s=S−Ds = S - D, generator qq takes the value pqp_q, and every other generator takes the largest value mjm_j of its own sequence.

For each generator, print the smallest index tjt_j at which the value it takes occurs in its sequence.

Notes

In the first example the first generator produces 1, 2, 3, 4, 5, 0, 1, 2, ... and the second generator produces 2, 3, 2, 3, 2, ....

In the second example the first generator produces 0, 2, 0, 2, 0, ... and the second generator produces 2, 4, 2, 4, 2, ....

Examples2

  1. Example 1

    Input
    2 3
    1 1 1 6
    2 4 0 5
    
    Expected output
    8
    4 1
    
  2. Example 2

    Input
    2 2
    0 7 2 8
    2 5 0 6
    
    Expected output
    -1