#include<iostream>
using namespace std;
const int N = 10005;
int n , fa[N], k;
int main()
{
cin >> n >> k;
for(int i = 1; i <= n; i++)
{
cin >> fa[i];
}
int x = k;
while(true)
{
cout << x << " ";
if(x == fa[x])
{
break;
}
x = fa[x];
}
return 0;
}
#include<iostream>
using namespace std;
const int N = 10005;
int n , fa[N], k;
int main()
{
cin >> n >> k;
for(int i = 1; i <= n; i++)
{
cin >> fa[i];
}
int x = k;
while(true)
{
cout << x << " ";
if(x == fa[x])
{
break;
}
x = fa[x];
}
return 0;
}