Problem: Halting Problem

A unique law is enforced in the Republic of Finite Loop. 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 exists no algorithm that can determine whether an arbitrary given program halts or not for a given arbitrary input. Fortunately, your products are based on a simple computation model given below. So, you can write a program that can tell whether a given program based on the model will eventually halt for a given input.

The computation model for the products has only one variable $x$ and $N + 1$ states, numbered $1$ through $N + 1$. The variable $x$ can store any integer value. The state $N + 1$ means that the program has terminated. For each integer $i$ ($1 \leq i \leq N$), the behavior of the program in the state $i$ is described by five integers $a_i$, $b_i$, $c_i$, $d_i$ and $e_i$ ($c_i$ and $e_i$ are indices of states).

On start of a program, its state is initialized to $1$, and the value of $x$ is initialized by $x_0$, the input to the program. When the program is in the state $i$ ($1 \leq i \leq N$), either of the following takes place in one execution step:

* if $x$ is equal to $a_i$, the value of $x$ changes to $x + b_i$ and the program state becomes $c_i$;
* otherwise, the value of $x$ changes to $x + d_i$ and the program state becomes $e_i$.



The program terminates when the program state becomes $N + 1$.

Your task is to write a program to determine whether a given program eventually halts or not for a given input, and, if it halts, to compute how many steps are executed. The initialization is not counted as a step.

Input

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


$N$ $x_0$
$a_1$ $b_1$ $c_1$ $d_1$ $e_1$
.
.
.
$a_N$ $b_N$ $c_N$ $d_N$ $e_N$


The first line contains two integers $N$ ($1 \leq N \leq 10^5$) and $x_0$ ($−10^{13} \leq x_0 \leq 10^{13}$). The number of the states of the program is $N + 1$. $x_0$ is the initial value of the variable $x$. Each of the next $N$ lines contains five integers $a_i$, $b_i$, $c_i$, $d_i$ and $e_i$ that determine the behavior of the program when it is in the state $i$. $a_i$, $b_i$ and $d_i$ are integers between $−10^{13}$ and $10^{13}$, inclusive. $c_i$ and $e_i$ are integers between $1$ and $N + 1$, inclusive.

Output

If the given program eventually halts with the given input, output a single integer in a line which is the number of steps executed until the program terminates. Since the number may be very large, output the number modulo $10^9 + 7$.

Output $-1$ if the program will never halt.

Sample Input 1


2 0
5 1 2 1 1
10 1 3 2 2


Sample Output 1


9


Sample Input 2


3 1
0 1 4 2 3
1 0 1 1 3
3 -2 2 1 4


Sample Output 2


-1


Sample Input 3


3 3
1 -1 2 2 2
1 1 1 -1 3
1 1 4 -2 1


Sample Output 3


-1






Example

Input

2 0
5 1 2 1 1
10 1 3 2 2


Output

9

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
using lint = __int128;
using pi = pair<lint, lint>;
const int MAXN = 100005;
const int mod = 1e9 + 7;

int n;
// qry[i] : (where does it leads to, for what cost)
// nxt[i] : (where does it leads to, for what addition)
// val[i] : what HP to take qry[i]
// result[i] : (how long, where (as a portal) does it leads to)

pi qry[MAXN], nxt[MAXN];
vector<pi> revlist[MAXN];
pi result[MAXN];
lint val[MAXN];
bool visited[MAXN];

namespace FindEnd{
	vector<int> gph[MAXN];
	map<lint, int> mp;
	int dep[MAXN];
	void dfs(int x, lint depth){
		visited[x] = 1;
		int restore = -1;
		if(x <= n){
			if(mp.count(val[x] + depth)){
				restore = mp[val[x] + depth];
			}
			mp[val[x] + depth] = x;
		}
		for(auto &fuck : revlist[x]){
			auto i = fuck.first;
			auto j = fuck.second;
			if(mp.count(i + depth)){
				int vert = mp[i + depth];
				result[j] = pi(dep[x] - dep[vert], vert);
			}
			else{
				result[j] = pi(dep[x], n + 1);
			}
		}
		for(auto &i : gph[x]){
			dep[i] = dep[x] + 1;
			dfs(i, depth + nxt[i].second);
		}
		if(x <= n){
			if(~restore) mp[val[x] + depth] = restore;
			else mp.erase(mp.find(val[x] + depth));
		}
	}
	void solve(){
		for(int i=1; i<=n; i++){
			gph[nxt[i].first].push_back(i);
		}
		dfs(n + 1, 0);
	}
}

