7-9
#include<bits/stdc++.h>
#define vint vector<int>
#define vstr vector<string>
#define vll vector<long long>
#define ll long long
#define ull unsigned long long
#define pf printf
#define sf scanf
#define sfd(n) scanf("%d", &n)
#define sfc(n) scanf("%c", &n)
#define sflf(n) scanf("%lf", &n)
#define sfll(n) scanf("%lld", &n)
#define pfd(n) printf("%d", n)
#define pfc(n) printf("%c", n)
#define pflf(n) printf("%lf", n)
#define pfll(n) printf("%lld", n)
#define pft printf("\t")
#define pfn printf("\n")
#define pfk printf(" ")
#define PI 3.1415926
#define MAX 100010
#define MOD ((long long)1e9+7)
using namespace std;
int a[MAX];
struct D{
int num;
int p;
};
D data[MAX];
void F(int x, int y) {
int root = x;
int cnt = 1;
while( root!=-1 ) {
if( data[root].p ) {
cnt = data[root].p+1;
break;
}
root = a[root];
cnt++;
}
data[y].num = y;
data[y].p = cnt;
}
int main() {
int n;
cin >> n;
for( int i=1; i<=n; i++ ) {
cin >> a[i];
}
for( int i=1; i<=n; i++ ) {
F(a[i], i);
}
sort(data+1, data+n+1, [](D a, D b) {
if( a.p>b.p ) {
return true;
}else if( a.p==b.p ){
return a.num<b.num;
}
return false;
});
int min = data[1].p;
pfd(min);
pfn;
for( int i=1; i<=n; i++ ) {
// cout << data[i].num << " " << data[i].p << endl;
if( data[i].p!=min ) {
break;
}
if( i>1 ) pfk;
pfd(data[i].num );
}
return 0;
}