#pragma comment(linker, "/STACK:102400000000,102400000000")
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5+8;
/****************************************************/
int ch[N][2],mn[N<<2];
int deg[N];
int n,m,ans;
int L[N],R[N],cnt;
void dfs(int &u){if(!u) return ;
L[u] = ++ cnt;
dfs(ch[u][0]),dfs(ch[u][1]);
R[u] = cnt;
}
void pushup(int rt){
mn[rt]=min(mn[rt<<1],mn[rt<<1|1]);
}
void build(int rt,int l,int r){
mn[rt] = N;
if(l == r) return ;
int m = r+l >> 1;
build(rt<<1 ,l ,m);
build(rt<<1|1,m+1,r);
pushup(rt);
}
void update(int rt,int l,int r,int v,int p){
if(l==r){mn[rt] = v; return ; }
int m = r+l >> 1;
if(p<=m) update(rt<<1 ,l ,m,v,p);
else update(rt<<1|1,m+1,r,v,p);
pushup(rt); return ;
}
int query(int rt,int l,int r,int L,int R){
if(L<=l&&r<=R) return mn[rt];
int m = r+l >> 1,ans = N;
if(L<=m) ans = min(ans,query(rt<<1 ,l ,m,L,R));
if(R> m) ans = min(ans,query(rt<<1|1,m+1,r,L,R));
return ans;
}
vector<int>v;
int z , y;
void dfs1(int &u){if(!u) return ;
if(ch[u][0]) z = query(1,1,n,L[ch[u][0]],R[ch[u][0]]);else z = u;
if(ch[u][1]) y = query(1,1,n,L[ch[u][1]],R[ch[u][1]]);else y = u;
// printf("%d <--- %d %d %d %d\n", u,z,y,ch[u][0],ch[u][1]);
if(y < z) swap(ch[u][0],ch[u][1]),ans++;
dfs1(ch[u][0]);
v.push_back(u);
dfs1(ch[u][1]);
}
int main(){
//freopen("tiS1_6.in","r",stdin);
//freopen("tiS1_6.out","w",stdout);
while(~scanf("%d%d",&n,&m)){
v.clear(); ans = 0; cnt = 0;
build(1,1,n);
for(int i=1;i<=n;i++) {
scanf("%d%d",&ch[i][0],&ch[i][1]);
deg[i] = 0;
if(ch[i][0] != 0) deg[i] ++;
if(ch[i][1] != 0) deg[i] ++;
}
dfs(m);
for(int i=1;i<=n;i++)if(deg[i] < 2)update(1,1,n,i,L[i]);
// for(int i=1;i<=n;i++) printf("%d [%d %d]\n",i, L[i],R[i] );
dfs1(m);
printf("%d\n%d",ans,v[0]);
for(int i=1;i<v.size();i++) printf(" %d", v[i]); puts("");
}
return 0;
}
/***
Input
3
7 4
0 0
1 3
0 0
2 5
6 7
0 0
0 0
7 4
5 7
6 3
0 0
2 1
0 0
0 0
0 0
7 4
0 0
6 3
0 0
2 5
1 7
0 0
0 0
Output
0
1 2 3 4 6 5 7
1
3 2 6 4 5 1 7
2
1 5 7 4 3 2 6
***/