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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <bits/stdc++.h>

#define scanf(arg...) (scanf(arg)?:0)
#define FOR(x, b, e) for(int x = b; x <= (e); ++x)
#define FORD(x, b, e) for(int x = b; x >= (e); --x)
#define REP(x, b) for(int x = 0; x < (b); ++x)
#define SIZE(c) ((int)c.size())
#define ALL(c) (c).begin(), (c).end()
#define PB push_back
#define MP make_pair
#define ST first
#define ND second

using namespace std;

typedef long double LD;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector <int> VI;
typedef pair <int, int> PII;
typedef vector <PII> VPII;

namespace Dinic
{

const int MAXN = 5e3 + 9;
const long long INF2 = 1e9 + 33;
const long long INF = INF2 * INF2;

vector <int> tab[MAXN], G[MAXN];
vector < pair<int, long long> > kra;
int odw[MAXN];
vector < pair<int, int> > vec;
queue <int> Q;
int dis[MAXN];
long long mini = INF;
long long wyn = 0;
int ile = 1, kon;

void bfs(int poz, int n)
{
	Q.push(poz);
	
	for(int i = 1; i <= n; i++)
	  {
		  dis[i] = INF2;
	  }
	
	dis[poz] = 0;
	
	while(!Q.empty())
	  {
		  poz = Q.front();
		  Q.pop();
		  
		  for(int i = 0; i < int(G[poz].size()); i++)
		    {
				if(kra[G[poz][i]].second != 0)
				  {
					  if(dis[poz] + 1 < dis[kra[G[poz][i]].first])
					    {
							Q.push(kra[G[poz][i]].first);
							dis[kra[G[poz][i]].first] = dis[poz] + 1;
						}
					  
					  if(dis[kra[G[poz][i]].first] == dis[poz] + 1)
					    tab[poz].push_back(G[poz][i]);
				  }
			}
	  }
}

bool dfs(int poz)
{
	odw[poz] = ile;
	
	if(poz == kon)
	  return true;
	
	for(int i = 0; i < int(tab[poz].size()); i++)
	  {
		  if(odw[kra[tab[poz][i]].first] < ile)
		    {
			    if(dfs(kra[tab[poz][i]].first))
			      {
					  vec.push_back(make_pair(poz, i));
					  mini = min(mini, kra[tab[poz][i]].second);
					  return true;
				  }
				
				swap(tab[poz][i], tab[poz].back());
				tab[poz].pop_back();
				
				i--;
			}
	  }
	
	return false;
}

bool block(int n, int s)
{
	bool czy = false;
	
	ile++;
	bfs(s, n);
	
	ile++;
	vec.clear();
	mini = INF;
	
	while(dfs(s))
	  {
		  ile++;
		  wyn += mini;
		  czy = true;
		  
		  for(int i = 0; i < int(vec.size()); i++)
		    {
				int nr = tab[vec[i].first][vec[i].second];
				
				kra[nr].second -= mini;
				kra[nr ^ 1].second += mini;
				
				if(kra[nr].second == 0)
				  {
					  swap(tab[vec[i].first][vec[i].second], tab[vec[i].first].back());
					  tab[vec[i].first].pop_back();
				  }
			}
		  
		  vec.clear();
		  mini = INF;
	  }
	
	return czy;
}

//od 1 do n
long long Dinic(int n, int s, int t)
{
	kon = t;
	
	while(block(n, s));
	
	return wyn;
}

void add_edge(int a, int b, long long c)
{
    kra.push_back(make_pair(b, c));
    kra.push_back(make_pair(a, 0));
    
    G[a].push_back(int(kra.size()) - 2);
    G[b].push_back(int(kra.size()) - 1);
}

}

const int KONIEC = 1e6 + 100;
const int MAXN = 109;

int a[MAXN];
int b[MAXN];
int c[MAXN];
VPII eventy[KONIEC + 10];
set <int> seti;
int liczba = 1;
LL m;

void dodaj(int x, int y)
{
    LL dl = y - x + 1;
    
    for(set<int>::iterator it = seti.begin(); it != seti.end(); ++it)
      Dinic::add_edge(liczba, *it, dl);
    
    Dinic::add_edge(1, liczba, dl * m);
    liczba++;
}

int main()
{
    int n;
    LL suma = 0;
    
    cin>>n>>m;
    
    liczba += n;
    liczba++;
    
    REP(i, n)
      {
          scanf("%d %d %d", &a[i], &b[i], &c[i]);
          
          a[i]++;
          b[i]++;
          
          suma += c[i];
          
          eventy[a[i]].PB(MP(1, i + 2));
          eventy[b[i]].PB(MP(-1, i + 2));
      }
    
    int pop = 0;
    
    FOR(i, 1, KONIEC)
      {
          if(SIZE(eventy[i]) != 0)
            {
                dodaj(pop, i - 1);
                pop = i;
            }
          
          REP(j, SIZE(eventy[i]))
            {
                if(eventy[i][j].ST == 1)
                  seti.insert(eventy[i][j].ND);
                
                else
                  seti.erase(eventy[i][j].ND);
            }
      }
    
    REP(i, n)
      Dinic::add_edge(i + 2, liczba, c[i]);
    
    if(Dinic::Dinic(liczba, 1, liczba) == suma)
      puts("TAK");
    
    else
      puts("NIE");
    
    return 0;
}