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
#include <cstdio>
//#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define debug 0


// Na podstawie "Algorytmiki Praktycznej" Piotra Stanczyka
typedef vector<int> VI;
const int INF = 1000000001;
#define VAR(v,n)  __typeof(n) v=(n)
#define REP(x, n) for(int x=0; x<(n); ++x)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define SIZE(x) (int)x.size()
#define ITER typename vector<Ed>::iterator
#define PB push_back
template <class V, class E> struct Graph {
  struct Ed : E {
    int v;
    Ed(E p, int w) : E(p), v(w) { }
  };
  struct Ve : V, vector<Ed> { };
  vector<Ve> g;
  Graph(int n = 0) : g(n) { }
  void EdgeD(int b, int e, E d = E()) {
    g[b].PB(Ed(d, e));
  }
  void EdgeU(int b, int e, E d = E()) {
    Ed eg(d, e);
    eg.rev = SIZE(g[e]) + (b == e);
    g[b].PB(eg);
    eg.rev = SIZE(g[eg.v = b]) - 1;
    eg.c = 0;
    g[e].PB(eg);
  }
  int out;
  vector<ITER> itL;
  VI vis;

  int FlowDfs(int x, int fl) {
    int r = 0, f;
    if (x == out || !fl) return fl;
    for (ITER & it = itL[x]; it != g[x].end(); ++it) {
      if (vis[x] + 1 == vis[it->v] && (it->c - it->f)) {
//        it->f += f = FlowDfs(it->v, fl <? it->c - it->f);  // TODO
        f = FlowDfs(it->v, min(fl,it->c - it->f));
        if (debug)
          printf("v = %d, rev = %d, f: %d %d, dif = %d\n", it->v, it->rev, it->f, g[it->v][it->rev].f, f);
        it->f += f;
        g[it->v][it->rev].f -= f;
        r += f;
        fl -= f;
        if (!fl) break; 
      }
    }
    return r;
  }

  int MaxFlow(int s, int f) {
    int res = 0, n = SIZE(g); vis.resize(n); itL.resize(n);
    out = f;
    REP(x, n) FOREACH(it, g[x]) it->f = 0; 
    int q[n], b, e;
    while (1) {
      REP(x, n) vis[x] = -1, itL[x] = g[x].begin();
      for (q[vis[s] = b = e = 0] = s; b <= e; ++b)
        FOREACH(it, g[q[b]]) if (vis[it->v] == -1 && it->c - it->f > 0)
          vis[q[++e] = it->v] = vis[q[b]] + 1;
        if (vis[f] == -1) break;
          res += FlowDfs(s, INF); 
    }
    return res;
  }
};

struct Ve {
  int rev, c, f;
};

struct Vs {
  int t;
};


/*
int main() {
  int n, m, s, f, b, e;
  cin >> n >> m >> s >> f;
  Graph<Vs, Ve> g(n);
  Ve l;
  REP(x, m) {
    cin >> b >> e >> l.c;
    g.EdgeU(b, e, l);
  }
  cout << "Wielkosc calkowitego przeplywu: " << g.MaxFlow(s, f) << endl;
  cout << "Wielkosc przeplywu dla kolejnych krawedzi:" << endl;
  REP(x, SIZE(g.g)) FOREACH(it, g.g[x]) if (it->f > 0)
    cout << "f(" << x << ", " << it->v << ") = " << it->f << endl;
  return 0;
}
*/


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

task t[100];
int n, m;
int x[200];
int stamp[200], s;  // number of timestamps

int main() {
  scanf("%d %d", &n, &m);
  int sum_c = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d %d %d", &t[i].p, &t[i].k, &t[i].c);
    sum_c += t[i].c;
    x[2*i] = t[i].p;
    x[2*i+1] = t[i].k;
  }
  sort(x, x+2*n);
  s = 0;
  stamp[s++] = x[0];
  for (int i = 1; i < 2*n; ++i) {
    if (x[i] != x[i-1])
      stamp[s++] = x[i];
  }
  if (debug) {
    printf("stamps\n");
    for (int i = 0; i < s; ++i)
      printf("%d ", stamp[i]);
    printf("\n");
  }
  int new_n = 1 + n + s;
  Graph<Vs, Ve> g(new_n);
  Ve l;
  for (int i = 0; i < n; ++i) {
    l.c = t[i].c;
    if (debug >= 2)
      printf("add %d %d (%d)\n", 0, 1+i, l.c);
    g.EdgeU(0, 1+i, l);
//    l.c = 0;
//    g.EdgeD(1+i, 0, l);
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < s-1; ++j) {
      if (t[i].p <= stamp[j] && t[i].k >= stamp[j+1]) {
        l.c = (stamp[j+1]-stamp[j]);
        if (debug >= 2)
          printf("add %d %d (%d)\n", 1+i, 1+n+j, l.c);
        g.EdgeU(1 + i, 1 + n + j, l);
//        l.c = 0;
//        g.EdgeD(1 + n + j, 1 + i, l);
      }
    }
  }
  for (int i = 0; i < s-1; ++i) {
    l.c = m * (stamp[i+1]-stamp[i]);
    if (debug >= 2)
      printf("add %d %d (%d)\n", 1+n+i, n+s, l.c);
    g.EdgeU(1 + n + i, n+s, l);
//    l.c = 0;
//    g.EdgeD(n+s, 1 + n + i, l);
  }
  int M = g.MaxFlow(0, n+s);
  if (M >= sum_c)
    printf("TAK\n");
  else
    printf("NIE\n");

  if (M >= sum_c && debug) {
    


  }

  if (debug) {
    printf("suma c = %d\n", sum_c);
    printf("Wielkosc calkowitego przeplywu: %d\n", g.MaxFlow(0, n+s));
    printf("Wielkosc przeplywu dla kolejnych krawedzi:\n");
    REP(y, SIZE(g.g)) FOREACH(it, g.g[y]) {
//      if (it->f != 0) {
//        if (y > it->v && it->f > 0)
//          printf("ERROR **************************************************************\n");
        printf("f(%d, %d) = %d, c = %d\n", y, it->v, it->f, it->c);
//      }
    }
  }

  return 0;
}