Problem:
Score : 1200 points

Problem Statement

We have N irregular jigsaw pieces. Each piece is composed of three rectangular parts of width 1 and various heights joined together. More specifically:

The i-th piece is a part of height H, with another part of height A_i joined to the left, and yet another part of height B_i joined to the right, as shown below. Here, the bottom sides of the left and right parts are respectively at C_i and D_i units length above the bottom side of the center part.

Snuke is arranging these pieces on a square table of side 10^{100}. Here, the following conditions must be held:

All pieces must be put on the table.

The entire bottom side of the center part of each piece must touch the front side of the table.

The entire bottom side of the non-center parts of each piece must either touch the front side of the table, or touch the top side of a part of some other piece.

The pieces must not be rotated or flipped.

Determine whether such an arrangement is possible.

Constraints

1 \leq N \leq 100000

1 \leq H \leq 200

1 \leq A_i \leq H

1 \leq B_i \leq H

0 \leq C_i \leq H - A_i

0 \leq D_i \leq H - B_i

All input values are integers.

Input

Input is given from Standard Input in the following format:

N H
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
:
A_N B_N C_N D_N

Output

If it is possible to arrange the pieces under the conditions, print YES; if it is impossible, print NO.

Sample Input 1

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

Sample Output 1

YES

The figure below shows a possible arrangement.

Sample Input 2

4 2
1 1 0 1
1 1 0 1
1 1 0 1
1 1 0 1

Sample Output 2

NO

Sample Input 3

10 4
1 1 0 3
2 3 2 0
1 2 3 0
2 1 0 0
3 2 0 2
1 1 3 0
3 2 0 0
1 3 2 0
1 1 1 3
2 3 0 0

Sample Output 3

YES

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
#include <unordered_set>
#include <unordered_map>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;
#define two(X) (1<<(X))
#define twoL(X) (((int64)(1))<<(X))
#define contain(S,X) (((S)&two(X))!=0)
#define containL(S,X) (((S)&twoL(X))!=0)
const double pi=acos(-1.0);
const double eps=1e-11;
template<class T> inline void ckmin(T &a,T b){if(b<a) a=b;}
template<class T> inline void ckmax(T &a,T b){if(b>a) a=b;}
template<class T> inline T sqr(T x){return x*x;}
typedef pair<int,int> ipair;
#define SIZE(A) ((int)A.size())
#define LENGTH(A) ((int)A.length())
#define MP(A,B) make_pair(A,B)
#define PB(X) push_back(X)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,a) for(int i=0;i<(a);++i)
#define ALL(A) A.begin(),A.end()

const int maxnode=2000000+16;

vector<int> g[maxnode];
bool v[maxnode];
int d[maxnode];

void dfs(int p)
{
	if (v[p]) return;
	v[p]=true;
	for (int x:g[p]) dfs(x);
}
int main()
{
#ifdef _MSC_VER
	freopen("input.txt","r",stdin);
#endif
	std::ios::sync_with_stdio(false);
	int n,h;
	while (cin>>n>>h)
	{
		h+=2;
		vector<ipair> edges;
		REP(i,n)
		{
			int a,b,c,d;
			cin>>a>>b>>c>>d;
			int s=((c==0)?(a+h):c);
			int t=((d==0)?b:(d+h));
			edges.emplace_back(s,t);
		}
		bool ok=true;
		REP(i,h+h) g[i].clear();
		REP(i,h+h) v[i]=false;
		for (auto e:edges) g[e.first].push_back(e.second);
		FOR(i,h,h+h) dfs(i);
		for (auto e:edges) if (!v[e.first]) ok=false;

		REP(i,h+h) g[i].clear();
		REP(i,h+h) v[i]=false;
		for (auto e:edges) g[e.second].push_back(e.first);
		REP(i,h) dfs(i);
		for (auto e:edges) if (!v[e.second]) ok=false;

		REP(i,h+h) d[i]=0;
		for (auto e:edges) ++d[e.first],--d[e.second];
		REP(i,h) if (d[i]>0) ok=false;
		FOR(i,h,h+h) if (d[i]<0) ok=false;

		if (ok)
			printf("YES\n");
		else
			printf("NO\n");
		fflush(stdout);
#ifndef _MSC_VER
		break;
#endif
	}
	return 0;
}