#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]):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]);
if(cnt!=n-1) printf("%d ",u);else printf("%d\n",u);
Output(ls[u]);
}
else {
Output(ls[u]);
if(cnt!=n-1) printf("%d ",u);else printf("%d\n",u);
Output(rs[u]);
}
cnt++;
}
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;
}