#include <cstdio>
#include <iostream>
#include <memory.h>

using namespace std;

const int maxn = 1 << 18;
const int inf = 1e9 + 7;

int n, cnt, a[maxn], b[maxn], c[maxn], l[maxn], r[maxn], p[maxn];
int cur = -1;
bool fail = false;

void dfs1(int v) {
	if (v > n) return;
	b[l[v]] = a[v];
	dfs1(l[v]);
	dfs1(r[v]);
}

void dfs2(int v) {
 	if (v <= n) dfs2(l[v]);
	if (b[v] == -1) {
		b[v] = ++cur;
		c[p[v]] = cur;
	}
	else {
		fail |= b[v] <= cur;
		cur = max(cur, b[v]);
	}
	if (v <= n) dfs2(r[v]);
}

int t;

int main() {
 	freopen("tree.in", "r", stdin);
	freopen("tree.out", "w", stdout);

	memset(a, -1, sizeof(a));
	memset(b, -1, sizeof(b));
	memset(c, -1, sizeof(c));

	cin >> n >> t;
	cnt = n;
	for (int i = 1; i <= n; i++) {
	 	cin >> l[i] >> r[i] >> a[i];
		if (l[i] == 0) l[i] = ++cnt;
		if (r[i] == 0) r[i] = ++cnt;
		p[l[i]] = i;
		p[r[i]] = i;
	}
	b[1] = t;
	dfs1(1);
	dfs2(1);
	if (fail) cout << -1 << endl;
	else for (int i = 1; i <= n; i++) cout << c[i] << " \n"[i == n];	

	return 0;
}