Under construction

Still being built. Things may change

Tree

Time limit2sMemory limit1024 MB

Summary
Given a binary search tree with keys but no values, assign each vertex a value from 1 to 10^9 so the rebuilt tree is a BST, or report -1 if impossible.
Level

Medium6 of 10

Topics
Tree, DFS, Greedy
Solved
No attempts yet

Statement

Sasha recently learned about binary search trees in her computer science class. A binary search tree is a binary tree where each vertex holds a number called its key. The key of a vertex is greater than every key in its left subtree and less than every key in its right subtree.

Such a tree lets you store a set of numbers and check efficiently whether a given number belongs to the set. Sasha has already implemented a binary search tree. Storing a set is not very interesting to her, though. She would rather store an associative array, where each key has another number called its value. Each vertex then holds two numbers, a key and a value, and the tree is a binary search tree by the vertex keys.

Sasha wants an associative array design in which each vertex stores only one number. She defines the following operation on a binary search tree that stores key-value pairs. The new key of the left child of a vertex becomes the key of that vertex. The new key of the right child becomes the value of that vertex. If a vertex has no left or right child before the operation, that child appears and gets the corresponding key. The new key of the root becomes some number TT.

You are given a binary search tree, but the values stored in its vertices are not given. Print any set of values, where each value is an integer from 11 to 10910^9, such that the tree obtained after the operation is a binary search tree. If no such set exists, print that it is impossible.

Input

The first line contains two integers nn and TT separated by a space (1≤n≤1051 \le n \le 10^5, 1≤T≤1091 \le T \le 10^9), where nn is the number of vertices in the original tree and TT is the key of the root in the new tree.

The next nn lines describe the vertices. Line ii contains three integers lil_i, rir_i, aia_i (1≤li≤n1 \le l_i \le n, 0≤ri≤n0 \le r_i \le n, 0≤ai≤1090 \le a_i \le 10^9): the index of the left child of vertex ii, the index of its right child, and the key of vertex ii. If a vertex has no left or right child, the corresponding number is 0. The first vertex is the root of the tree.

Output

If no solution exists, print −1-1 on the first line. Otherwise, print nn numbers, the value of each vertex in order. Each number must be an integer from 11 to 10910^9.

Examples1

  1. Example 1

    Input
    3 4
    2 3 2
    0 0 1
    0 0 5
    
    Expected output
    6 3 7