#include <stdio.h>
#define N 25
 
typedef struct{
  int id;
  int sibling;
  int parent;
  int right;
  int left;

} Node;
 
int main(){
  int i, j, n;
  int id, right, left, depth, parent;
  Node node[N];
 
  scanf("%d", &n);
 
  for(i = 0; i < n;  i++){
    node[i].sibling = -1;
    node[i].parent = -1;
  }
 
  for( i = 0; i < n; i++){
    scanf("%d%d%d", &id, &left, &right);
 
    node[id].id = id;
    node[id].right = right;
    node[id].left = left; 

    if(right != -1 || left != -1){
      node[right].parent = id;
      node[left].parent = id;
      node[right].sibling = left;
      node[left].sibling = right;
    }