//**
给定一颗二叉树的根节点 root,按照如下两种标准分别实现二叉树的边界节点的逆时针打印。
标准一:
1,根节点为边界节点。
2,叶节点为边界节点。
3,如果节点在其所在的层中是最左的或最右的,那么该节点也是边界节点。
标准二:
1,根节点为边界节点。
2,叶节点为边界节点。
3,树左边界延伸下去的路径为边界节点。
4,树右边界延伸下去的路径为边界节点。
ps:具体请对照样例
*/
import java.io.*;
import java.util.*;
public class Main{
    public static void main(String[] args)throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] ss = br.readLine().trim().split(" ");
        int len = Integer.parseInt(ss[0]);
        int rootnum = Integer.parseInt(ss[1]);
        TreeNode[] nodes = new TreeNode[len+1];
        for(int i=1;i<=len;i++){
            nodes[i] = new TreeNode(i);
        }
        for(int i=0;i<len;i++){
            String[] arr = br.readLine().trim().split(" ");
            int data = Integer.parseInt(arr[0]);
            int left = Integer.parseInt(arr[1]);
            int right = Integer.parseInt(arr[2]);
            nodes[data].left = nodes[left];
            nodes[data].right = nodes[right];
        }
        TreeNode root = nodes[rootnum];
        fun1(root); //标准一
        StringBuilder sb = new StringBuilder();
        fun2(root,sb); //标准二
        System.out.println(sb.toString().trim());
    }
    public static void fun2(TreeNode root,StringBuilder sb){
        if(root==null) return;
        sb.append(root.val+" ");
        if(root.left!=null&&root.right!=null){
            printleft(root.left,true,sb);
            printright(root.right,true,sb);
        }else{
            fun2(root.left==null?root.right:root.left,sb);
        }
    }
    public static void printleft(TreeNode root,boolean flag,StringBuilder sb){
        if(root==null) return ;
        if(flag||(root.left==null&&root.right==null)){
            sb.append(root.val+" ");
        }
        printleft(root.left,flag,sb);
        printleft(root.right,flag&&(root.left==null),sb);
    }
    public static void printright(TreeNode root,boolean flag,StringBuilder sb){
        if(root==null) return;
        printright(root.left,flag&&(root.right==null),sb);
        printright(root.right,flag,sb);
        if(flag||(root.left==null&&root.right==null)){
            sb.append(root.val+" ");
        }
    }
    public static void fun1(TreeNode root){
        if(root==null) return;
        int height = treeHeight(root);
        TreeNode[][] res = new TreeNode[height][2];
        edgeNode(root,res,0);
        StringBuilder sb = new StringBuilder();
        //左边
        for(int i=0;i<height;i++){
            sb.append(res[i][0].val+" ");
        }
        //中间
        midNode(root,res,0,sb);
        for(int i=height-1;i>=0;i--){
            if(res[i][0]!=res[i][1]){
                sb.append(res[i][1].val+" ");
            }
        }
        System.out.println(sb.toString().trim());
    }
    public static void midNode(TreeNode root,TreeNode[][] res,int l,StringBuilder sb){
        if(root==null) return;
        if(root.left==null&&root.right==null&&res[l][0]!=root&&res[l][1]!=root){
            sb.append(root.val+" ");
        }
        midNode(root.left,res,l+1,sb);
        midNode(root.right,res,l+1,sb);
    }
    public static void edgeNode(TreeNode root,TreeNode[][] res,int l){
        if(root==null) return;
        res[l][0] = res[l][0]==null?root:res[l][0];
        res[l][1] = root;
        edgeNode(root.left,res,l+1);
        edgeNode(root.right,res,l+1);
    }
    //树高
    public static int treeHeight(TreeNode root){
        if(root==null) return 0;
        return Math.max(treeHeight(root.left)+1,treeHeight(root.right)+1);
    }
}
class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;
    public TreeNode(int val){
        this.val = val;
    }
}