#include <stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#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;
bool changed[maxn];
int dfs(int u)
{
int lmin=(ls[u])?dfs(ls[u]):u;
int rmin=(rs[u])?dfs(rs[u]):inf;
if(lmin>rmin) changed[u]=true,ans++;
return min(lmin,rmin);
}
void Output(int u)
{
if(!u) return;
if(changed[u])
{
Output(rs[u]);
printf("%d ",u);
Output(ls[u]);
}
else {
Output(ls[u]);
printf("%d ",u);
Output(rs[u]);
}
}
int main()
{
scanf("%d%d",&n,&rt);
for(int i=1;i<=n;i++)
scanf("%d%d",&ls[i],&rs[i]);
ms(changed,0);
dfs(rt);
printf("%d\n",ans);
Output(rt);
return 0;
}