#include <bits/stdc++.h>   using namespace std;   pair<int, int> ar[110];   set<int> se;   int findstart(int k) {     while (ar[k].first != 0) k = ar[k].first;     return k;   }   int findend(int k) {     while (ar[k].second != 0) k = ar[k].second;     return k;   }   void addalltoend(int i) {     se.insert(ar[i].second);     while (ar[i].second != 0) {       i = ar[i].second;       se.insert(ar[i].second);     }   }   int main() {     int n, en, nen;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> ar[i].first >> ar[i].second;     }     int i = 0;     while (ar[i].first == 0 && ar[i].second == 0 && i != n + 1) {       i++;     }     if (n == 1) {       cout << 0 <<     << 0;       return 0;     }     if (n != 1 && i == n + 1) {       for (i = 1; i < n; i++) {         cout << i - 1 <<     << i + 1 <<   n ;       }       cout << i - 1 <<     << 0 <<   n ;       return 0;     }     i = findstart(i);     en = findend(i);     se.insert(i);     addalltoend(i);     se.erase(0);     for (set<int>::const_iterator it = se.begin(); it != se.end(); it++)       for (int i = 1; i <= n; i++) {         if (ar[i].first == 0 && ar[i].second != 0 &&             se.find(ar[i].second) == se.end()) {           ar[en].second = i;           ar[i].first = en;           i = findstart(i);           addalltoend(i);           en = findend(i);         }       }     for (int i = 1; i <= n; i++) {       if (ar[i].first == 0 && ar[i].second == 0) {         ar[en].second = i;         ar[i].first = en;         en = i;       }     }     for (int i = 1; i <= n; i++)       cout << ar[i].first <<     << ar[i].second <<   n ;     for (set<int>::const_iterator it = se.begin(); it != se.end(); it++) return 0;   }   