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
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>

#define PB          push_back
#define ST          first
#define ND          second

using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using VII = vector<PII>;
using LL = long long int;
using ULL = unsigned long long int;

// From: https://github.com/jaehyunp/stanfordacm/blob/master/code/PushRelabel.cc
struct Edge {
  int from, to, cap, flow, index;
  Edge(int from, int to, int cap, int flow, int index) :
    from(from), to(to), cap(cap), flow(flow), index(index) {}
};

struct PushRelabel {
  int N;
  vector<vector<Edge> > G;
  vector<LL> excess;
  vector<int> dist, active, count;
  queue<int> Q;

  PushRelabel(int N) : N(N), G(N), excess(N), dist(N), active(N), count(2*N) {}

  void AddEdge(int from, int to, int cap) {
    G[from].push_back(Edge(from, to, cap, 0, G[to].size()));
    if (from == to) G[from].back().index++;
    G[to].push_back(Edge(to, from, 0, 0, G[from].size() - 1));
  }

  void Enqueue(int v) { 
    if (!active[v] && excess[v] > 0) { active[v] = true; Q.push(v); } 
  }

  void Push(Edge &e) {
    int amt = int(min(excess[e.from], LL(e.cap - e.flow)));
    if (dist[e.from] <= dist[e.to] || amt == 0) return;
    e.flow += amt;
    G[e.to][e.index].flow -= amt;
    excess[e.to] += amt;    
    excess[e.from] -= amt;
    Enqueue(e.to);
  }
  
  void Gap(int k) {
    for (int v = 0; v < N; v++) {
      if (dist[v] < k) continue;
      count[dist[v]]--;
      dist[v] = max(dist[v], N+1);
      count[dist[v]]++;
      Enqueue(v);
    }
  }

  void Relabel(int v) {
    count[dist[v]]--;
    dist[v] = 2*N;
    for (int i = 0; i < G[v].size(); i++) 
      if (G[v][i].cap - G[v][i].flow > 0)
        dist[v] = min(dist[v], dist[G[v][i].to] + 1);
    count[dist[v]]++;
    Enqueue(v);
  }

  void Discharge(int v) {
    for (int i = 0; excess[v] > 0 && i < G[v].size(); i++) Push(G[v][i]);
    if (excess[v] > 0) {
      if (count[dist[v]] == 1) 
        Gap(dist[v]); 
      else
        Relabel(v);
    }
  }

  LL GetMaxFlow(int s, int t) {
    count[0] = N-1;
    count[N] = 1;
    dist[s] = N;
    active[s] = active[t] = true;
    for (int i = 0; i < G[s].size(); i++) {
      excess[s] += G[s][i].cap;
      Push(G[s][i]);
    }
    
    while (!Q.empty()) {
      int v = Q.front();
      Q.pop();
      active[v] = false;
      Discharge(v);
    }
    
    LL totflow = 0;
    for (int i = 0; i < G[s].size(); i++) totflow += G[s][i].flow;
    return totflow;
  }
};

const int MAXN = 300;

int n, m;

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

struct Point {
  int x, idx;
  bool beg;
};

bool operator<(const Point& p1, const Point& p2) {
  if (p1.x != p2.x) {
    return p1.x < p2.x;
  } else if (p1.beg != p2.beg) {
    return p1.beg < p2.beg;
  } else {
    return p1.idx < p2.idx;
  }
}

Prog prog[MAXN];
Point points[MAXN];
int block_sizes[MAXN];
int block_begins[MAXN];

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%d %d %d", &prog[i].p, &prog[i].k, &prog[i].c);
    points[2 * i].x = prog[i].p;
    points[2 * i].idx = i;
    points[2 * i].beg = true;
    points[2 * i + 1].x = prog[i].k;
    points[2 * i + 1].idx = i;
    points[2 * i + 1].beg = false;
  }
  sort(points, points + 2 * n);
  int prev = points[0].x;
  int blocks = 0;
  for (int i = 0; i < 2 * n; ++i) {
    if (points[i].x != prev) {
      block_sizes[blocks] = points[i].x - prev;
      block_begins[blocks] = prev;
      ++blocks;
      prev = points[i].x;
    }
  }
/*
cerr << blocks;
for (int i = 0; i < 2 * n; ++i) {
  cerr << points[i].x << " " << points[i].idx << " " << points[i].beg << endl;
}
*/
  PushRelabel pr(blocks + n + 2);
  LL total_c = 0;
  for (int i = 0; i < n; ++i) {
    pr.AddEdge(blocks + i + 1, blocks + n + 1, prog[i].c);
    total_c += prog[i].c;
  }
  for (int i = 0; i < blocks; ++i) {
    pr.AddEdge(0, i + 1, m * block_sizes[i]);
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < blocks; ++j) {
      if (block_begins[j] >= prog[i].p && block_begins[j] + block_sizes[j] <= prog[i].k) {
        pr.AddEdge(j + 1, blocks + i + 1, block_sizes[j]);
      }
    }
  }
  LL flow = pr.GetMaxFlow(0, blocks + n + 1);
  if (flow == total_c) {
    printf("TAK\n");
  } else {
    printf("NIE\n");
  }
  return 0;
}