Under construction

Still being built. Things may change

Halting Problem

Time limit3sMemory limit512 MB

Summary
Given N states that add b or d to an integer x depending on whether x equals a, find the step count to reach state N+1 from state 1, or print -1.
Level

Medium7 of 10

Topics
Simulation, Math, Hash map
Solved
No attempts yet

Problem

In the Republic of Finite Loop, a unique law is enforced. Under the law, programs that never halt are regarded as viruses. Releasing such a program is a cybercrime. So, you want to make sure that your software products always halt under their normal use.

It is widely known that there is no algorithm that can determine whether an arbitrary program halts for an arbitrary input. Fortunately, your products are based on a simple computation model given below. So, you can write a program that tells whether a given program based on the model will eventually halt for a given input.

The computation model has one variable xx and N+1N + 1 states, numbered 1 through N+1N + 1. The variable xx can store any integer value. State N+1N + 1 means that the program has terminated. For each integer ii (1≤i≤N1 \le i \le N), the behavior of the program in state ii is described by five integers aia_i, bib_i, cic_i, did_i, and eie_i. Here cic_i and eie_i are indices of states.

When the program starts, its state is set to 1, and xx is set to x0x_0, the input to the program. When the program is in state ii (1≤i≤N1 \le i \le N), one of the following happens in one execution step:

  • If xx is equal to aia_i, the value of xx changes to x+bix + b_i and the program state becomes cic_i.
  • Otherwise, the value of xx changes to x+dix + d_i and the program state becomes eie_i.

The program terminates when its state becomes N+1N + 1.

Write a program that determines whether a given program eventually halts for a given input. If it halts, compute how many steps it executes. The initialization is not counted as a step.

Input

The input consists of a single test case in the following format.

N x0
a1 b1 c1 d1 e1
.
.
.
aN bN cN dN eN

The first line contains two integers NN (1≤N≤1051 \le N \le 10^5) and x0x_0 (−1013≤x0≤1013-10^{13} \le x_0 \le 10^{13}). The program has N+1N + 1 states. Each of the next NN lines contains five integers aia_i, bib_i, cic_i, did_i, and eie_i that determine the behavior of the program in state ii. The integers aia_i, bib_i, and did_i are between −1013-10^{13} and 101310^{13} inclusive, and cic_i and eie_i are between 1 and N+1N + 1 inclusive.

Output

If the program eventually halts for the given input, output a single integer on a line: the number of steps executed until the program terminates. Since the number may be very large, output it modulo 109+710^9 + 7.

Output −1-1 if the program never halts.

Examples4

  1. Example 1

    Input
    2 0
    5 1 2 1 1
    10 1 3 2 2
    
    Expected output
    9
    
  2. Example 2

    Input
    3 1
    0 1 4 2 3
    1 0 1 1 3
    3 -2 2 1 4
    
    Expected output
    -1
    
  3. Example 3

    Input
    3 3
    1 -1 2 2 2
    1 1 1 -1 3
    1 1 4 -2 1
    
    Expected output
    -1
    
  4. Example 4

    Input
    13 3
    15 -10 4 4 2
    19 0 4 4 3
    23 -20 4 4 1
    6 1 2 1 5
    6 17 3 -1 6
    3 2 9 4 7
    7 0 9 4 8
    11 -5 9 4 6
    6 1 7 1 10
    6 5 8 -8 11
    1 2 11 1 12
    6 1 14 2 13
    6 -4 11 1 12
    
    Expected output
    26