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
#include<bits/stdc++.h>
#define PII pair<int,int>
#define f first
#define s second
#define VI vector<int>
#define LL long long
#define MP make_pair
#define LD long double
#define PB push_back
#define ALL(V) V.begin(),V.end()
#define abs(x) max((x),-(x))
#define PDD pair<LD,LD> 
#define VPII vector< PII > 
#define siz(V) ((int)V.size())
#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, n)     for(int x=0;x<(n);x++)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
using namespace std;
int n,a,b,c,d,k,m,z;
const int MXN=1e6+6;
int A[MXN];
int B[MXN];
int C[MXN];
map<int,int> M;
int ID=1;
VI V[MXN];
VI V2[MXN];
int pos[MXN];
bool cmp(int a,int b)
	{
	return A[a]<A[b];
	}
class cmp2
	{
	public:
	bool operator()(int a,int b)
		{
		return B[a]>B[b];
		}
	};

priority_queue<int,VI,cmp2> q;
VI MM[MXN];
bool calc(int x,int mode)
	{
	sort(ALL(V[x]),cmp);
	int w=0;
	int i=0;
	if(mode==0)V2[x].clear();
	while(1)
		{
//		cerr<<i<<" "<<x<<" "<<mode<<endl;
		if(q.size())
			{
			int a=q.top();
			q.pop();
			if(w>B[a])
				{
				if(mode==0)
					{
					puts("NIE");
					exit(0);
					}
				if(mode==1)
					{
					while(q.size())q.pop();
					return 0;
					}
/*				if(mode==2)
					{
					puts("WAA");
					exit(-1);
					}*/
				}
			if(mode==0||mode==2)
				{
				pos[a]=w++;
				}
			else w++;
			if(mode==0)V2[x].PB(a);
			}
		else
			{
			if(i>=V[x].size())break;
			w=A[V[x][i]];
			}
		while(i<V[x].size() && w==A[V[x][i]])
			{
			q.push(V[x][i]);
			i++;
			}		
		}
	return 1;
	}
bool moge(int A,bool pchaj=0)
	{
//	cerr<<a<<" "<<A<<" pchaj: "<<pchaj<<endl;
	if(pchaj==0)return calc(A,1);
	if(pchaj==1)calc(A,2);
	return 1;
	}
bool done[MXN];
int jakie[MXN];
set<int> S;
main()
{
scanf("%d%d",&n,&k);
FOR(i,1,n)
	{
	scanf("%d%d%d",&A[i],&B[i],&C[i]);
	M[C[i]]=1;
	}
for(auto &it:M)
	it.s=ID++;
FOR(i,1,n)
	{
	C[i]=M[C[i]];
	V[C[i]].PB(i);
	}
k=--ID;
bool ok=1;
FOR(i,1,k)calc(i,0);
//puts("TAK");return 0;

//FOR(i,1,n)printf("%d\n",pos[i]);
int res=0;
if(k>1)
	while(1)
	{
//	FOR(i,1,n)fprintf(stderr,"%d %d %d %d\n",pos[i],A[i],B[i],done[i]);cerr<<endl;
	int mi=1e9;mi++;
	int mi2=1e9;mi2++;

	FOR(i,1,n)
		{
		if(done[i]==0)mini(mi,pos[i]);
		}
	if(mi>1e9)break;
	bool ok=1;
	VI pomm;
	FOR(i,1,n)
		{
		if(A[i]==mi&&done[i]==0)
			{
			pomm.PB(i);
			}
		}
	FOR(i,1,n)
		if(done[i]==0)
			{
		//	if(A[i]>mi)mini(mi2,A[i]);
			if(pos[i]>mi)mini(mi2,pos[i]);
			}
			
	if(mi2>1e9)break;
	int skok=mi2-mi;
//	cerr<<"skok: "<<skok<<endl;
	REP(i,pomm.size())
		{
		A[pomm[i]]+=skok;
		if(A[pomm[i]]>B[pomm[i]])ok=0;
		}
	REP(i,pomm.size())
		jakie[pomm[i]]=1;
		
	REP(i,pomm.size())
		{
		if(!ok)break;
		if(jakie[pomm[i]]==1)
			{
			jakie[pomm[i]]=2;
			ok&=moge(C[pomm[i]]);
			}
		}
	
	
	if(ok)
		REP(i,pomm.size())
			{
			if(jakie[pomm[i]]==2)
				{
				jakie[pomm[i]]=0;
				ok&=moge(C[pomm[i]],2);
				}
			}
	else
		REP(i,pomm.size())
			{
			if(pos[pomm[i]]==mi)
				{
				A[pomm[i]]-=skok;
				done[pomm[i]]=1;
				}
			}
//	cerr<<"ok: "<<ok<<endl;
	}

res=0;
FOR(i,1,n)
	{
	if(S.count(pos[i])==0)	
		{
		res++;
		S.insert(pos[i]);
		}
	}
printf("%d\n",res);
FOR(i,1,n)printf("%d\n",pos[i]);
}