#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long
#define ull unsigned long long
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define piii pair<int,pii>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
#define base 2333
const int mod=1e9+7;
const int maxn=1e3+10;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){
if(y==0) return x;
return gcd(y,x%y);
}
int n,x,y;
char c1,c2;
int L[maxn],R[maxn];
int vis[maxn];
vector<int> g[maxn],ans;
void dfs(int u,int dep){
if(u==-1) return ;
g[dep].push_back(u);
dfs(R[u],dep+1);
ans.push_back(u);
dfs(L[u],dep+1);
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
rep(i,0,n){
cin>>c1>>c2;
if(c1!='-') x=(c1-'0');
else x=-1;
if(c2!='-') y=(c2-'0');
else y=-1;
L[i]=x,R[i]=y;
if(x!=-1) vis[x]=1;
if(y!=-1) vis[y]=1;
}
if(n==0) {cout<<endl<<endl;return 0;}
int rot=-1;
rep(i,0,n) if(vis[i]==0) rot=i;
dfs(rot,0);
cout<<g[0][0];
rep(i,1,n) rep(j,0,g[i].size())
cout<<" "<<g[i][j];
cout<<endl;
rep(i,0,ans.size()){
cout<<ans[i];
if(i==ans.size()-1) cout<<"\n";
else cout<<" ";
}
return 0;
}