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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <bits/stdc++.h>
 
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define all(v) (v).begin(),(v).end()
 
#define PII pair<int,int>
#define mp make_pair
#define st first
#define nd second
#define pb push_back
#define lint long long int
#define VI vector<int>
 
#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } 
#define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } 
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }}
 
#define make( x) int (x); scanf("%d",&(x));
#define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y));
#define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z));
#define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t));
#define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} 
#define IOS ios_base::sync_with_stdio(0)
#define HEAP priority_queue
 
#define read( x) scanf("%d",&(x));
#define read2( x, y) scanf("%d%d",&(x),&(y));
#define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z));
#define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t));
#define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);}
 
 
using namespace std;


/* Usage:
   1) n=...; s=...; t=...;
   2) REP(i,n) g[i].clear();
   3) add_edge(...);
   4) compute_flow();
*/
#define max_n 1500

struct flow{
	struct Edge {
    lint v,cap,flow;
    lint back_ind;
    Edge *back;
    Edge(lint vi, lint ci):v(vi),cap(ci){}
	};
	

	int n,s,t;
	lint e[max_n],h[max_n];
	vector<Edge> g[max_n];
	vector<Edge>::iterator cur[max_n];
	void bfs(int start, int start_h) {
  	  queue<int> q;
    	h[start] = start_h;
    	for(q.push(start);!q.empty();q.pop()) {
      	  int u = q.front();
        	FORE(i,g[u])
          	  if (i->back->flow < i->back->cap && h[i->v]>h[u]+1) {
            	        h[i->v] = h[u] + 1;
              	      q.push(i->v);
            	}
    	}
	}

	

	lint compute_flow() {
    queue<int> q;
    FOR(i,0,n) {
        FORE(j,g[i]) {
            j->flow = 0;
            j->back = &g[j->v][j->back_ind];
        }
        cur[i] = g[i].begin();
        h[i] = e[i] = 0;
    }
    FORE(i,g[s]) {
        i->flow = i->cap;
        i->back->flow = -i->flow;
        if (e[i->v]==0 && i->v!=t) q.push(i->v);
        e[i->v] += i->flow;
    }
    h[s] = n;
    int relabel_counter = 0;
    for(;!q.empty();q.pop()) {
        int u = q.front();
        while (e[u]>0) {
            if (cur[u]==g[u].end()) { // relabel
                relabel_counter++;
                h[u] = 2*n+1;
                FORE(i,g[u]) if(i->flow < i->cap) h[u]=min(h[u],1+h[i->v]);
                cur[u] = g[u].begin(); 
                continue; 
            }
            if (cur[u]->flow < cur[u]->cap && h[u]==h[cur[u]->v]+1) { // push
                lint d = min(e[u], cur[u]->cap - cur[u]->flow);
                cur[u]->flow += d;
                cur[u]->back->flow -= d;
                e[u] -= d;
                e[cur[u]->v] += d;
                if (e[cur[u]->v]==d && cur[u]->v!=t && cur[u]->v!=s) q.push(cur[u]->v);
            } else cur[u]++; 
        }
        if (relabel_counter >= n) { 
            FOR(i,0,n) h[i]=2*n+1;
            bfs(t,0);
            bfs(s,n);
            relabel_counter = 0;
        }
    }
    return e[t];
	}
	void add_edge(int a, int b, lint c, int c_back=0) {
    	g[a].push_back(Edge(b,c));
    	g[b].push_back(Edge(a,c_back));
    	g[a].back().back_ind = g[b].size()-1;
    	g[b].back().back_ind = g[a].size()-1;
	}
};



int main() {	
	int n,m;
	vector<PII> ints;
	VI cost;
	read2(n, m);
	set<int> times;
	FOR(i,0,n) {
		make3(p, k, c);
		times.insert(p);
		times.insert(k);
		ints.pb(mp(p,k));
		cost.pb(c);
	}
	VI tms;
	FORE(i, times) tms.pb(*i);
	
	flow FLOW;
	FLOW.n = 2 + (tms.size() - 1) + n;
	FLOW.s = 0; FLOW.t = FLOW.n-1;
	FOR(i,0,FLOW.n) FLOW.g[i].clear();
	FOR(i,0,n) {
		FLOW.add_edge(0, i+1, cost[i]);
	}
	FOR(i,0,tms.size() - 1) {
		FLOW.add_edge(i + n + 1,FLOW.t, m * (tms[i+1]-tms[i]));
	}
	FOR(i,0,n) FOR(j,0,tms.size() - 1) {
		if (ints[i].st <= tms[j] && ints[i].nd >= tms[j+1]) {
			FLOW.add_edge(i+1, j+n+1, tms[j+1] - tms[j]);
		}
	}
	int my_flow = FLOW.compute_flow();
	int sum = 0;
	FOR(i,0,n) sum += cost[i];
	if (my_flow == sum) printf("TAK\n");
	else printf("NIE\n");
}