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
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Comparator;

public class sze {

	static class data {

	public
		data(int s, int k, int c)
		{
			start = s;
			koniec = k;
			czas = c;
		}
		
		int start;
		int koniec;
		int czas;
	}
	
	private static int skipWhitespace(final InputStream in) throws IOException {
		int val = -1;
		while ((val = in.read()) != -1) {
			if (!Character.isWhitespace((char) val))
				break;
		}
		return val;
	}

	private static int readInt(final InputStream in) {
		final StringBuilder b = new StringBuilder();
		try {
			int val = skipWhitespace(in);
			b.append((char) val);
			while ((val = in.read()) != -1) {
				if (Character.isWhitespace((char) val))
					break;
				b.append((char) val);
			}
			return Integer.parseInt(b.toString());
		} catch (final IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static void run(final InputStream in, PrintStream out) {
		int n = readInt(in);
		int m = readInt(in);

		
		sze.data zad[] = new sze.data[n];
		
		for (int i = 0; i < n; i++) {
			zad[i] = new data(readInt(in), readInt(in), readInt(in));
		}
		Arrays.sort(zad, new Comparator() {
			public int compare(Object o1, Object o2) {
				int k = ((data) o1).czas - ((data) o2).czas;
				if (k==0)
				{
					k =  (((data) o1).koniec - ((data) o2).koniec);
				}					
			    return k;
			}
		});
		
		for (int i = 0; i < n; i++) {
            System.err.println( zad[i].start+ " " + zad[i].koniec + " " + zad[i].czas);
		}
		System.err.println();
	
		boolean pro[][] = new boolean[m+1][1000000+1];
		for (int z = 0; z < n; z++)
		{
			boolean wyk = false;
			for (int i = zad[z].start; i < zad[z].koniec; i++) {
				for (int p = 1; p <= m; p++) {
					if (!pro[p][i]) {
						pro[p][i] = true;
						zad[z].czas--;
						wyk = true;
						break;
					}
				}

				if (wyk) {
                    break;					
				}
			}
			if (!wyk) {
				out.println("NIE");
				return;
			}
		}
		
		Arrays.sort(zad, new Comparator() {
			public int compare(Object o1, Object o2) {
				return ((data) o1).koniec - ((data) o2).koniec;
			}
		});
		
		for (int z = 0; z < n; z++)
        {
			if (zad[z].czas > 0) {
				for (int i = zad[z].start; i < zad[z].koniec; i++) {
					for (int p = 1; p <= m; p++) {

						if (!pro[p][i]) {
							pro[p][i] = true;
							zad[z].czas--;
							break;
						}
					}

					if (zad[z].czas == 0)
						break;
				}
				if (zad[z].czas > 0) {
					out.println("NIE");
					return;
				}
			}
		}
		
		out.println("TAK");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		run(System.in, System.out);
	}
}