#include <bits/stdc++.h>
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=5e5+5;
int ls[maxn],rs[maxn];
int ans=0,n,rt,cnt;
bool changed[maxn];
int dfs(int u)
{
int lmin=(ls[u])?dfs(ls[u]):u;
int rmin=(rs[u])?dfs(rs[u]):u;
if(lmin>rmin) changed[u]=true,ans++;
return min(lmin,rmin);
}vector<int>v;
void Output(int u)
{
#pragma comment(linker, "/STACK:102400000,102400000")
if(!u) return;
if(changed[u])
{
Output(rs[u]);
v.push_back(u);
Output(ls[u]);
}
else {
Output(ls[u]);
v.push_back(u);
Output(rs[u]);
}
cnt++;
}
int main()
{
cnt = ans = 0;
v.clear();
scanf("%d%d",&n,&rt);
for(int i=1;i<=n;i++)
scanf("%d%d",&ls[i],&rs[i]);
ms(changed,0);
dfs(rt);
Output(rt);
printf("%d\n",ans);
printf("%d",v[0]);
for(int i=1;i<v.size();i++) printf(" %d",v[i]);
printf("\n");
return 0;
}