1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <bits/stdc++.h>
using namespace std;

#define FOR(i,n) for(int i=0; i < int(n); ++i)
#define FO(i,a,b) for(int i=int(a); i<int(b); ++i)
#define OF(i,a,b) for(int i=int(b)-1; i>=int(a); --i)

typedef long long ll;
typedef pair<int,int> pii;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;

#define siz size()*1LL

#define fi first
#define se second

#define ASS assert

#define remin(a,b) a=min(a,b)
#define remax(a,b) a=max(a,b)

#define ALL(c) (c).begin(), (c).end()

#define NL '\n'

#define D //if(0)
//#define cerr if(0)cerr



inline int rint(){
	int i = 0;
	scanf("%d", &i);
	return i;
}



struct PKC{
	int p,k,c;
};




inline void merge(pii a[], int as, pii b[], int bs, pii c[], int& cs){
	cs = 0;
	int ia = 0;
	int ib = 0;
	
	while(ia < as && ib < bs){
		if(a[ia].fi < b[ib].fi) c[cs++] = a[ia++];
		else c[cs++] = b[ib++];
	}
	
	while(ia < as) c[cs++] = a[ia++];
	while(ib < bs) c[cs++] = b[ib++];
}

int main(){
	int n = rint();
	int m = rint();
	
	vector<PKC> d;
	
	FOR(i,n){
		int p = rint();
		int k = rint();
		int c = rint();
		d.push_back({p,k,c});
	}
	
	pii ma[2][109];
	int ma_size[2] = {0,0};
	
	pii temp[109];
	int temp_size = 0;
	
	int cma = 0;
	
	sort(ALL(d), [](PKC a, PKC b){if(a.p == b.p) return a.k - a.c > b.k - b.c; return a.p > b.p;});
		
	FOR(i,1000009){
		
		temp_size = 0;
		while(!d.empty() && d.back().p == i){
			auto t = d.back();
			d.pop_back();
			
			temp[temp_size++] = pii{t.k - t.c, t.c};
		}
		
		if(temp_size > 0){
			merge(ma[cma], ma_size[cma], temp, temp_size, ma[!cma], ma_size[!cma]);
			cma = !cma;
		}
		
		if(ma_size[cma] > 0 && ma[cma][0].fi < i){
			puts("NIE");
			return 0;
		}
		
		
		int zmiany = min(m,(int)ma_size[cma]);
		int w = 0;
		FOR(j, zmiany){
			++ma[cma][j].fi;
			if(w != j) ma[cma][w] = ma[cma][j];
			if(--ma[cma][j].se > 0){
				++w;
			}
		}
		
		if(ma[cma][w-1].fi > ma[cma][zmiany].fi){
			merge(ma[cma], w, ma[cma] + zmiany, ma_size[cma]-zmiany, ma[!cma], ma_size[!cma]);
			cma = !cma;
		}
		else if(w < zmiany){
			int diff = zmiany - w;
			FO(j, zmiany, ma_size[cma]) ma[cma][j-diff] = ma[cma][j];
			ma_size[cma] -= diff;
		}
	}
	
	puts("TAK");
	return 0;
}