namespace MatchQuery{
	int indeg[MAXN], dep[MAXN];
	vector<int> gph[MAXN];
	map<lint, int> mp;
	void dfs(int x, lint depth, vector<pi> &collected){
		int restore = -1;
		if(mp.count(val[x] + depth)){
			restore = mp[val[x] + depth];
		}
		mp[val[x] + depth] = x;
		for(auto &fuck : revlist[x]){
			auto i = fuck.first;
			auto j = fuck.second;
			if(mp.count(i + depth)){
				int vert = mp[i + depth];
				result[j] = pi(dep[x] - dep[vert], vert);
			}
			else{
				collected.emplace_back(i + depth, j);
			}
		}
		for(auto &i : gph[x]){
			if(indeg[i]) continue;
			dep[i] = dep[x] + 1;
			dfs(i, depth + nxt[i].second, collected);
		}
		if(~restore) mp[val[x] + depth] = restore;
		else mp.erase(mp.find(val[x] + depth));
	}
	void solve(){
		vector<int> noe;
		for(int i=1; i<=n; i++){
			if(visited[i]) continue;
			noe.push_back(i);
		}
		for(auto &i : noe){
			indeg[nxt[i].first]++;
			gph[nxt[i].first].push_back(i);
		}
		queue<int> que;
		for(auto &i : noe){
			if(!indeg[i]){
				que.push(i);
			}
		}
		while(sz(que)){
			int x = que.front(); que.pop();
			indeg[nxt[x].first]--;
			if(indeg[nxt[x].first] == 0) que.push(nxt[x].first);
		}
		for(auto &i : noe){
			if(!indeg[i]) continue;
			vector<int> v = {i};
			vector<vector<pi>> w;
			lint T = nxt[i].second;
			for(int j = nxt[i].first; j != i; j= nxt[j].first){
				v.push_back(j);
				T += nxt[j].second;
			}
			for(auto &i : v){
				vector<pi> ww;
				dfs(i, 0, ww);
				w.push_back(ww);
			}
			if(T < 0){
				for(auto &j : v){
					nxt[j].second *= -1;
					val[j] *= -1;
				}
				for(auto &j : w){
					for(auto &k : j){
						k.first *= -1;
					}
				}
				T *= -1;
			}
			if(T == 0) T = 696969420420420420ll;
			auto D = [&](lint x){
				if(x >= 0) return x / T;
				return (x - T + 1) / T;
			};
			for(auto &i : v) indeg[i] = 0;
			vector<lint> psum(2 * sz(v) + 1);
			for(int i=0; i<sz(v); i++){
				psum[i + 1] = nxt[v[i]].second;
				psum[i + 1 +sz(v)] = nxt[v[i]].second;
			}
			for(int i=1; i<sz(psum); i++) psum[i] += psum[i-1];
			set<tuple<lint, lint, lint>> s;
			vector<tuple<lint, lint, lint>> lis(2 * sz(v));
			for(int i=0; i<2*sz(v); i++){
				lint key = val[v[i%sz(v)]] - psum[i];
				lis[i] = make_tuple(key - D(key) * T, key, (lint)i);
			}
			for(int i=0; i<sz(v); i++) s.insert(lis[i]);
			for(int i=0; i<sz(v); i++){
				for(auto fuck : w[i]){
					auto k = fuck.first;
					auto l = fuck.second;
					lint curk = k - psum[i];
					auto lbnd = s.lower_bound(make_tuple(curk - D(curk) * T, curk, (lint)-1));
					if(lbnd != s.end() && get<0>(*lbnd) == curk - D(curk) * T){
						lint nxtk = get<1>(*lbnd);
						int dist = get<2>(*lbnd) + dep[qry[l].first] - i;
						int vtx = v[get<2>(*lbnd) % sz(v)];
						lint ndist = dist + ((nxtk - curk) / T) * sz(v);
						result[l] = min(result[l], pi(ndist, vtx));
					}
				}
				s.erase(lis[i]);
				s.insert(lis[i + sz(v)]);
			}
		}
	}
}

int main(){
	long long ff;
	scanf("%d %lld",&n,&ff);
	qry[0].first = 1;
	qry[0].second = ff;
	for(int i=1; i<=n; i++){
		long long a, b, c, d, e;
		scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e);
		qry[i] = pi(c, a + b);
		nxt[i] = pi(e, d);
		val[i] = a;
	}
	nxt[n + 1] = pi(n + 1, n + 1);
	fill(result, result + n + 1, pi((lint)1e30, -1));
	for(int i=0; i<=n; i++){
		revlist[qry[i].first].emplace_back(qry[i].second, i);
	}
	FindEnd::solve();
	MatchQuery::solve();
	int pos = 0;
	lint ret = 0;
	vector<int> vis(n + 2);
	while(pos != n + 1 && !vis[pos]){
		vis[pos] = 1;
		ret += result[pos].first;
		ret %= mod;
		pos = result[pos].second;
		if(pos == -1) break;
		if(pos == n + 1) break;
		ret++;
		ret %= mod;
	}
	if(pos != n + 1) puts("-1");
	else cout << (int)(ret % mod) << endl;
}