#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;
// }
int mystack[N],cur[N];
void Dfs(int m){
int len = 0;mystack[++len]=m;cur[m]=0;
for(int u;len;){
u = mystack[len];
// printf("%d <--\n",u);
if(cur[u]==0) L[u] = ++ cnt; // 第一次遍历到当前节点
if(cur[u]>=2) {R[u]=cnt,--len;continue;} // 最后一次
int &to = ch[u][cur[u]]; // 遍历子节点
if(to) mystack[++len] = to, cur[to] = 0;
cur[u]++;
}
}
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 m){
int len = 0;mystack[++len]=m;cur[m]=0;
for(int u;len;){
u = mystack[len];
if(cur[u]==0) {
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;
if(y < z) swap(ch[u][0],ch[u][1]),ans++;
}
if(cur[u]==1) {v.push_back(u);}
if(cur[u]>=2) {--len;continue;}
int &to = ch[u][cur[u]];
if(to) mystack[++len] = to, cur[to] = 0;
cur[u]++;
}
}
// 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++) printf("%d [%d %d]\n",i, L[i],R[i] );
for(int i=1;i<=n;i++)if(deg[i] < 2)update(1,1,n,i,L[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;
}