#include <iostream>

using namespace std;

int main()
{
   int n, k;
   int num = 1;
   cin >> n >> k;
   for (int i = 0; i < n; i++){
      for (int j = 0; j < k - 1; j++){
         cout << num;
         num ++;
         if ( j != n-1) cout<<" ";
      }
      for (int j = k - 1; j < n; j++){
         cout << 25 - (k -1) * (i + 1)  + j - k;
         if ( j != n - 1) cout<< " ";
      }
      if ( i != n - 1) cout << endl;
   }

}


#include <iostream>

using namespace std;

int main()
{
   int n, k;
   int num = 1;

   int** temp;
   int cnt = 0;
   cin >> n >> k;
   int t = n * n;
   for (int i = 0; i < n; i++) temp = new int* [n];
   for (int i = 0; i < n; i++) temp[i] = new int [n];
   for (int i = 0; i < n; i++){
      for (int j = 0; j < k - 1; j++){
         //cout << num;
         temp[i][j] = num;
         num ++;
         //if ( j != n-1) cout<<" ";
      }
      for (int j = k - 1; j < n; j++){
         temp[i][j] = t - (n - k) + (j - k + 1);
      }
      t = temp[i][k - 1] - 1;
      //if ( i != n - 1) cout << endl;
   }

   for (int i = 0; i < n; i++)
      cnt = cnt + temp[i][k - 1];
   cout << cnt << endl;
   for (int i = 0; i < n; i++){
      for (int j = 0; j < n; j++){
         cout << temp[i][j];
         if (j != n - 1) cout << " ";
      }
      if (i != n - 1) cout << endl;
   }

}


#include <iostream>

using namespace std;

int main()
{
   int n, k;
   int num = 1;
   cin >> n >> k;
   for (int i = 0; i < n; i++){
      for (int j = 0; j < k - 1; j++){
         cout << num;
         num ++;
         if ( j != n-1) cout<<" ";
      }
      for (int j = k - 1; j < n; j++){
         cout << n * (n - i) - (n - k) + (j - k + 1);
         if ( j != n - 1) cout<< " ";
      }
      if ( i != n - 1) cout << endl;
   }

}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <math.h>
#include <string>
#include <vector>
#include <time.h>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <bitset>

using namespace std;

////////////////////////////////////////////////////////////////////////////////
//#pragma comment(linker, "/STACK:100000000")
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for(int i = 0; i < (int)(n); ++i)
#define for1(i, n) for(int i = 1; i < (int)(n); ++i)
#define forin(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template<class T> inline bool Min(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> inline bool Max(T &a, T b) { return a < b ? (a = b, true) : false; }

inline int    ni() { int    a; scanf("%d",   &a); return a; } // next_int();
inline ll     nl() { ll     a; scanf("%lld", &a); return a; } // next_long();
inline double nd() { double a; scanf("%lf",  &a); return a; } // next_double();

inline void pi(int    a) { printf ( "%d ",      a); }
inline void pl(ll     a) { printf ( "%lld ",    a); }
inline void pd(double a) { printf ( "%.12lf ",  a); }
////////////////////////////////////////////////////////////////////////////////

#ifdef LOCAL
const int N = 10;
#else
const int N = 510;
#endif

int xs[N][N];

void solve()
{
    int n = ni();
    int k = ni() - 1;

    int cur = 1;

    for ( int i = 0; i < n; i++ )
        for ( int j = 0; j < k; j++ )
            xs[i][j] = cur++;

    for ( int i = 0; i < n; i++ )
        for ( int j = k; j < n; j++ )
            xs[i][j] = cur++;

    int sum = 0;
    forn(i, n) sum += xs[i][k];

    printf ( "%d\n", sum);
    forn(i, n)
    {
        forn(j, n) printf("%d ", xs[i][j]);
        printf("\n");
    }
}

int main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    solve();

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <math.h>
#include <string>
#include <vector>
#include <time.h>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <bitset>

using namespace std;

////////////////////////////////////////////////////////////////////////////////
//#pragma comment(linker, "/STACK:100000000")
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for(int i = 0; i < (int)(n); ++i)
#define for1(i, n) for(int i = 1; i < (int)(n); ++i)
#define forin(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template<class T> inline bool Min(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> inline bool Max(T &a, T b) { return a < b ? (a = b, true) : false; }

inline int    ni() { int    a; scanf("%d",   &a); return a; } // next_int();
inline ll     nl() { ll     a; scanf("%lld", &a); return a; } // next_long();
inline double nd() { double a; scanf("%lf",  &a); return a; } // next_double();

inline void pi(int    a) { printf ( "%d ",      a); }
inline void pl(ll     a) { printf ( "%lld ",    a); }
inline void pd(double a) { printf ( "%.12lf ",  a); }
////////////////////////////////////////////////////////////////////////////////

#ifdef LOCAL
const int N = 10;
#else
const int N = 1e5+10;
#endif

int xs[N][N];

void solve()
{
    int n = ni();
    int k = ni() - 1;

    int cur = 1;

    for ( int i = 0; i < n; i++ )
        for ( int j = 0; j < k; j++ )
            xs[i][j] = cur++;

    for ( int i = 0; i < n; i++ )
        for ( int j = k; j < n; j++ )
            xs[i][j] = cur++;

    forn(i, n)
    {
        forn(j, n) printf("%d ", xs[i][j]);
        printf("\n");
    }
}

int main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    solve();

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <math.h>
#include <string>
#include <vector>
#include <time.h>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <bitset>

using namespace std;

////////////////////////////////////////////////////////////////////////////////
//#pragma comment(linker, "/STACK:100000000")
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for(int i = 0; i < (int)(n); ++i)
#define for1(i, n) for(int i = 1; i < (int)(n); ++i)
#define forin(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template<class T> inline bool Min(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> inline bool Max(T &a, T b) { return a < b ? (a = b, true) : false; }

inline int    ni() { int    a; scanf("%d",   &a); return a; } // next_int();
inline ll     nl() { ll     a; scanf("%lld", &a); return a; } // next_long();
inline double nd() { double a; scanf("%lf",  &a); return a; } // next_double();

inline void pi(int    a) { printf ( "%d ",      a); }
inline void pl(ll     a) { printf ( "%lld ",    a); }
inline void pd(double a) { printf ( "%.12lf ",  a); }
////////////////////////////////////////////////////////////////////////////////

#ifdef LOCAL
const int N = 10;
#else
const int N = 510;
#endif

int xs[N][N];

void solve()
{
    int n = ni();
    int k = ni() - 1;

    int cur = 1;

    for ( int i = 0; i < n; i++ )
        for ( int j = 0; j < k; j++ )
            xs[i][j] = cur++;

    for ( int i = 0; i < n; i++ )
        for ( int j = k; j < n; j++ )
            xs[i][j] = cur++;

    forn(i, n)
    {
        forn(j, n) printf("%d ", xs[i][j]);
        printf("\n");
    }
}

int main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    solve();

    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;

const int MAXN = 1001 ;
int a [ MAXN ] [ MAXN ] = { } ;

main(){
    ios::sync_with_stdio(0);

    int n,m;
    cin>>n>>m;
    m--;
    for(int i=0 ; i<m ; i++)
        for(int j=0 ; j<n ; j++)
            a[i][j]=j*m+i+1;
    for(int i=m ; i<n;i++)
        for(int j=0 ; j<n ; j++)
            a[i][j]= n * m + j * (n - m ) + i - m+1;
    int s=0;
    for(int i=0;i<n;i++)
        s+=a[m][i];
    cout<<s<<'\n';
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++)
            cout<<a[j][i]<<' ';
        cout<<'\n';
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MAX_N = 5e2+5;
bool vis[MAX_N];
int ans[MAX_N][MAX_N],num;
int main(){
    int n,k;
    cin>>n>>k;
    int start0 = (k-1)*n+1,step=n-k+1;
    for(int i=1;i<=n;i++){
        num+=start0;
        vis[start0] = 1;
        ans[i][k] = start0;
        start0+=step;
    }
    start0 = 1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<k;j++){
            while(vis[start0])start0++;
            ans[i][j] = start0;
            vis[start0] = 1;
        }
    }
     for(int i=1;i<=n;i++){
        for(int j=k+1;j<=n;j++){
            while(vis[start0])start0++;
            ans[i][j] = start0;
            vis[start0] = 1;
        }
     }
     cout<<num<<endl;
     for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout<<ans[i][j]<<" ";
        }
        cout<<endl;
     }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MAX_N = 5e2+5;
bool vis[MAX_N*MAX_N];
int ans[MAX_N][MAX_N],num;
int main(){
    int n,k;
    cin>>n>>k;
    int start0 = (k-1)*n+1,step=n-k+1;
    for(int i=1;i<=n;i++){
        num+=start0;
        vis[start0] = 1;
        ans[i][k] = start0;
        start0+=step;
    }
    start0 = 1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<k;j++){
            while(vis[start0])start0++;
            ans[i][j] = start0;
            vis[start0] = 1;
        }
    }
     for(int i=1;i<=n;i++){
        for(int j=k+1;j<=n;j++){
            while(vis[start0])start0++;
            ans[i][j] = start0;
            vis[start0] = 1;
        }
     }
     cout<<num<<endl;
     for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout<<ans[i][j]<<" ";
        }
        cout<<endl;
     }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MAX_N = 5e2+5;
bool vis[MAX_N];
int ans[MAX_N][MAX_N],num;
int main(){
    int n,k;
    cin>>n>>k;
    int start0 = (k-1)*n+1,step=n-k+1;
    for(int i=1;i<=n;i++){
        num+=start0;
        vis[start0] = 1;
        ans[i][k] = start0;
        start0+=step;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<k;j++){
            while(vis[start0])start0++;
            ans[i][j] = start0;
            vis[start0] = 1;
        }
    }
     for(int i=1;i<=n;i++){
        for(int j=k+1;j<=n;j++){
            while(vis[start0])start0++;
            ans[i][j] = start0;
            vis[start0] = 1;
        }
     }
     cout<<num<<endl;
     for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout<<ans[i][j]<<" ";
        }
        cout<<endl;
     }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAX = 510;
int a[MAX][MAX];
int main(){
    int n, k;
    scanf("%d %d", &n, &k);
    k--;
    int cur = 1;
    for(int i = 0; i < n; i++)for(int j = 0; j < k; j++)a[i][j] = cur++;
    for(int i = 0; i < n; i++)for(int j = k; j < n; j++)a[i][j] = cur++;
    int ans = 0;
    for(int i = 0; i < n; i++)ans+=a[i][k];
    printf("%d\n", ans);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){
    int n, k;
    scanf("%d %d", &n, &k);
    long long ans = 0;
    for(int i = k; i <= n*n; i+=n)ans+= 1ll*i;
    int cur =1;
    printf("%lld\n", ans);
    for(int i = 1; i <=n; i++){
        for(int j = 1; j <=n; j++){
            printf("%d ", cur++);
        }
        printf("\n");
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main(){

    //freopen("1.in", "r", stdin);
	//freopen("1.out", "w", stdout);

	int n, k;
	cin >> n >> k;
	int m[510][510];

	int cont = 1;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			m[i][j] = cont++;
		}
	}

	ll ans = 0;
	for(int i = 1; i <= n; i++)
		ans += m[i][k];

	cout << ans << endl;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++)
			printf("%d ", m[i][j]);
		printf("\n");
	}

	return 0;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main(){

    //freopen("1.in", "r", stdin);
	//freopen("1.out", "w", stdout);

	int n, k;
	cin >> n >> k;
	int m[510][510];

	int cont = n*n;
	int cont2 = 1;
	for(int i = 1; i <= n; i++){
		for(int j = n; j >= 1; j--){
			if(j >= k)
				m[i][j] = cont--;
			else{
				for(j = 1; j < k; j++)
					m[i][j] = cont2++;
				break;
			}

		}
	}

	ll ans = 0;
	for(int i = 1; i <= n; i++)
		ans += m[i][k];

	cout << ans << endl;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++)
			printf("%d ", m[i][j]);
		printf("\n");
	}

	return 0;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define INF 300000

int main(){

    //freopen("1.in", "r", stdin);
	//freopen("1.out", "w", stdout);

	int n, k;
	cin >> n >> k;
	int cont = 1;
	int inf = INF;
	ll ans = 0;
	for(int i = 0; i < n; i++)
		ans += INF+i;
	cout << ans << endl;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			if(j == k)
				printf("%d ", inf++);
			else if(j >= k)
				printf("%d ", inf+500+j);

			else
				printf("%d ", cont++);
		}
		printf("\n");
	}

	return 0;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main(){

    //freopen("1.in", "r", stdin);
	//freopen("1.out", "w", stdout);

	int n, k;
	cin >> n >> k;
	int m[510][510];

	int cont = 1;
	for(int i = 1; i <= 509; i++){
		for(int j = 1; j <= 509; j++){
			m[i][j] = cont++;
		}
	}

	ll ans = 0;
	for(int i = 1; i <= n; i++)
		ans += m[i][k];

	cout << ans << endl;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++)
			printf("%d ", m[i][j]);
		printf("\n");
	}

	return 0;

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main(){

    //freopen("1.in", "r", stdin);
	//freopen("1.out", "w", stdout);

	int n, k;
	cin >> n >> k;
	int m[510][510];

	int cont = n*n;
	int cont2 = 1;
	for(int i = 1; i <= n; i++){
		for(int j = n; j >= 1; j--){
			if(j >= k)
				m[i][j] = cont--;
			else
				m[i][j] = cont2++;
		}
	}

	ll ans = 0;
	for(int i = 1; i <= n; i++)
		ans += m[i][k];

	cout << ans << endl;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++)
			printf("%d ", m[i][j]);
		printf("\n");
	}

	return 0;

}


//vien
#include <iostream>
#include <fstream>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>

#define fi_name "C.inp"
#define fo_name "C.out"

using namespace std;

int d[123321];
int n,k,ck,max1,dem;
int a[1001][1001];

int main()
{
	int i,j;

//	freopen(fi_name,"r",stdin);
//	freopen(fo_name,"w",stdout);

	while(cin>>n>>k)
	{
		int x,y,z,cs;
		dem=0;
		x=(k-1)*n+1;
		cs=n-k+1;
		max1=0;
		for(i=1;i<=n;i++)
		{
			a[i][k]=x;
			max1+=x;
			x+=cs;
		}
		x-=cs;
		y=1;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<k;j++)
			{
				if(a[i][j]==0)
				{
					a[i][j]=y;
				}
				else
					break;
				y++;
			}
		}
		for(i=1;i<=n;i++)
		{
			y=a[i][k]+1;
			for(j=k+1;j<=n;j++)
			{
				a[i][j]=y;
				y++;
			}
		}
		printf("%d\n",max1);
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				if(j>1)
					cout<<" ";
				printf("%d",a[i][j]);
			}
			printf("\n");
		}
	}

	return 0;
}


//vien
#include <iostream>
#include <fstream>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>

#define fi_name "C.inp"
#define fo_name "C.out"

using namespace std;

int d[1221];
int n,k,ck,max1,dem;
int a[1001][1001];

int main()
{
	int i,j;

//	freopen(fi_name,"r",stdin);
//	freopen(fo_name,"w",stdout);

	while(cin>>n>>k)
	{
		int x,y,z,cs;
		dem=0;
		x=(k-1)*n+1;
		cs=n-k+1;
		max1=0;
		for(i=1;i<=n;i++)
		{
			a[i][k]=x;
			max1+=x;
			d[x]=1;
			x+=cs;
		}
		x-=cs;
		y=1;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<k;j++)
			{
				if(a[i][j]==0)
				{
					a[i][j]=y;
				}
				else
					break;
				y++;
			}
		}
		for(i=1;i<=n;i++)
		{
			y=a[i][k]+1;
			for(j=k+1;j<=n;j++)
			{
				a[i][j]=y;
				y++;
			}
		}
		printf("%d\n",max1);
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				if(j>1)
					cout<<" ";
				printf("%d",a[i][j]);
			}
			printf("\n");
		}
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (int r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
#define f first
#define s second
const int MAX=1000*100+5;
int n, a, maxi=0;
vector<vector<int>>A;
vector<int>dp, type;
int rec(int v){
	if(dp[v]>-1)return dp[v];
	if(A[v].size()>1){
		dp[v]=INT_MIN;
		return dp[v];
	}
	if(type[v]){
		dp[v]=1;
		return 1;
	}
	dp[v]=1+rec(A[v][0]);
	return dp[v];
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	cin>>n;
	A.resize(n+1);
	type.resize(n+1);
	rep(i,1,n+1)cin>>type[i];
	rep(i,1, n+1){
		cin>>a;
		A[a].pb(i);
	}
	dp.assign(n+1, -1);
	for(int i=1; i<=n; i++){
		if(dp[i]==-1){
			rec(i);
		}
	}
	rep(i,1,n+1)maxi=max(maxi, dp[i]);
	cout<<maxi<<"\n";
	rep(i,1,n+1){
		if(maxi==dp[i]){
			int x=i;
			cout<<x<<" ";
			maxi--;
			while(maxi){
				x=A[x][0];
				cout<<x<<" ";
				maxi--;
			}
			return 0;
		}
	}
	cout<<maxi<<"\n";
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (int r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
#define f first
#define s second
const int MAX=1000*100+5;
int n, a, maxi=1, x;
vector<vector<int>>A;
vector<int>d, type, dp;
int rec(int v){
	if(d[v]>1)return 0;
	if(dp[v]>-1)return dp[v];
	int ans=0;
	for(int i:A[v]){
		ans=max(ans, rec(i)+1);
	}
	dp[v]=ans;
	return ans;
}
void print(int v){
	if(d[v]>1||(type[v]==1&&v!=x))return;
	if(A[v].size())
	print(A[v][0]);
	if(v>0)
	cout<<v<<" ";
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	cin>>n;
	A.resize(n+1);
	type.resize(n+1);
	dp.assign(n+1, -1);
	rep(i,1,n+1)cin>>type[i];
	d.assign(n+1, 0);
	rep(i,1, n+1){
		cin>>a;
		d[a]++;
		A[i].pb(a);
	}
	for(int i=1; i<=n; i++){
		if(type[i]==1&&d[i]<2){
			maxi=max(rec(i), maxi);
		}
	}
	cout<<maxi<<"\n";
	for(int i=1; i<=n; i++){
		if(dp[i]==maxi&&type[i]==1){
			x=i;
			print(i);
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (int r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
#define f first
#define s second
const int MAX=1000*100+5;
int n, a, maxi=0;
vector<vector<int>>A;
vector<int>dp, type;
int rec(int v){
	if(A[v].size()>1){
		dp[v]=INT_MIN/4;
		return dp[v];
	}
	if(dp[v]>-1)return dp[v];
	if(type[v]){
		dp[v]=1;
		return 1;
	}
	dp[v]=1+rec(A[v][0]);
	maxi=max(maxi, dp[v]);
	return dp[v];
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	cin>>n;
	A.resize(n+1);
	type.resize(n+1);
	rep(i,1,n+1)cin>>type[i];
	rep(i,1, n+1){
		cin>>a;
		A[a].pb(i);
	}
	dp.assign(n+1, -1);
	for(int i=1; i<=n; i++){
		if(dp[i]==-1){
			rec(i);
		}
	}
	cout<<maxi<<"\n";
	rep(i,1,n+1){
		if(maxi==dp[i]){
			int x=i;
			cout<<x<<" ";
			maxi--;
			while(maxi){
				x=A[x][0];
				cout<<x<<" ";
				maxi--;
			}
			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (int r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
#define f first
#define s second
const int MAX=1000*100+5;
int n, a, maxi=1;
vector<vector<int>>A;
vector<int>d, type, dp;
int rec(int v){
	if(d[v]>1)return 0;
	if(dp[v]>-1)return dp[v];
	int ans=0;
	for(int i:A[v]){
		ans=max(ans, rec(i)+1);
	}
	dp[v]=ans;
	return ans;
}
void print(int v){
	if(d[v]>1)return;
	if(A[v].size())
	print(A[v][0]);
	if(v>0)
	cout<<v<<" ";
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	cin>>n;
	A.resize(n+1);
	type.resize(n+1);
	dp.assign(n+1, -1);
	rep(i,1,n+1)cin>>type[i];
	d.assign(n+1, 0);
	rep(i,1, n+1){
		cin>>a;
		d[a]++;
		A[i].pb(a);
	}
	for(int i=1; i<=n; i++){
		if(type[i]==1&&d[i]<2){
			maxi=max(rec(i), maxi);
		}
	}
	cout<<maxi<<"\n";
	for(int i=1; i<=n; i++){
		if(dp[i]==maxi&&type[i]==1){
			print(i);
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
int n,tag[100001],ok[100001],pre[100001];
vector<vector<int>> graph,rgraph;
queue<int> q;

int main(){
// 	freopen("art.out", "w", stdout);
// 	freopen("art.in", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	memset(pre,-1,sizeof pre);
	cin >> n;
	graph.resize(n+1);
	rgraph.resize(n+1);
	for(int i = 1 ; i <= n ; i += 1){
		cin >> tag[i];
		if(tag[i]){
			q.push({i});
		}
	}
	for(int i = 1 ; i <= n ; i += 1){
		int a;
		cin >> a;
		graph[a].push_back(i);
		rgraph[i].push_back(a);
	}
	for(int i = 1 ; i <= n ; i += 1){
		if(graph[i].size() == 1){
			ok[i] = true;
		}
	}
	int ans,dist=0;
	while(q.size()){
		dist += 1;
		int f = q.size();
		while(f--){
			int next = q.front();
			ans = next;
			q.pop();
			ans = next;
			for(int i : rgraph[next]){
				if(ok[i]){
					q.push(i);
					pre[i] = next;
				}
			}
		}
	}
	cout << dist << endl;
	vector<int> v;
	while(ans != -1){
		v.push_back(ans);
		ans = pre[ans];
	}
	for(int i : v){
		cout << i << " ";
	}cout << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (int r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
#define f first
#define s second
const int MAX=1000*100+5;
int n, a, maxi=1, x;
vector<vector<int>>A;
vector<int>d, type, dp;
int rec(int v){
	if(d[v]>1)return 0;
	if(dp[v]>-1)return dp[v];
	int ans=0;
	for(int i:A[v]){
		ans=max(ans, rec(i)+1);
	}
	dp[v]=ans;
	return ans;
}
void print(int v){
	if(d[v]>1||(type[v]==1&&v!=x))return;
	if(A[v].size())
	print(A[v][0]);
	if(v>0)
	cout<<v<<" ";
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	cin>>n;
	A.resize(n+1);
	type.resize(n+1);
	dp.assign(n+1, -1);
	rep(i,1,n+1)cin>>type[i];
	d.assign(n+1, 0);
	rep(i,1, n+1){
		cin>>a;
		d[a]++;
		A[i].pb(a);
	}
	for(int i=1; i<=n; i++){
		if(type[i]==1&&d[i]<2){
			maxi=max(rec(i), maxi);
		}
	}
	cout<<maxi<<"\n";
	for(int i=1; i<=n; i++){
		if(dp[i]==maxi&&type[i]==1){
			x=i;
			// if(n==100000){
				// cout<<x<<" "<<maxi<<" \n";
			// }
			print(i);
			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (int r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
#define f first
#define s second
const int MAX=1000*100+5;
int n, a, maxi=0;
vector<vector<int>>A;
vector<int>dp, type, o;
int rec(int v){
	if(dp[v]>-1)return dp[v];
	if(A[v].size()>1){
		dp[v]=INT_MIN;
		return dp[v];
	}
	if(type[v]){
		dp[v]=1;
		return 1;
	}
	dp[v]=1+rec(A[v][0]);
	return dp[v];
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	cin>>n;
	A.resize(n+1);
	type.resize(n+1);
	o.resize(n+1);
	rep(i,1,n+1)cin>>type[i];
	rep(i,1, n+1){
		cin>>a;
		o[i]=a;
		A[a].pb(i);
	}
	dp.assign(n+1, 0);
	for(int i=n; i>=1; i--){
		if(type[i])dp[i]++;
		if(A[o[i]].size()==1){
			dp[o[i]]+=dp[i]+1;
		}
	}
	rep(i,1,n+1){
		maxi=max(maxi, dp[i]);
	}
	cout<<maxi<<"\n";
	rep(i,1,n+1){
		if(maxi==dp[i]){
			int x=i;
			cout<<x<<" ";
			maxi--;
			while(maxi){
				x=A[x][0];
				cout<<x<<" ";
				maxi--;
			}
			return 0;
		}
	}
	cout<<maxi<<"\n";
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (int r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
#define f first
#define s second
const int MAX=1000*100+5;
int n, a, maxi=1;
vector<vector<int>>A;
vector<int>d, type, dp;
int rec(int v){
	if(d[v]>1)return 0;
	if(dp[v]>-1)return dp[v];
	int ans=0;
	for(int i:A[v]){
		ans=max(ans, rec(i)+1);
	}
	dp[v]=ans;
	return ans;
}
void print(int v){
	if(d[v]>1)return;
	if(A[v].size())
	print(A[v][0]);
	if(v>0)
	cout<<v<<" ";
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	cin>>n;
	A.resize(n+1);
	type.resize(n+1);
	dp.assign(n+1, -1);
	rep(i,1,n+1)cin>>type[i];
	d.assign(n+1, 0);
	rep(i,1, n+1){
		cin>>a;
		d[a]++;
		A[i].pb(a);
	}
	for(int i=1; i<=n; i++){
		if(type[i]==1&&d[i]<2){
			maxi=max(rec(i), maxi);
		}
	}
	cout<<maxi<<"\n";
	for(int i=1; i<=n; i++){
		if(dp[i]==maxi&&type[i]==1){
			print(i);
			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (int r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
#define f first
#define s second
const int MAX=1000*100+5;
int n, a, maxi=1, x;
vector<vector<int>>A;
vector<int>d, type, dp;
int rec(int v){
	if(d[v]>1)return 0;
	if(dp[v]>-1)return dp[v];
	int ans=0;
	for(int i:A[v]){
		ans=max(ans, rec(i)+1);
	}
	dp[v]=ans;
	return ans;
}
void print(int v){
	if(d[v]>1||(type[v]==1&&v!=x))return;
	if(A[v].size())
	print(A[v][0]);
	if(v>0)
	cout<<v<<" ";
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	cin>>n;
	A.resize(n+1);
	type.resize(n+1);
	dp.assign(n+1, -1);
	rep(i,1,n+1)cin>>type[i];
	d.assign(n+1, 0);
	rep(i,1, n+1){
		cin>>a;
		d[a]++;
		A[i].pb(a);
	}
	for(int i=1; i<=n; i++){
		if(type[i]==1&&d[i]<2){
			maxi=max(rec(i), maxi);
		}
	}
	cout<<maxi<<"\n";
	for(int i=1; i<=n; i++){
		if(dp[i]==maxi&&type[i]==1){
			x=i;
			if(n==100000){
				cout<<x<<" "<<maxi<<" \n";
			}
			print(i);
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (int r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
#define f first
#define s second
const int MAX=1000*100+5;
int n, a, maxi=1;
vector<vector<int>>A;
vector<int>dp, type;
int rec(int v){
	if(A[v].size()>1){
		dp[v]=INT_MIN/4;
		return dp[v];
	}
	if(dp[v]>-1)return dp[v];
	if(type[v]){
		dp[v]=1;
		return 1;
	}
	dp[v]=1+rec(A[v][0]);
	maxi=max(maxi, dp[v]);
	return dp[v];
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	cin>>n;
	A.resize(n+1);
	type.resize(n+1);
	rep(i,1,n+1)cin>>type[i];
	rep(i,1, n+1){
		cin>>a;
		A[a].pb(i);
	}
	dp.assign(n+1, -1);
	for(int i=1; i<=n; i++){
		if(dp[i]==-1){
			rec(i);
		}
	}
	rep(i,1,n+1){
		dp[i]=max(1, dp[i]);
	}
	cout<<maxi<<"\n";
	rep(i,1,n+1){
		if(maxi==dp[i]){
			int x=i;
			cout<<x<<" ";
			maxi--;
			while(maxi){
				x=A[x][0];
				cout<<x<<" ";
				maxi--;
			}
			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (int r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
#define f first
#define s second
const int MAX=1000*100+5;
int n, a, maxi=0;
vector<vector<int>>A;
vector<int>dp, type, o;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	cin>>n;
	A.resize(n+1);
	type.resize(n+1);
	o.resize(n+1);
	rep(i,1,n+1)cin>>type[i];
	rep(i,1, n+1){
		cin>>a;
		o[i]=a;
		A[a].pb(i);
	}
	dp.assign(n+1, 0);
	for(int i=n; i>=1; i--){
		if(A[i].size()>1)continue;
		if(type[i])dp[i]++;
		if(A[o[i]].size()==1&&type[o[i]]==0){
			dp[o[i]]+=dp[i]+1;
		}
	}
	rep(i,1,n+1){
		maxi=max(maxi, dp[i]);
	}
	cout<<maxi<<"\n";
	rep(i,1,n+1){
		if(maxi==dp[i]){
			int x=i;
			cout<<x<<" ";
			maxi--;
			while(maxi){
				x=A[x][0];
				cout<<x<<" ";
				maxi--;
			}
			return 0;
		}
	}
	cout<<maxi<<"\n";
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (int r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
#define f first
#define s second
const int MAX=1000*100+5;
int n, a, maxi=0;
vector<vector<int>>A;
vector<int>dp, type, o;
int rec(int v){
	if(dp[v]>-1)return dp[v];
	if(A[v].size()>1){
		dp[v]=INT_MIN;
		return dp[v];
	}
	if(type[v]){
		dp[v]=1;
		return 1;
	}
	dp[v]=1+rec(A[v][0]);
	return dp[v];
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	cin>>n;
	A.resize(n+1);
	type.resize(n+1);
	o.resize(n+1);
	rep(i,1,n+1)cin>>type[i];
	rep(i,1, n+1){
		cin>>a;
		o[i]=a;
		A[a].pb(i);
	}
	dp.assign(n+1, 0);
	for(int i=n; i>=1; i--){
		if(A[i].size()>1)continue;
		if(type[i])dp[i]++;
		if(A[o[i]].size()==1){
			dp[o[i]]+=dp[i]+1;
		}
	}
	rep(i,1,n+1){
		maxi=max(maxi, dp[i]);
	}
	cout<<maxi<<"\n";
	rep(i,1,n+1){
		if(maxi==dp[i]){
			int x=i;
			cout<<x<<" ";
			maxi--;
			while(maxi){
				x=A[x][0];
				cout<<x<<" ";
				maxi--;
			}
			return 0;
		}
	}
	cout<<maxi<<"\n";
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (int r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
#define f first
#define s second
const int MAX=1000*100+5;
int n, a, maxi=1;
vector<vector<int>>A;
vector<int>dp, type;
int rec(int v){
	if(A[v].size()>1){
		dp[v]=-n;
		return dp[v];
	}
	if(dp[v]>-1)return dp[v];
	if(type[v]){
		dp[v]=1;
		return 1;
	}
	dp[v]=1+rec(A[v][0]);
	maxi=max(maxi, dp[v]);
	return dp[v];
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	cin>>n;
	A.resize(n+1);
	type.resize(n+1);
	rep(i,1,n+1)cin>>type[i];
	rep(i,1, n+1){
		cin>>a;
		A[a].pb(i);
	}
	dp.assign(n+1, -1);
	for(int i=1; i<=n; i++){
		if(dp[i]==-1){
			rec(i);
		}
	}
	rep(i,1,n+1){
		dp[i]=max(1, dp[i]);
	}
	cout<<maxi<<"\n";
	rep(i,1,n+1){
		if(maxi==dp[i]){
			int x=i;
			cout<<x<<" ";
			maxi--;
			while(maxi){
				x=A[x][0];
				cout<<x<<" ";
				maxi--;
			}
			return 0;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (int r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
#define mp(a, b) make_pair(a, b)
#define f first
#define s second
const int MAX=1000*100+5;
int n, a, maxi=0;
vector<vector<int>>A;
vector<int>dp, type;
int rec(int v){
	if(dp[v]>-1)return dp[v];
	if(A[v].size()>1){
		dp[v]=INT_MIN/4;
		return dp[v];
	}
	if(type[v]){
		dp[v]=1;
		return 1;
	}
	dp[v]=1+rec(A[v][0]);
	return dp[v];
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	cin>>n;
	A.resize(n+1);
	type.resize(n+1);
	rep(i,1,n+1)cin>>type[i];
	rep(i,1, n+1){
		cin>>a;
		A[a].pb(i);
	}
	dp.assign(n+1, -1);
	for(int i=1; i<=n; i++){
		if(dp[i]==-1){
			rec(i);
		}
	}
	rep(i,1,n+1)maxi=max(maxi, dp[i]);
	cout<<maxi<<"\n";
	rep(i,1,n+1){
		if(maxi==dp[i]){
			int x=i;
			cout<<x<<" ";
			maxi--;
			while(maxi){
				x=A[x][0];
				cout<<x<<" ";
				maxi--;
			}
			return 0;
		}
	}
	cout<<maxi<<"\n";
}


#include <bits/stdc++.h>

#define ll long long
#define FOR(i, a, b) for (int i = a; i<=b; i++)
#define FORD(i, a, b) for (int i = a; i>=b; i--)
#define ii pair<int,int>
#define vii vector<ii>
#define mp(a,b) make_pair(a,b)
#define task name

using namespace std;

const int oo = 1e9+7;
const int mod = 1e9+7;

int n, type[100001], cnt[100001], pa[100001], a, pos, MAX = 0;

vector<int> p[100001];

void dfs(int u)
{
    int goc=u;
    p[u].push_back(u);
    while(cnt[pa[u]]==1 && pa[u]!=0)
    {
        p[goc].push_back(pa[u]);
        u=pa[u];
    }
    if (p[goc].size()>MAX)
    {
        MAX=p[goc].size();
        pos=goc;
    }
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//freopen(task".inp", "r", stdin);
	//freopen(task".out", "w", stdout);
	cin >> n;
	FOR(i,1,n)
	{
	    cin>>type[i];
	}
	int a;
	FOR(i,1,n)
	{
	    cin >> pa[i];
	    cnt[pa[i]]++;
	}
	FOR(i,1,n) if (type[i])
    {
        dfs(i);
    }
    cout << MAX << '\n';
    FORD(i,MAX-1,0)
    {
        cout << p[pos][i] << " ";
    }
}


#include<bits/stdc++.h>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<iostream>
#define ios   ios_base::sync_with_stdio(false);  cin.tie(0);    cout.tie(0);
typedef  long long int ll;
#define ld long double
const int M=1e9+7;
const int N=1e5+5;
#define fi first
#define se second
#define pb push_back
#define endl "\n"
using namespace std;
ll out[N],in[N];
ll type[N],a[N];
vector<ll> v[N];
bool vis[N];
int main()
{
   ll n;cin>>n;
   for(ll i=1;i<=n;++i)
   cin>>type[i];
    for(ll i=1;i<=n;++i)
    {
        cin>>a[i];
        if(a[i]!=0)
        {
        //    v[a[i]].pb(i);
            ++out[a[i]];
            ++in[i];
        }
    }
    ll val1=0;
    stack<ll> ans;
    for(ll i=1;i<=n;++i)
    {
        ll x=i,val=0;
        stack<ll> s;
        if(!vis[x] && type[x] && in[x]<=1 && out[x]==0)
        {
            ++val;
            s.push(x);
            vis[x]=1;
            x=a[x];
            while(!vis[x] && !type[x] && out[x]+in[x]<=2 && x)
            {
                  ++val;
                  s.push(x);
                  vis[x]=1;
                  x=a[x];
            }
        }
        if(val1<val)
        {
            val1=val;
            ans=s;
        }
    }
    cout<<val1<<endl;
    while(!ans.empty())
    {
        cout<<ans.top()<<" ";
        ans.pop();
    }
}


#include<bits/stdc++.h>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<iostream>
#define ios   ios_base::sync_with_stdio(false);  cin.tie(0);    cout.tie(0);
typedef  long long int ll;
#define ld long double
const int M=1e9+7;
const int N=1e5+5;
#define fi first
#define se second
#define pb push_back
#define endl "\n"
using namespace std;
ll out[N],in[N];
ll type[N],a[N];
vector<ll> v[N];
bool vis[N];
int main()
{
   ll n;cin>>n;
   for(ll i=1;i<=n;++i)
   cin>>type[i];
    for(ll i=1;i<=n;++i)
    {
        cin>>a[i];
        if(a[i]!=0)
        {
            v[a[i]].pb(i);
            ++out[a[i]];
            ++in[i];
        }
    }
    ll val1=0;
    stack<ll> ans;
    for(ll i=1;i<=n;++i)
    {
        ll x=i,val=0;
        stack<ll> s;
        if(!vis[x] && type[x] && in[x]==1 && out[x]==0)
        {
            ++val;
            s.push(x);
            x=a[x];
            while(!vis[x] && !type[x] && out[x]+in[x]<=2 && x)
            {
                  ++val;
                  s.push(x);
                  vis[x]=1;
                  x=a[x];
            }
        }
        if(val1<val)
        {
            val1=val;
            ans=s;
        }
    }
    cout<<val1<<endl;
    while(!ans.empty())
    {
        cout<<ans.top()<<" ";
        ans.pop();
    }
}


#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
	// freopen("input.txt", "r", stdin);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	vector<int> type(n);
	for (int& i : type) {
		cin >> i;
	}
	vector<vector<int>> g(n);
	vector<int> deg(n);
	for (int i = 0; i < n; i++) {
		int a;
		cin >> a;
		if (a) {
			g[i].push_back(a - 1);
			deg[a - 1]++;
		}
	}
	vector<int> dist(n, 1e9);
	vector<int> way(n, -1);
	for (int i = 0; i < n; i++) {
		if (type[i]) {
			dist[i] = 0;
			queue<int> q;
			q.push(i);
			while (!q.empty()) {
				int v = q.front();
				q.pop();
				for (int u : g[v]) {
					if (deg[u] > 1) {
						continue;
					}
					if (dist[v] + 1 < dist[u]) {
						dist[u] = dist[v] + 1;
						way[u] = v;
						q.push(u);
					}
				}
			}
		}
	}
	int ans = -1;
	int ind;
	for (int i = 0; i < n; i++) {
		if (dist[i] != 1e9 && dist[i] > ans) {
			ans = dist[i];
			ind = i;
		}
	}
	cout << ans + 1 << endl;
	while (ind >= 0) {
		cout << ind + 1 << ' ';
		ind = way[ind];
	}
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long

ll n,x,mx=0,res;
ll indeg[100007];
ll nbor[100007];
vector<ll> hotels;

int main()
{
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>x;
        if(x==1) hotels.push_back(i);
    }
    for(int i=1; i<=n; i++)
    {
        cin>>x;
        if(x)
            nbor[i] = x;
        indeg[x]++;
    }
    ll nod;
    for(int i=0; i<hotels.size(); i++)
    {
        nod = hotels[i];
        int cnt = 1;

        while(1)
        {
            nod = nbor[nod];
            if(nod==0 || indeg[nod]!=1) break;
            else cnt++;
        }
        if(cnt>mx) mx = cnt, res = hotels[i];//, cout << res <<  " " <<  cnt << endl;;
    }

    nod = res;
    vector<int> ans;
    ans.push_back(res);

    while(1)
    {
         nod = nbor[nod];
        if(nod==0 || indeg[nod]!=1) break;
        else ans.push_back(nod);

//          //  cout<<"hi"<<endl;
//        if(prev==ans.size()) break;
//        if(indeg[nod]!=1) {ans.erase(ans.begin()+ans.size()-1); break;}
    }
    cout << ans.size() << endl;
    for(int i=ans.size()-1; i>=0; i--)
    {
        cout << ans[i] << " " ;
    }
    puts("");

    return 0;
}

/*

9
0 0 0 1 1 0 1 0 1
3 1

6
0 0 0 0 1 1
0 1 2 3 4 0

5
0 0 1 0 1
0 1 2 2 4

4
1 0 0 0
2 3 4 2
*/


#include <bits/stdc++.h>
using namespace std;

#define ll long long

ll n,x,mx=0,res;
ll indeg[100007];
vector<ll> nbor[100007], hotels;

int main()
{
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>x;
        if(x==1) hotels.push_back(i);
    }
    for(int i=1; i<=n; i++)
    {
        cin>>x;
        if(x)
            nbor[i].push_back(x);
        indeg[x]++;
    }
    ll nod;
    for(int i=0; i<hotels.size(); i++)
    {
        nod = hotels[i];
        int cnt = 0;
        while(1)
        {
            int prev = cnt;
            if(nbor[nod].size()>=1)
            {
                nod = nbor[nod][0];
                cnt++;
            }
            if(cnt==prev or indeg[nod]!=1) break;

        }
        if(cnt>mx) mx = cnt, res = hotels[i];//, cout << res <<  " " <<  cnt << endl;;
    }

    nod = res;
    vector<int> ans;
    ans.push_back(res);

    while(1)
    {
        int prev = ans.size();
        if(nbor[nod].size()>=1)
        {
            nod = nbor[nod][0];
            ans.push_back(nod);
        }
          //  cout<<"hi"<<endl;
        if(prev==ans.size()) break;
        if(indeg[nod]!=1) {ans.erase(ans.begin()+ans.size()-1); break;}
    }
    cout << ans.size() << endl;
    for(int i=ans.size()-1; i>=0; i--)
    {
        cout << ans[i] << " " ;
    }
    puts("");

    return 0;
}

/*

9
0 0 0 1 1 0 1 0 1
3 1

6
0 0 0 0 1 1
0 1 2 3 4 0

5
0 0 1 0 1
0 1 2 2 4

4
1 0 0 0
2 3 4 2
*/


#include <bits/stdc++.h>
using namespace std;
int n , dad[100005]={0} , out[100005]={0};
bool t[100005]={0};
int main()
{
    cin>>n;
    for(int i=1; i<=n; i++)
        cin>>t[i];
    for(int i=1; i<=n; i++)
    {
        int a;
        cin>>a;
        // a ->>> I
        if(a!=0)
        out[a]++;
        dad[i]=a; // reverse the connection
    }
    /*for(int i=1; i<=n; i++)
        cout<<out[i]<<" ";
    cout<< endl;*/

    int res=0 , s=-1;
    for(int i=1; i<=n; i++)
    {
        int tmp=0;
        if(t[i]==1)
        {
            int j=i , c=1;
            tmp=i;
            while(1)
            {
                //cout<<j<< endl;
                j=dad[j];  // go back
                //cout<<j<< endl;
                //cout<<out[j]<< endl;
                // does this object is valid
                if(out[j]>1)
                    break;
                if(dad[j]==0)
                    break;
                c++;
            }

            if(c>res){
                res=c;
                s=tmp;
            }
        }
    }

    deque<int> path;
    path.push_front(s);
    while(dad[s]!=0)
    {
        s=dad[s];
        if(out[s]>1)
        break;
        path.push_front(s);
    }

    cout<<res<< endl;
    for(int i=0; i<path.size(); i++)
    cout<<path[i]<<" ";
    cout<< endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n , dad[100005]={0} , out[100005]={0};
bool t[100005]={0};
int main()
{
    cin>>n;
    for(int i=1; i<=n; i++)
        cin>>t[i];
    for(int i=1; i<=n; i++)
    {
        int a;
        cin>>a;
        // a ->>> I
        if(a!=0)
        out[a]++;
        dad[i]=a; // reverse the connection
    }
    /*for(int i=1; i<=n; i++)
        cout<<out[i]<<" ";
    cout<< endl;*/

    int res=0 , s=-1;
    for(int i=1; i<=n; i++)
    {
        int tmp=0;
        if(t[i]==1)
        {
            int j=i , c=1;
            tmp=i;
            while(1)
            {
                //cout<<j<< endl;
                j=dad[j];  // go back
                c++;
                //cout<<j<< endl;
                //cout<<out[j]<< endl;
                if(out[j]>1){
                    c--;
                    break;
                }
                if(dad[j]==0)
                    break;
            }

            if(c>res){
                res=c;
                s=tmp;
            }
        }
    }

    deque<int> path;
    path.push_front(s);
    while(dad[s]!=0)
    {
        s=dad[s];
        if(out[s]>1)
        break;
        path.push_front(s);
    }

    cout<<res<< endl;
    for(int i=0; i<path.size(); i++)
    cout<<path[i]<<" ";
    cout<< endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n , dad[100005]={0} , out[100005]={0};
bool t[100005]={0};
int main()
{
    cin>>n;
    for(int i=1; i<=n; i++)
        cin>>t[i];
    for(int i=1; i<=n; i++)
    {
        int a;
        cin>>a;
        // a ->>> I
        if(a!=0)
        out[a]++;
        dad[i]=a; // reverse the connection
    }
    /*for(int i=1; i<=n; i++)
        cout<<out[i]<<" ";
    cout<< endl;*/

    int res=0 , s=-1;
    for(int i=1; i<=n; i++)
    {
        int tmp=0;
        if(t[i]==1)
        {
            int j=i , c=1;
            tmp=i;
            while(1)
            {
                //cout<<j<< endl;
                j=dad[j];  // go back
                //cout<<j<< endl;
                //cout<<out[j]<< endl;
                // does this object is valid
                if(out[j]>1)
                    break;
                if(j!=0)
                c++;
                if(dad[j]==0)
                    break;
            }

            if(c>res){
                res=c;
                s=tmp;
            }
        }
    }

    deque<int> path;
    path.push_front(s);
    while(dad[s]!=0)
    {
        s=dad[s];
        if(out[s]>1)
        break;
        path.push_front(s);
    }

    cout<<res<< endl;
    for(int i=0; i<path.size(); i++)
    cout<<path[i]<<" ";
    cout<< endl;
    return 0;
}


//g++  5.4.0

#include <bits/stdc++.h>
using namespace std;
#define ll long long

const ll N = 1e5+5;
vector<ll> g[N];
vector<ll> rg[N];
vector<ll> temp;
vector<ll> a(N);

void dfs(ll v)
{

    if(a[v] == 1)
    {
        dfs(g[v][0]);
        temp.push_back(v);
    }
    else if( rg[v].size() == 1)
    {
        if(g[v].size() == 1)
            dfs(g[v][0]);
        temp.push_back(v);
    }

    return;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    ll n;
    cin >> n;
    for(ll i = 1; i <= n ; i++ ) cin >> a[i];
    for(ll i = 1; i <= n ; i++ )
    {
        ll x;
        cin >> x;
        if(x)
        {
              g[i].push_back(x);
              rg[x].push_back(i);
        }
    }
    ll idx = -1;
    ll maxi = 0;
    for(ll i = 1; i <= n ; i++ )
    {
        if(a[i] == 1)
        {
            temp.clear();
            dfs(i);

            if((ll)temp.size() > maxi)
            {
                maxi = (ll)temp.size();
                idx = i;
            }
        }
    }
    temp.clear();
    dfs(idx);
    ll l = (ll)temp.size();
    cout << l << "\n";

    for(ll i = 0 ; i < l ; i++)
        cout << temp[i] << " ";
}


//g++  5.4.0

#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    ll n;
    cin >> n;
    vector<ll> h(n+5) ;
    vector<ll> deg(n+5,0) , a(n+5,0);
    for(ll i = 1; i <= n ; i++ ) cin >> h[i];
    for(ll i = 1; i <= n ; i++ ) cin >> a[i], deg[a[i]]++;
    ll c = -1;
    ll maxi = 0;
    for(ll i = 1; i <= n ; i++ )
    {
        if(h[i] == 1)
        {
            ll x = a[i];
            ll cnt = 1;
            while( x > 0 && deg[x] == 1)
                cnt++ , x = a[x];
            if(cnt > maxi)
            {
                maxi = cnt;
                c = i;
            }
        }
    }
    vector<ll> temp;
    temp.push_back(c);
    c = a[c];
    while(c > 0 && deg[c] == 1)
        temp.push_back(c) , c = a[c];
    ll l = (ll)temp.size();
    cout << l << "\n";

    for(ll i = l-1 ; i >= 0 ; i--)
        cout << temp[i] << " ";
}


#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
int n;
int a[maxn],b[maxn],c[maxn],p[maxn];
bool vis[maxn];
int main()
{
	cin>>n;
	memset(c,0,sizeof(c));
	memset(vis,false,sizeof(vis));
	for(int i=0;i<maxn;i++)
	   p[i]=i;

	for(int i=1;i<=n;i++)
	   cin>>a[i];
	for(int i=1;i<=n;i++)
	{
	   cin>>b[i];
	   c[b[i]]++;//记录出度
	}
	for(int i=1;i<=n;i++)
	{
	 	if(c[b[i]]<=1)
	 	{
	 		p[b[i]]=i;//记录满足的节点
		}
	}
	int dx=0,dc=1;
	for(int i=1;i<=n;i++)
	{
		if(vis[i]) continue;
		if(c[i]<=1)
		{
			int t=i,z=1;
			while(p[t]!=t&& !vis[i])
			{
				if(c[t]>1)break;
				z++,t=p[t];
				vis[t]=true;
			}
			if(a[t]==1&&z>dx)
			   dx=max(dx,z),dc=i;
		}
		vis[i]=true;
	}
	cout<<dx<<endl;
	while(p[dc]!=dc)
	{
		cout<<dc<<" ";
		dc=p[dc];
	}
	cout<<dc<<endl;
	  return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
int n;
int a[maxn],b[maxn],c[maxn],p[maxn];
bool vis[maxn];
int main()
{
	cin>>n;

	memset(c,0,sizeof(c));
	memset(vis,false,sizeof(vis));
	for(int i=0;i<maxn;i++)
	   p[i]=i;

	for(int i=1;i<=n;i++)
	   cin>>a[i];
	for(int i=1;i<=n;i++)
	{
	   cin>>b[i];
	   c[b[i]]++;//记录出度
	}
	for(int i=1;i<=n;i++)
	{
	 	if(c[b[i]<=1])
	 	{
	 		p[b[i]]=i;//记录满足的节点
		}
	}
	int x=0,d=1;
	for(int i=1;i<=n;i++)
	{
		if(vis[i]) continue;
		if(c[i]<=1)
		{
			int t=i,k=1;
			while(p[t]!=t&&!vis[i])
			{
				if(c[t]>1)break;
				k++,t=p[t];
				vis[t]=true;
			}
			if(a[t]==1&&k>x)
			   x=max(x,k),d=i;
		}
		vis[i]=true;
	}
	cout<<x<<endl;
	while(p[d]!=d)
	{
		cout<<d<<" ";
		d=p[d];
	}
	cout<<d<<endl;
	  return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef pair<db,db> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define sci(n) scanf("%d",&n)
#define scii(n,m) scanf("%d%d",&n,&m)
#define scl(n) scanf("%lld",&n)
#define scll(n,m) scanf("%lld%lld",&n,&m)

#define xx first
#define yy second
#define READ  freopen("input.txt","r",stdin)
#define WRITE freopen("output.txt","w",stdout);
#define pi acos(-1)
#define SQR(a) (a)*(a)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define pri priority_queue<int>
#define rev_pri priority_queue<int,vector<int>,greater<int> >
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod 1000000007
#define inf INT_MAX
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define snuke(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)

const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
//ll ncr(ll n,ll r){
//    if(n==r)return 1;
//    if(r==1)return n;
//    if(dp[n][r]!=-1)return dp[n][r];
//    return dp[n][r]=ncr(n-1,r)+ncr(n-1,r-1);
//}

//bool checkbit(int mask,int bit){
//    return mask & (1<<bit);
//}
//int setbit(int mask,int bit){
//    return mask | (1<<bit) ;
//}
int clearbit(int mask,int bit){
    return mask & ~(1<<bit);
}
int togglebit(int mask,int bit){
    return mask ^ (1<<bit);
}
int topbit(signed t){
	return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
	return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
	return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
	return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
	return __builtin_popcount(t);
}
int popcount(ll t){
	return __builtin_popcountll(t);
}
bool ispow2(int i){
	return i&&(i&-i)==i;
}

#define CAPITAL
void yes(bool ex=true){
	#ifdef CAPITAL
	cout<<"YES"<<"\n";
	#else
	cout<<"Yes"<<"\n";
	#endif
	if(ex)exit(0);
}
void no(bool ex=true){
	#ifdef CAPITAL
	cout<<"NO"<<"\n";
	#else
	cout<<"No"<<"\n";
	#endif
	if(ex)exit(0);
}
void possible(bool ex=true){
	#ifdef CAPITAL
	cout<<"POSSIBLE"<<"\n";
	#else
	cout<<"Possible"<<"\n";
	#endif
	if(ex)exit(0);
}
void impossible(bool ex=true){
	#ifdef CAPITAL
	cout<<"IMPOSSIBLE"<<"\n";
	#else
	cout<<"Impossible"<<"\n";
	#endif
	if(ex)exit(0);
}
template<typename T> bool checkbit(T mask, T bit){
    return mask & (((T)1)<<bit);
}
template<typename T> T setbit(T mask, T bit){
    return mask | (((T)1)<<bit);
}

template <typename T> T BigMod (T b,T p,T m){
    if (p == 0) return 1;if (p%2 == 0){T s = BigMod(b,p/2,m);return ((s%m)*(s%m))%m;}return ((b%m)*(BigMod(b,p-1,m)%m))%m;
}
template <typename T> T ModInv (T b,T m){
    return BigMod(b,m-2,m);
}
template <typename T> T POW(T B,T P){
    if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));
}
template <typename T> T Bigmod(T b,T p,T m){
    if(p==0) return 1; else if (!(p&1)) return SQR(Bigmod(b,p/2,m)) % m;else return ((b % m) * Bigmod(b,p-1,m)) % m;
}
template <typename T> T Dis(T x1,T y1,T x2, T y2){
    return sqrt( SQR(x1-x2) + SQR(y1-y2) );
}
template <typename T> T Angle(T x1,T y1,T x2, T y2){
    return atan( double(y1-y2) / double(x1-x2));
}
template <typename T> T DIFF(T a,T b) {
    T d = a-b;if(d<0)return -d;else return d;
}
template <typename T> T ABS(T a) {
    if(a<0)return -a;else return a;
}
template <typename T> T euclide(T a,T b,T &x,T &y) {
    if(a<0){T d=euclide(-a,b,x,y);x=-x;return d;}
    if(b<0){T d=euclide(a,-b,x,y);y=-y;return d;}
    if(b==0){x=1;y=0;return a;}
    else{T d=euclide(b,a%b,x,y);T t=x;x=y;y=t-(a/b)*y;return d;}
}
template <typename T> T gcd(T a,T b){
    if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);
}
template <typename T> T lcm(T a,T b) {
    if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));
}

template<class t>
int lwb(const vector<t>&v,const t&a){
	return lower_bound(all(v),a)-v.begin();
}

template<typename A, typename B>
class Dijkstra {
public:
    vector<vector<pair<int, A>>> vertexs;
    vector<int>par;
    B Cost_Function;
    Dijkstra(int n, B cost) : Cost_Function(cost) {
        vertexs = vector<vector<pair<int, A>>>(n, vector<pair<int, A>>{});
        par = vector<int>(n);
    }
    ~Dijkstra() {
        vertexs.clear();
    }
    void add_edge(int a, int b, A c) {
        vertexs[a].push_back(make_pair(b, c));
    }
    void clr(){par.clear(); }
    vector<ll> build_result(int StartPoint) {
        vector<ll> dist(vertexs.size(), 2e18);
        dist[StartPoint] = 0;
        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> next;
        next.push(make_pair(0, StartPoint));
        while (next.empty() == false) {
            pair<ll, int> now = next.top();
            next.pop();
            if (dist[now.second] != now.first) continue;
            for (auto x : vertexs[now.second]) {
                ll now_cost = now.first + x.second;// Cost_Function(x.second);
                if (dist[x.first] > now_cost) {
                    dist[x.first] = now_cost;
                    par[x.first] = now.second;
                    next.push(make_pair(now_cost, x.first));
                }
            }
        }
        return dist;
    }
};

string to_string(char c) {
	return string(1, c);
}
string to_string(bool b) {
	return b?"true":"false";
}
string to_string(const char* s) {
	return string(s);
}
string to_string(string s) {
	return s;
}

void DBG() {
	cerr << "]" << endl;
}
template<class H, class... T> void DBG(H h, T... t) {
	cerr << to_string(h);
	if(sizeof...(t))
		cerr << ", ";
	DBG(t...);
}
#define _DEBUG
#ifdef _DEBUG
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif

const ll MOD = (ll)mod;//998244353;
ll domd(ll x) {
    return x - (MOD*(x/MOD));
}
ll add(ll x, ll y) {
	x += y;
	if (x >= MOD) return x - MOD;
	return x;
}
ll sub(ll x, ll y) {
	x -= y;
	if (x < 0) return x + MOD;
	return x;
}
ll mult(ll x, ll y) {
	return (x * y) % MOD;
}
ll bin_pow(ll x, ll p) {
	if (p == 0) return 1;
	if (p & 1) return mult(x, bin_pow(x, p - 1));
	return bin_pow(mult(x, x), p / 2);
}
ll rev(ll x) {
	return bin_pow(x, MOD - 2);
}

//#define oj.txt
const int nax=100050;
int n,w,m;
int ar[nax],br[nax],ln[nax];
int mn,cn,par[nax];
vi v[nax],vfr[nax];
bool vz[nax];
int dfs(int sr)
{
    vz[sr]=1;
    if(ln[sr]!=-1) return ln[sr];
    int tot=0,tm;
    for(int i=0;i<vfr[sr].size();i++){
        if(!vz[vfr[sr][i]]&&v[vfr[sr][i]].size()==1&&vfr[sr][i]!=0){
            tm=(dfs(vfr[sr][i]));
            if(tm==-1){continue;}
            if(tm+1>tot){par[sr]=vfr[sr][i];tot=tm+1; }
        }
    }
    if(tot==0){ln[sr]=0;par[sr]=sr;return 0; }
    ln[sr]=tot;
    return tot;
}
void solve()
{
    int a,b,c,q,j,k,l,mx=0,i;
    int hn,hm,gc,sx,sy,ex=1,ey=-1;
    sci(n);
    FOR(i,1,n+1) sci(ar[i]),ln[i]=-1;
    FOR(i,1,n+1){
        sci(br[i]);
        vfr[i].pb(br[i]);
        //vfr[br[i]].pb(i);
        v[br[i]].pb(i);
    }
    FOR(i,n,n+5) ln[i]=-1;
    FOR(i,1,n+1) if(ar[i]){
        a=dfs(i);
        if(w<a+1){sx=i;w=a+1;}
    }cout<<w<<'\n';vi an;
    if(sx==par[sx]){cout<<sx<<'\n';return;}
    an.pb(sx);
    do{
        an.pb(par[sx]);
        sx=par[sx];
    }while(par[sx]!=sx);
    for(i=an.size()-1;i>=0;i--){cout<<an[i]<<' ';}
    cout<<'\n';
}
int main()
{
    //IOS;
    #ifdef oj.txt
    READ;
    WRITE;
    #endif // oj
    clock_t tStart = clock();
    ll t=1,mex=1;
   //cin>>t;
    while(t--){
        solve();
    }
    //fprintf(stderr, "Time = %.10fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> graph,rgraph;
int n,tag[100001],ok[100001];
queue<vector<int>> q;

int main(){
	//freopen("art.out", "w", stdout);
	//freopen("art.in", "r", stdin);
	cin >> n;
	graph.resize(n+1);
	rgraph.resize(n+1);
	for(int i = 1 ; i <= n ; i += 1){
		cin >> tag[i];
		if(tag[i]){
			q.push({i});
		}
	}
	for(int i = 1 ; i <= n ; i += 1){
		int a;
		cin >> a;
		graph[a].push_back(i);
		rgraph[i].push_back(a);
	}
	for(int i = 1 ; i <= n ; i += 1){
		if(graph[i].size() == 1){
			ok[i] = true;
		}
	}
	vector<int> ans;
	while(q.size()){
		vector<int> next = q.front();
		q.pop();
		ans = next;
		for(int i : rgraph[next.back()]){
			if(ok[i]){
				vector<int> v = next;
				v.push_back(i);
				q.push(v);
			}
		}
	}
	cout << ans.size() << endl;
	for(int i = ans.size()-1 ; i >= 0 ; i -= 1){
		cout << ans[i] << " ";
	}cout << endl;
}


#include <bits/stdc++.h>
using namespace std;
int n,tag[100001],ok[100001],pre[100001];
vector<vector<int>> graph,rgraph;
queue<int> q;

int main(){
	//freopen("art.out", "w", stdout);
	//freopen("art.in", "r", stdin);
	memset(pre,-1,sizeof pre);
	cin >> n;
	graph.resize(n+1);
	rgraph.resize(n+1);
	for(int i = 1 ; i <= n ; i += 1){
		cin >> tag[i];
		if(tag[i]){
			q.push({i});
		}
	}
	for(int i = 1 ; i <= n ; i += 1){
		int a;
		cin >> a;
		graph[a].push_back(i);
		rgraph[i].push_back(a);
	}
	for(int i = 1 ; i <= n ; i += 1){
		if(graph[i].size() == 1){
			ok[i] = true;
		}
	}
	int ans,dist=0;
	while(q.size()){
		dist += 1;
		int f = q.size();
		while(f--){
			int next = q.front();
			ans = next;
			q.pop();
			ans = next;
			for(int i : rgraph[next]){
				if(ok[i]){
					q.push(i);
					pre[i] = next;
				}
			}
		}
	}
	cout << dist << endl;
	vector<int> v;
	while(ans != -1){
		v.push_back(ans);
		ans = pre[ans];
	}
	for(int i : v){
		cout << i << " ";
	}cout << endl;
}


#include<bits/stdc++.h>

using namespace std;
int t[100007];
vector<int> g[100007];
vector<int> g1[100007];
int d[100007];
int p[100007];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for(int i=0; i<n; i++){
        cin >> t[i];
    }
    for(int i=0; i<n; i++){
        int a;
        cin >> a;
        if(a != 0){
            g[a-1].push_back(i);
        }
    }
    vector<int> nn(0);
    for(int i=0; i<n; i++){
        if(g[i].size() > 1){
            g[i] = nn;
        }
    }
    for(int i=0; i<n; i++){
        for(int j=0; j<g[i].size(); j++){
            g1[g[i][j]].push_back(i);
        }
    }
    queue<int> q;
    for(int i=0; i<n; i++){
        if(t[i] == 1){
            q.push(i);
            d[i]=1;
            p[i]=-1;
            while(!q.empty()){
                int v = q.front();
                q.pop();
                for(int j=0; j<g1[v].size(); j++){
                    d[g1[v][j]] = d[v] + 1;
                    q.push(g1[v][j]);
                    p[g1[v][j]] = v;
                }
            }
        }
    }
    int m = 0;
    int ans = -1;
    for(int i=0; i<n; i++){
        if(m < d[i]){
            m = d[i];
            ans = i;
        }
    }
    cout << m << endl;
    while(p[ans] != -1){
        cout << ans + 1 << ' ';
        ans = p[ans];
    }
    cout << ans + 1 << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;

vector<vi> adj, revadj;
vi res, path;
int vis[100005], v[100005];

void solve(int u){
    vis[u] = 1;
    path.push_back(u);
    for(int v : adj[u])
        if(!vis[v] and revadj[v].size() == 1)
            solve(v);
}

int main(){
    int n; cin >> n;
    adj.assign(n+5, vi());
    revadj.assign(n+5, vi());

    for(int i = 1; i <= n; i++)
        cin >> v[i];
    for(int i = 1; i <= n; i++){
        int x; cin >> x;
        if(x != 0){
            adj[i].push_back(x);
            revadj[x].push_back(i);
        }
    }

    for(int i = 1; i <= n; i++)
        if(v[i]){
            path.clear();
            solve(i);
            if(path.size() > res.size())
                res = path;
        }
    reverse(res.begin(), res.end());
    cout << res.size() << endl;
    for(int val : res)
        cout << val << " ";
    cout << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;

vector<vi> adj, revadj;
vi res, path;
int vis[100005], v[100005];

void solve(int u){
    vis[u] = 1;
    path.push_back(u);
    for(int v : adj[u])
        if(!vis[v] and revadj[v].size() == 1)
            solve(v);
}

int main(){
    int n; cin >> n;
    adj.assign(n+5, vi());
    revadj.assign(n+5, vi());

    for(int i = 1; i <= n; i++)
        cin >> v[i];
    for(int i = 1; i <= n; i++){
        int x; cin >> x;
        if(x != 0){
            adj[i].push_back(x);
            revadj[x].push_back(i);
        }
    }

    for(int i = 1; i <= n; i++)
        if(v[i]){
            path.clear();
            solve(i);
            if(path.size() > res.size())
                res = path;
        }
    cout << res.size() << endl;
    for(int val : res)
        cout << val << " ";
    cout << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll	    long long
#define vi      vector <int>
#define vii     vector <pair<int,int>>
#define ii      pair<int,int>
#define loop(n)	for(int i=0;i<n;i++)
#define loopj(n)	for(int j=0;j<n;j++)
#define endl	"\n"
ll mod=1e9+7;
const int N = 1e6+7;
bool type[N];
int arr[N];
int main()
{
      //freopen ("output.txt","w",stdout);
        ios_base::sync_with_stdio(0), cin.tie(0),cout.tie(0);

        ll n,m,k;
        cin >> n;
        loop(n)cin>>type[i];
        loop(n)cin>>arr[i];
        int x=0,mx=0,sz=0,sum=0;
        loop(n){
            sum+=arr[i];
            if(type[i]){
                if(sum >mx){
                    sz=i-x+1;
                    x=i+1;
                    mx=sum;
                }
                sum=0;
            }
        }
        cout << sz<<endl;
        for(int i=sz;i>=1;i--)
            cout << x-i+1 << " ";

}


#include <bits/stdc++.h>
using namespace std;
#define ll	    long long
#define vi      vector <int>
#define vii     vector <pair<int,int>>
#define ii      pair<int,int>
#define loop(n)	for(int i=0;i<n;i++)
#define loopj(n)	for(int j=0;j<n;j++)
#define endl	"\n"
ll mod=1e9+7;
const int N = 1e6+7;
bool type[N];
int prv[N],cntF[N];
vector<int>vect;
int main()
{
      //freopen ("output.txt","w",stdout);
        ios_base::sync_with_stdio(0), cin.tie(0),cout.tie(0);

        ll n,m,k;
        cin >> n;
        loop(n)cin>>type[i];
        loop(n){
            cin>>prv[i];
            prv[i]--;
            if(prv[i]!=-1)
                cntF[prv[i]]++;
            }
        int x=0,mx=0,sz=0,sum=0;
        loop(n){

            if(type[i]){
                int c=i;
                vector<int>ans;
                while(prv[c]!=-1 && cntF[prv[c]]<=1){
                    ans.push_back(c);
                    c=prv[c];
                }
                    ans.push_back(c);
                if(ans.size()>vect.size())
                    vect=ans;
            }

        }
        cout << vect.size()<<endl;
        reverse(vect.begin(), vect.end());
        loop(vect.size())
            cout << vect[i]+1 << " ";

}


#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
using namespace std;

ll n,k,dem[100000],cha[100000],tp[100000];
vector<ll> ans[100000];

ll maxx;

const int oo= 1e9+90;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
//	freopen(task".inp", "r", stdin);
//	freopen(task".out", "w", stdout);
    cin>>n;
    FOR(i,1,n)
    {
        cin>>tp[i];
    }
    FOR(i,1,n)
    {
        cin>>cha[i];
        if(cha[i]!=0) dem[cha[i]]++;
    }
    maxx=0;
    FOR(i,1,n){
        if(tp[i]==1)
        {
            ll j=i;
            ans[i].push_back(j);
            while(cha[j]!=0 && dem[cha[j]]==1)
            {
                j=cha[j];
                ans[i].push_back(j);
            }
            if(maxx<ans[i].size())
            {
                k=i;
                maxx=ans[i].size();
            }
        }
    }
    cout<<maxx<<endl;
    FORR(i,maxx-1,0)
        cout<<ans[k][i]<<" ";
	return 0;
}


#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
using namespace std;

ll n,k,dem[100010],cha[100010],tp[100010];
vector<ll> ans[100010];

ll maxx;

const int oo= 1e9+90;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
//	freopen(task".inp", "r", stdin);
//	freopen(task".out", "w", stdout);
    cin>>n;
    FOR(i,1,n)
    {
        cin>>tp[i];
    }
    FOR(i,1,n)
    {
        cin>>cha[i];
        if(cha[i]!=0) dem[cha[i]]++;
    }
    maxx=0;
    FOR(i,1,n){
        if(tp[i]==1)
        {
            ll j=i;
            ans[i].push_back(j);
            while(cha[j]!=0 && dem[cha[j]]==1)
            {
                j=cha[j];
                ans[i].push_back(j);
            }
            if(maxx<ans[i].size())
            {
                k=i;
                maxx=ans[i].size();
            }
        }
    }
    cout<<maxx<<endl;
    FORR(i,maxx-1,0)
        cout<<ans[k][i]<<" ";
	return 0;
}


#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
using namespace std;

ll n,k,dem[100000],cha[100000],tp[100000],d;;
vector<ll> ans[100000];

ll maxx;

const int oo= 1e9+90;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
//	freopen(task".inp", "r", stdin);
//	freopen(task".out", "w", stdout);
    cin>>n;
    FOR(i,1,n)
    {
        cin>>tp[i];
    }
    FOR(i,1,n)
    {
        cin>>cha[i];
        if(cha[i]!=0) dem[cha[i]]++;
    }
    maxx=0;
    FOR(i,1,n){
        if(tp[i]==1)
        {
            ll j=i;
            ans[i].push_back(j);
            while(cha[j]!=0 && dem[cha[j]]==1)
            {
                ans[i].push_back(cha[j]);
                j=cha[j];
            }
            if(maxx<ans[i].size())
            {
                k=i;
                maxx=ans[i].size();
            }
        }
    }
    cout<<maxx<<endl;
    FORR(i,maxx-1,0)
        cout<<ans[k][i]<<" ";
	return 0;
}


#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
using namespace std;

ll n,k,dem[100000],cha[100000],tp[100000],d;;
vector<ll> ans[100000];

ll maxx;

const int oo= 1e9+90;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
//	freopen(task".inp", "r", stdin);
//	freopen(task".out", "w", stdout);
    cin>>n;
    FOR(i,1,n)
    {
        cin>>tp[i];
    }
    FOR(i,1,n)
    {
        cin>>cha[i];
        if(cha[i]!=0) dem[cha[i]]++;
    }
    maxx=0;
    FOR(i,1,n){
        if(tp[i]==1)
        {
            ll j=i;
            ans[i].push_back(j);
            while(cha[j]!=0 && dem[cha[j]]==1)
            {
                j=cha[j];
                ans[i].push_back(j);
            }
            if(maxx<ans[i].size())
            {
                k=i;
                maxx=ans[i].size();
            }
        }
    }
    cout<<maxx<<endl;
    FORR(i,maxx-1,0)
        cout<<ans[k][i]<<" ";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long deg[200000],par[200000],a[200000];
vector<long long>adj[200000];
vector<long long>v;
vector<long long> ans;

int main(){

ios_base::sync_with_stdio(false);

long long n,i,j,u,g,k,cur;

cin>>n;

for(i=1;i<=n;i++)
cin>>a[i];

for(i=1;i<=n;i++){
cin>>par[i];

deg[par[i]]++;}

for(i=1;i<=n;i++){

if(a[i]==1){

cur=i;
v.clear();

while((par[cur]!=0)&&(deg[par[cur]]<=1)){
v.push_back(cur);
cur=par[cur];}

v.push_back(cur);

if(ans.size()<v.size())
ans=v;}}

cout<<ans.size()<<endl;
for(i=0;i<ans.size();i++)
cout<<ans[i]<<" ";

return 0;}


#include<bits/stdc++.h>
using namespace std;

long long deg[200000],vis[200000],a[200000];
vector<long long>adj[200000];
vector<long long>v;

long long find(long long src,long long len){

  v.push_back(src);

  if((vis[src]==1))
  return len-1;

  else if(adj[src].size()==0)
  return len;

  vis[src]=1;

  if(deg[src]>1)
  return len-1;

  else
  return find(adj[src][0],len+1);}

int main(){

ios_base::sync_with_stdio(false);

long long n,i,j,u,g,ans=0;

cin>>n;

for(i=1;i<=n;i++){
cin>>a[i];
if(a[i]==1)
g=i;}

for(i=1;i<=n;i++){
cin>>u;

if(u!=0){
adj[i].push_back(u);
deg[u]++;}}

for(i=1;i<=n;i++){

if(a[i]==1){
v.clear();
for(j=0;j<adj[i].size();j++)
ans=max(ans,find(i,1));}}

if(ans==0){
ans=1;
v.push_back(g);}

cout<<ans<<endl;
for(i=0;i<ans;i++)
cout<<v[i]<<" ";

return 0;}


#include<bits/stdc++.h>
using namespace std;

long long deg[200000],vis[200000],a[200000];
vector<long long>adj[200000];
vector<long long>v;

long long find(long long src,long long len){

  v.push_back(src);

  if(a[src]==1){
  v.pop_back();
  return len-1;}

  if((vis[src]==1)){
	 v.pop_back();
  return len-1;}

  if(deg[src]>1){
	 v.pop_back();
  return len-1;}

  if(adj[src].size()==0)
  return len;

  vis[src]=1;

  return find(adj[src][0],len+1);}

int main(){

ios_base::sync_with_stdio(false);

long long n,i,j,u,g,ans=0;

cin>>n;

for(i=1;i<=n;i++){
cin>>a[i];
if(a[i]==1)
g=i;}

for(i=1;i<=n;i++){
cin>>u;

if(u!=0){
adj[i].push_back(u);
deg[u]++;}}

for(i=1;i<=n;i++){

if(a[i]==1){
v.clear();
for(j=0;j<adj[i].size();j++)
ans=max(ans,find(i,1));}}

if(ans==0){
ans=1;
v.push_back(g);}

reverse(v.begin(),v.end());

cout<<ans<<endl;
for(i=0;i<ans;i++)
cout<<v[i]<<" ";

return 0;}


#include<bits/stdc++.h>
using namespace std;

long long deg[200000],par[200000],a[200000];
vector<long long>adj[200000];
vector<long long>v;
vector<long long> ans;

int main(){

ios_base::sync_with_stdio(false);

long long n,i,j,u,g,k,cur;

cin>>n;

for(i=1;i<=n;i++)
cin>>a[i];

for(i=1;i<=n;i++){
cin>>par[i];

deg[par[i]]++;}

for(i=1;i<=n;i++){

if(a[i]==1){

cur=i;
v.clear();

while((par[cur]!=0)&&(deg[par[cur]]<=1)){
v.push_back(cur);
cur=par[cur];}

v.push_back(cur);

if(ans.size()<v.size())
ans=v;}}

reverse(ans.begin(), ans.end());
cout<<ans.size()<<endl;
for(i=0;i<ans.size();i++)
cout<<ans[i]<<" ";

return 0;}


#include<bits/stdc++.h>
using namespace std;

long long deg[200000],vis[200000],a[200000];
vector<long long>adj[200000];
vector<long long>v;

long long find(long long src,long long len){

  v.push_back(src);

  if(a[src]==1){
  v.pop_back();
  return len-1;}

  if((vis[src]==1)){
	 v.pop_back();
  return len-1;}

  if(deg[src]>1){
	 v.pop_back();
  return len-1;}

  if(adj[src].size()==0)
  return len;

  vis[src]=1;

  return find(adj[src][0],len+1);}

int main(){

ios_base::sync_with_stdio(false);

long long n,i,j,u,g,ans=0;

cin>>n;

for(i=1;i<=n;i++){
cin>>a[i];
if(a[i]==1)
g=i;}

for(i=1;i<=n;i++){
cin>>u;

if(u!=0){
adj[i].push_back(u);
deg[u]++;}}

for(i=1;i<=n;i++){

if(a[i]==1){
v.clear();
vis[i]=1;
v.push_back(i);
for(j=0;j<adj[i].size();j++)
ans=max(ans,find(adj[i][j],2));}}

if(ans==0){
ans=1;
v.push_back(g);}

reverse(v.begin(),v.end());

cout<<ans<<endl;
for(i=0;i<ans;i++)
cout<<v[i]<<" ";

return 0;}


#include<bits/stdc++.h>
using namespace std;

const double eps = 1e-9;
typedef pair<double,double> pi;

    void sortbypair(double  x[], double  y[],long long  n){

    pair <double,double> pairs[n];
    for(long long  i = 0; i < n; ++i){
        pairs[i].first = y[i];
	pairs[i].second = x[i];}

    sort(pairs, pairs + n);
    for(long long i = 0; i < n; ++i){
        y[i] = pairs[i].first;
      x[i] = pairs[i].second;}}

int main(){

ios_base::sync_with_stdio(false);

long long n,i,u,v;
double x1,x2;

cin>>n;

cin>>x1>>x2;

double y1[n],y2[n],p1[n],p2[n];

for(i=0;i<n;i++){
cin>>u>>v;

y1[i]=u*(x1+eps)+v;
y2[i]=u*(x2-eps)+v;

p1[i]=p2[i]=i;}

sortbypair(p1,y1,n);
sortbypair(p2,y2,n);

for(i=0;i<n;i++)
if(p1[i]!=p2[i]){
cout<<"YES\n";
return 0;}

cout<<"NO\n";
return 0;}


#include<bits/stdc++.h>
using namespace std;

long long deg[200000],vis[200000],a[200000];
vector<long long>adj[200000];
vector<long long>v;

long long find(long long src,long long len){

  if((vis[src]==1))
  return len-1;

  if(deg[src]>1)
  return len-1;

  v.push_back(src);

  if(adj[src].size()==0)
  return len;

  vis[src]=1;

  return find(adj[src][0],len+1);}

int main(){

ios_base::sync_with_stdio(false);

long long n,i,j,u,g,ans=0;

cin>>n;

for(i=1;i<=n;i++){
cin>>a[i];
if(a[i]==1)
g=i;}

for(i=1;i<=n;i++){
cin>>u;

if(u!=0){
adj[i].push_back(u);
deg[u]++;}}

for(i=1;i<=n;i++){

if(a[i]==1){
v.clear();
for(j=0;j<adj[i].size();j++)
ans=max(ans,find(i,1));}}

if(ans==0){
ans=1;
v.push_back(g);}

reverse(v.begin(),v.end());
cout<<ans<<endl;
for(i=0;i<v.size();i++)
cout<<v[i]<<" ";

return 0;}


#include<bits/stdc++.h>
using namespace std;

long long deg[200000],vis[200000],a[200000];
vector<long long>adj[200000];
vector<long long>v;

long long find(long long src,long long len){

  v.push_back(src);

  if((vis[src]==1)){
	 v.pop_back();
  return len-1;}

  if(deg[src]>1){
	 v.pop_back();
  return len-1;}

  if(adj[src].size()==0)
  return len;

  vis[src]=1;

  return find(adj[src][0],len+1);}

int main(){

ios_base::sync_with_stdio(false);

long long n,i,j,u,g,ans=0;

cin>>n;

for(i=1;i<=n;i++){
cin>>a[i];
if(a[i]==1)
g=i;}

for(i=1;i<=n;i++){
cin>>u;

if(u!=0){
adj[i].push_back(u);
deg[u]++;}}

for(i=1;i<=n;i++){

if(a[i]==1){
v.clear();
for(j=0;j<adj[i].size();j++)
ans=max(ans,find(i,1));}}

if(ans==0){
ans=1;
v.push_back(g);}

reverse(v.begin(),v.end());

cout<<ans<<endl;
for(i=0;i<ans;i++)
cout<<v[i]<<" ";

return 0;}


#include<bits/stdc++.h>
using namespace std;
vector<int>v;
void dfs(int node,vector<int>adj[],int a[])
{
    v.push_back(node);
    for(int j:adj[node])
    dfs(j,adj,a);
}
int main()
{
    int n;
    cin>>n;
    int a[n+1];
    for(int i=1;i<=n;i++)
    cin>>a[i];
    vector<int>adj[n+1];
    int b[n+1],c[n+1];
    memset(c,0,sizeof(c));
    for(int i=1;i<=n;i++)
    {
        cin>>b[i];
        c[b[i]]++;
    }
    for(int i=1;i<=n;i++)
    {
       if(c[b[i]]==1 and b[i]!=0)
       adj[i].push_back(b[i]);
    }
    vector<int>ans;
    int pl=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]==1)
        {
            dfs(i,adj,a);
            if(v.size()>pl)
            {
                pl=v.size();
                ans.clear();
                for(int j:v)
                ans.push_back(j);
                v.clear();
            }
        }
    }
    cout<<pl<<"\n";
    for(int i=ans.size()-1;i>=0;i--)
    cout<<ans[i]<<" ";
    cout<<"\n";
    return 0;
}


/*
     AUTHOR : Ankit Kumar
     BRANCH : Information Technology
     INSTITUTE : Birla Institute of Technology
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace std;
using namespace __gnu_pbds;
typedef long long int ll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
            tree_order_statistics_node_update>
    new_data_set;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define MP make_pair
#define SET(a,val) memset(a,val,sizeof(a))
#define deci(n)         cout<<fixed<<setprecision(n);
#define show(a,l,r) for(int i=l;i<r;i++) cout<<a[i]<<" ";
#define all(a) (a).begin(), (a).end()
#define pll pair<ll,ll>
const int N=100003;
const int dx[] = {-1, 0, 1, 0, -1,-1, 1, 1};
const int dy[] = { 0, 1, 0,-1, -1, 1,-1, 1};
char dir[]={'L','D','R','U'};
//int dx[]={2,-2,1,-1,2,-2,1,-1};//for knights
//int dy[]={1,1,2,2,-1,-1,-2,-2};
const ll mod=1e9+7;
const ll inf=1e18+100;
ll BIT[30004];
int prime[100003],spf[17];
template<typename T>T lcm(T a, T b) {return a / gcd(a,b) * b;}
template<typename T> T min3(T a,T b,T c) {return min(a,min(b,c));}
//template<typename T> T min(T a,T b) {if(a>b) return b; else return a;}
template<typename T> T max3(T a,T b,T c) {return max(a,max(b,c));}
bool isVowel(char ch){ ch=toupper(ch); if(ch=='A'||ch=='U'||ch=='I'||ch=='O'||ch=='E') return true; return false;}
ll mInv(ll a, ll m)
{
    ll m0 = m;
    ll y = 0, x = 1;
    if (m == 1)
      return 0;
    while (a > 1)
    {
        ll q = a / m;
        ll t = m;
        m = a % m, a = t;
        t = y;
        y = x - q * y;
        x = t;
    }
    if (x < 0)
       x += m0;
return x;
}
ll power(ll x, ll y)
{
    ll res = 1;
   // x = x % mod;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) ;
        y = y>>1;
        x = (x*x);
        if(res>=mod)
            res%=mod;
        if(x>=mod)
            x%=mod;
    }
    return res;
}
ll gcd(ll a,ll b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
void update(int x, int delta)
{
    if(!x)
    {
        BIT[x]+=delta;
        return;
    }
      for(; x <= N; x += x&-x)
        BIT[x] += delta;
}
ll query(int x)
{
     ll sum = BIT[0];
     for(; x > 0; x -= x&-x)
        sum += BIT[x];
     return sum;
}
void Sieve()
{
    for(int i=1;i<=N;i++)
        prime[i]=1;
    prime[0]=0;
    prime[1]=0;
    for (ll p=2; p*p<=N; p++)
    {
        if (prime[p] == 1)
        {
            for (ll i=p*p; i<=N; i += p)
                prime[i] = 0;
        }
    }
}
//void sieve()
//{
//    spf[1] = 1;
//    for (int i=2; i<N; i++)
//        spf[i] = i;
//    for (int i=4; i<N; i+=2)
//        spf[i] = 2;
//    for (ll i=3; i*i<N; i++)
//    {
//        if (spf[i] == i)
//        {
//            for (ll j=i*i; j<N; j+=i)
//                if (spf[j]==j)
//                    spf[j] = i;
//        }
//    }
//}
                         /******************Lets Code*******************/

bool cmp(const pair<ll,ll>&a,const pair<ll,ll>&b)
{
    if(a.first>b.first)
        return true;
    else if(a.first==b.first)
    {
        if(a.second<b.second)
            return true;
        else
            return false;
    }
    else
        return false;
}
vector<int>v;
void dfs(int node,vector<int>adj[],int a[])
{
    if(a[node]==1)
        return;
    v.push_back(node);
    for(int j:adj[node])
    dfs(j,adj,a);
}
int main()
{
    fast;
    int n;
    cin>>n;
    int a[n+1];
    for(int i=1;i<=n;i++)
    cin>>a[i];
    vector<int>adj[n+1];
    int b[n+1],c[n+1];
    memset(c,0,sizeof(c));
    for(int i=1;i<=n;i++)
    {
        cin>>b[i];
        c[b[i]]++;
    }
    for(int i=1;i<=n;i++)
    {
       if(c[b[i]]==1 and b[i]!=0)
       adj[i].push_back(b[i]);
    }
    vector<int>ans;
    int pl=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]==1)
        {
            int x=i;
            if(adj[i].size()>0)
                x=adj[i][0];
            v.push_back(i);
            dfs(x,adj,a);
            if(v.size()>pl)
            {
                pl=v.size();
                ans.clear();
                for(int j:v)
                ans.push_back(j);
            }
            v.clear();
        }
    }
    cout<<pl<<"\n";
    for(int i=ans.size()-1;i>=0;i--)
    cout<<ans[i]<<" ";
    cout<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<long long int,long long int> pa;
#define resize(A) A.resize(unique(A.begin(), A.end()) - A.begin())
//ios_base::sync_with_stdio(false);cin.tie(NULL);
/*ifstream cin("input.txt");
ofstream cout("output.txt");*/
#define pb push_back
#define  MAX 100009
#define ll long long int
long long int MOD=998244353;
long long int mod=1e9+7;
long long int INF=1e18;
ll dx[]={1,-1,0,0};
ll dy[]={0,0,1,-1};
vector<ll> type(MAX);
vector<ll> out(MAX,0);
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    ll n;
    cin>>n;

vector<ll> prev(n+1);
    for(int i=1;i<=n;i++)
        cin>>type[i];
    for(int i=1;i<=n;i++)
    {

        cin>>prev[i];
        if(prev[i]==0)
            continue;
        out[prev[i]]++;

    }
    vector<ll> ans;
    for(int i=1;i<=n;i++)
    {
        if(type[i]==1)
        {
            vector<ll> tmp;
            ll cur=i;
            while(prev[cur]!=0&&out[prev[cur]]<=1)
            {
                tmp.pb(cur);
                cur=prev[cur];
            }
            tmp.pb(cur);
            if(tmp.size()>ans.size())
                ans=tmp;
        }
    }
    cout<<ans.size()<<endl;
    reverse(ans.begin(),ans.end());
    for(auto j:ans)
        cout<<j<<" ";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<long long int,long long int> pa;
#define resize(A) A.resize(unique(A.begin(), A.end()) - A.begin())
//ios_base::sync_with_stdio(false);cin.tie(NULL);
/*ifstream cin("input.txt");
ofstream cout("output.txt");*/
#define pb push_back
#define  MAX 100009
#define ll long long int
long long int MOD=998244353;
long long int mod=1e9+7;
long long int INF=1e18;
ll dx[]={1,-1,0,0};
ll dy[]={0,0,1,-1};
vector<ll> type(MAX);
vector<ll> out(MAX,0);
vector<ll> bfs(vector<ll> adj[],ll x,ll n)
{
    bool vis[n+1]={0};
    vis[x]=1;
    queue<ll> q;
    q.push(x);
    vector<ll> ans;
   // ans.pb(x);
    while(!q.empty())
    {
        x=q.front();
        q.pop();
        if(out[x]>1)
            break;
        ans.pb(x);
        for(auto j:adj[x])
        {
            if(!vis[j])
            {
                q.push(j);
                vis[j]=1;
            }
        }
    }
    return ans;
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    ll n;
    cin>>n;
    vector<ll> adj[n+1];
    for(int i=1;i<=n;i++)
        cin>>type[i];
    for(int i=1;i<=n;i++)
    {
        ll x;
        cin>>x;
        if(x==0)
            continue;
        out[x]++;
        adj[x].pb(i);
        adj[i].pb(x);
    }
    vector<ll> ans;
    for(int i=1;i<=n;i++)
    {
        if(type[i]==1)
        {
            vector<ll> temp=bfs(adj,i,n);
            if(temp.size()>ans.size())
                ans=temp;
        }
    }
    cout<<ans.size()<<endl;
    reverse(ans.begin(),ans.end());
    for(auto j:ans)
        cout<<j<<" ";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vll;

#define pb push_back

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define vin(v, n) for (int i = 0; i < (int)(n); i++) cin >> (v)[i];

vll types(1e6, -1);
vector<unordered_set<ll>> g(1e6);
vector<bool> visited(1e6, false);

string dfs(ll node) {
	//cout << "At " << node << endl;
	visited[node] = true;

	string s = to_string(node);

	for (auto i : g[node]) {
		if (!visited[i]) s += dfs(i);
	}

	return s;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	ll n;
	cin >> n;

	vll hotels;

	for (int i = 1; i <= n; i++) {
		cin >> types[i];
		if (types[i] == 1) hotels.pb(i);
	}

	vll tr;
	unordered_set<ll> occured;
	for (int i = 1; i <= n; i++) {
		ll a;
		cin >> a;

		if (a == 0) continue;

		if (occured.find(a) == occured.end()) g[a].insert(i), g[i].insert(a), occured.insert(a);
		else g[a].insert(i), g[i].insert(a), tr.pb(a);
	}

	for (auto i : tr) {
		//cout << i << " in tr" << endl;
		for (auto j : g[i]) {
			g[j].erase(g[j].find(i));
		}
		g[i].clear();
	}

	string ans = "";
	for (auto h : hotels) {
		string s = dfs(h);
		if (s.length() > ans.length()) ans = s;
	}
	ll an = ans.length();

	cout << an << endl;
	for (int i = an - 1; i >= 0; i--) cout << ans[i] << " ";

}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vll;

#define pb push_back

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define vin(v, n) for (int i = 0; i < (int)(n); i++) cin >> (v)[i];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	ll n;

	cin >> n;

	vll hotels;

	vll nums1(n + 1), nums2(n + 1);
	for (int i = 1; i <= n; i++) {
		cin >> nums1[i];

		if (nums1[i] == 1) hotels.pb(i);
	}

	vll count(1e6, 0);

	for (int i = 1; i <= n; i++) {
		cin >> nums2[i];

		if (nums2[i] != 0) count[nums2[i]]++;
	}

	ll bestLen = -1;
	vector<ll> bestVec;
	for (auto h : hotels) {
		vll path;
		auto cur = h;
		path.pb(h);

		while (nums2[cur] != 0 && count[nums2[cur]] <= 1) {
			path.pb(nums2[cur]);
			cur = nums2[cur];
		}

		ll pn = path.size();
		if (pn > bestLen) bestLen = pn, bestVec = path;
	}

	cout << bestLen << endl;
	for (ll i = bestLen - 1; i >= 0; i--) cout << bestVec[i] << " ";
}


#include <bits/stdc++.h>
using namespace std;

#define MX 100050
vector<int> g[MX];
bool vis[MX];
vector<int> res;
vector<int> path;
int ideg[MX];
bool type[MX];

void dfs(int u) {
	vis[u] = true;
	path.push_back(u);
	bool f = true;

	for(auto v : g[u])
		if(!vis[v] && ideg[v] < 2 && !type[v]) {
		    dfs(v);
		    f = false;
		}
	if(f && res.size() < path.size()) res = path;
	path.pop_back();
}

int main() {
	int i, n;
	scanf("%d", &n);
	for(i = 1; i <= n; i++) scanf("%d", type + i);
	for(i = 1; i <= n; i++) {
		int p;
		scanf("%d", &p);
		if(p) {
			g[i].push_back(p);
			ideg[p]++;
		}
	}
	for(i = 1; i <= n; i++)
		if(!vis[i] && type[i]) {
			path.clear();
			dfs(i);
		}
	printf("%d\n", res.size());
	for(i = res.size() - 1; i >= 0; i--) printf("%d%c", res[i], i == 0 ? '\n' : ' ');
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define MX 100050
vector<int> g[MX];
bool vis[MX];
vector<int> res;
vector<int> path;
bool type[MX];

void dfs(int u) {
	vis[u] = true;
	path.push_back(u);
	if(res.size() < path.size()) res = path;

	for(auto v : g[u])
		if(!vis[v] && g[v].size() < 3 && !type[v]) dfs(v);
	path.pop_back();
}

int main() {
	int i, n;
	scanf("%d", &n);
	for(i = 1; i <= n; i++) scanf("%d", type + i);
	for(i = 1; i <= n; i++) {
		int p;
		scanf("%d", &p);
		if(p) {
			g[p].push_back(i);
			g[i].push_back(p);
		}
	}
	for(i = 1; i <= n; i++)
		if(!vis[i] && type[i]) {
			path.clear();
			dfs(i);
		}
	printf("%d\n", res.size());
	for(i = res.size() - 1; i >= 0; i--) printf("%d%c", res[i], i == 0 ? '\n' : ' ');
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define MX 100050
vector<int> g[MX];
bool vis[MX];
vector<int> res;
vector<int> path;
bool type[MX];

void dfs(int u) {
	vis[u] = true;
	path.push_back(u);

	for(auto v : g[u]) {
		if(!vis[v] && g[v].size() < 3 && !type[v]) dfs(v);
		else if(res.size() < path.size()) res = path;
	}
}

int main() {
	int i, n;
	scanf("%d", &n);
	for(i = 1; i <= n; i++) scanf("%d", type + i);
	for(i = 1; i <= n; i++) {
		int p;
		scanf("%d", &p);
		if(p) {
			g[p].push_back(i);
			g[i].push_back(p);
		}
	}
	for(i = 1; i <= n; i++)
		if(!vis[i] && type[i]) {
			path.clear();
			dfs(i);
		}
	printf("%d\n", res.size());
	for(i = res.size() - 1; i >= 0; i--) printf("%d%c", res[i], i == 0 ? '\n' : ' ');
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define MX 100050
vector<int> g[MX];
bool vis[MX];
vector<int> res;
vector<int> path;
int ideg[MX];
bool type[MX];

void dfs(int u) {
	vis[u] = true;
	path.push_back(u);
	if(res.size() < path.size()) res = path;

	for(auto v : g[u])
		if(!vis[v] && ideg[v] < 2 && !type[v]) dfs(v);
	path.pop_back();
}

int main() {
	int i, n;
	scanf("%d", &n);
	for(i = 1; i <= n; i++) scanf("%d", type + i);
	for(i = 1; i <= n; i++) {
		int p;
		scanf("%d", &p);
		if(p) {
			g[i].push_back(p);
			ideg[p]++;
		}
	}
	for(i = 1; i <= n; i++)
		if(!vis[i] && type[i]) {
			path.clear();
			dfs(i);
		}
	printf("%d\n", res.size());
	for(i = res.size() - 1; i >= 0; i--) printf("%d%c", res[i], i == 0 ? '\n' : ' ');
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 110000;

int que[N], dp[N], prv[N], bf[N], deg[N];

int main() {

	ios_base::sync_with_stdio(0);

	int n; cin >> n;
	int hd = 0, tl = 0;
	for (int i = 1; i <= n; i++) {
		int u; cin >> u;
		if (u) dp[i] = 1, que[tl++] = i;
	}
	for (int i = 1; i <= n; i++) {
		int u; cin >> u;
		prv[i] = u;
		deg[u]++;
	}
	while (hd < tl) {
		int cur = que[hd++];
		if (prv[cur] && !dp[prv[cur]] && deg[prv[cur]] == 1) {
			dp[prv[cur]] = dp[cur] + 1;
			que[tl++] = prv[cur];
			bf[prv[cur]] = cur;
		}
	}

	int id = 0, Max = 0;
	for (int i = 1; i <= n; i++) if (Max < dp[i]) {
		Max = dp[i], id = i;
	}

	vector<int> ans;
	int cur = id;
	while (cur) ans.push_back(cur), cur = bf[cur];
	cout << ans.size() << endl;
	for (int i = 0; i < ans.size(); i++) cout << ans[i] << " \n"[i == ans.size() - 1];

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 100005;

int n;

int flag[maxn];
bool vis[maxn];

vector<int> adj[maxn];
vector<int> stor;
vector<int> cur;

void dfs(int u)
{
	if(adj[u].size() > 2) return;
	vis[u] = 1;
	cur.push_back(u);
	for(auto it : adj[u])
		if(!vis[it])
			dfs(it);
}

int main()

{
	cin >> n;
	for(int i = 1; i <= n; i ++)
		cin >> flag[i];
	for(int i = 1; i <= n; i ++)
	{
		int u;
		cin >> u;
		if(u == 0) continue;
		adj[i].push_back(u);
		adj[u].push_back(i);
	}
	for(int i = 1; i <= n; i ++)
	{
		if(flag[i])
		{
			cur.clear();
			dfs(i);
			if(cur.size() > stor.size())
				stor = cur;
		}
	}
	int sz = stor.size();
	cout << sz << endl;
	for(int i = sz - 1; i >= 0; i -- )
		cout << stor[i] << " ";
}


#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  #ifdef LOCAL_DEFINE
  freopen ("input.txt", "r", stdin);
  freopen ("output.txt", "w", stdout);
  #endif
  int n;
  cin >> n;
  vector <bool> type (n);
  vector <int> p(n);
  for (int i = 0; i < n; ++i) {
    int type_ip;
    cin >> type_ip;
    type[i] = type_ip == 1;
  }
  for (int i = 0; i < n; ++i) {
    cin >> p[i];
    --p[i];
  }
  vector <int> out_degree(n, 0);
  for (int i = 0; i < n; ++i) {
    if (p[i] >= 0) {
      out_degree[p[i]]++;
    }
  }
  vector <int> sol_path;
  for (int i = 0; i < n; ++i) {
    if (!type[i]) continue;
    vector <int> temp_path;
    int j = i;
    temp_path.push_back (j + 1);
    while (out_degree[p[j]] == 1) {
      temp_path.push_back (p[j] + 1);
      j = p[j];
    }
    if (sol_path.size() < temp_path.size()) {
      sol_path = temp_path;
    }
  }
  reverse(sol_path.begin(), sol_path.end());
  cout << sol_path.size() << '\n';
  for (int i : sol_path) {
    cout << i << ' ';
  }
}


#include <bits/stdc++.h>
using namespace std;

const int n_max = 100001;
int p[n_max], out_deg[n_max], type[n_max];
vector<int> ans_path, temp;

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
#ifdef LOCAL_DEFINE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
  {
    cin >> type[i];
  }
  for (int i = 0; i < n; ++i)
  {
    cin >> p[i];
    p[i]--;
    if (p[i] >= 0)
    {
      out_deg[p[i]]++;
    }
  }
  for (int i = 0; i < n; ++i)
  {
    if (!type[i])
      continue;
    int j = i;
    temp.clear();
    temp.push_back(j);
    while (out_deg[p[j]] <= 1 && p[j] != -1)
    {
      j = p[j];
      temp.push_back(j);
    }
    if (temp.size() > ans_path.size())
    {
      reverse(temp.begin(), temp.end());
      ans_path = temp;
    }
  }
  cout << ans_path.size() << '\n';
  for (int i : ans_path)
  {
    cout << i + 1 << ' ';
  }
}


#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
#define pb push_back
#define READ(FILE) freopen(FILE,"r",stdin)
#define WRITE(FILE) freopen(FILE,"w",stdout)
#define loop(i,s,e) for(int i=s;i<e;i++)
#define mego fast();
#define memes(x,y) memset(x,y,sizeof(x))
#define gone_pro main
using namespace std;
void fast()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

ll lcm(ll x, ll y)
{
    return (x*y)/__gcd(x,y);
}
ll sum(int x)
{
    ll s=x;
    return (s*(s+1))/2;
}
int gone_pro()
{
//    mego
int n;
cin>>n;
int a[n+1]={},b[n+1]={},c[n+1]={};
vector<int> ans,cur;
loop(i,1,n+1)
cin>>a[i];
loop(i,1,n+1)
cin>>b[i];
loop(i,1,n+1)
if(b[i])c[b[i]]++; //roads from b[i] to  other vertices
loop(i,1,n+1)
{cur.clear();
    if(a[i]==0)
        continue;
    int prevp=b[i];
    cur.pb(i);
    while(c[prevp]==1)
    {
        cur.pb(prevp);
        prevp=b[prevp];
       // cout<<prevp<<endl;
    }
    if(cur.size()>ans.size())ans=cur;
}
reverse(ans.begin(),ans.end());
cout<<ans.size()<<endl;
loop(i,0,ans.size())
cout<<ans[i]<<" ";
    return 0;
}


#include<iostream>
#include<algorithm>
#include<math.h>
#include<string>
#include<map>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<numeric>
#include<iomanip>
#define MAX 500005
#define MOD 1000000007
#define lo long long
#define INF 1e9
#define pp pair<long long , long long>
#define ppp pair<long long, pair<long long, long long> >
#define pb(a) push_back(a)
#define fi first
#define se second
#define PI 3.14159265
using namespace std;
lo a[MAX];
lo outdegree[MAX];
lo type[MAX];
lo dfs(lo node) {
    if(outdegree[node] > 1) {
        return 0;
    }

    lo ans = 1;
    if(a[node] != 0) {
        ans += dfs(a[node]);
    }

    return ans;
}
void getPath(lo node, lo K, vector<lo> & res) {
    if(K == 0) {
        return;
    }
    res.pb(node);
    if(a[node] != 0) {
        getPath(a[node], K - 1, res);
    }
}
void solve(lo n) {
    lo ans = 0;
    lo anode = 0;
    for(int i=1; i<=n; i++) {
        if(type[i] == 1) {
           if(dfs(i) > ans) {
                ans = dfs(i);
                anode = i;
           }
        }
    }
    vector<lo> res;

    getPath(anode, ans, res);
    reverse(res.begin(), res.end());

    cout<<ans<<endl;
    for(int i=0; i<res.size(); i++) {
        cout<<res[i]<<" ";
    }
}
int main() {

    lo n;
    cin>>n;
    //cout<<"here"<<endl;

    for(int i=1; i<=n; i++) {
        cin>>type[i];
    }

    for(int i=1; i<=n; i++) {
        lo par;
        cin>>par;
        a[i] = par;
        if(par != 0) {
            outdegree[par]++;
        }
    }
    solve(n);
    return 0;
}


#include<iostream>
#include<algorithm>
#include<math.h>
#include<string>
#include<map>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<numeric>
#include<iomanip>
#define MAX 500005
#define MOD 1000000007
#define lo long long
#define INF 1e9
#define pp pair<long long , long long>
#define ppp pair<long long, pair<long long, long long> >
#define pb(a) push_back(a)
#define fi first
#define se second
#define PI 3.14159265
using namespace std;
vector<lo> g[MAX];
lo type[MAX];
lo dp[MAX];
lo visited[MAX];

lo dfs(lo node) {
    visited[node] = 1;

    if(type[node] == 1) {
        dp[node] = 1;
        return 1;
    }

    lo ans = 0;
    lo sz = g[node].size();
    for(int i=0; i<g[node].size(); i++) {
        lo temp = 0;
        if(!visited[g[node][i]]) {
            temp = dfs(g[node][i]);
            if(sz == 1 && temp != 0) {
                ans += 1 + temp;
            }
        }

    }
    dp[node] = ans;
    return ans;
}

void solve(lo n) {
    for(int i=1; i<=n; i++) {
        if(!visited[i]) {
            dfs(i);
        }
    }
    lo ans = 0;
    lo anode;
    for(int i=1; i<=n; i++) {
        //cout<<i<<" "<<dp[i]<<endl;
        if(dp[i] > ans) {
            ans = dp[i];
            anode = i;
        }
    }
    cout<<ans<<endl;

    lo tnode = anode;

    lo temp = ans;
    cout<<anode<<" ";
    while(temp > 1) {
        for(int i=0; i<g[tnode].size(); i++) {
            cout<<g[tnode][i]<<" ";
            tnode = g[tnode][i];
            temp--;
        }
    }
}
int main() {

    lo n;
    cin>>n;

    for(int i=1; i<=n; i++) {
        cin>>type[i];
    }

    for(int i=1; i<=n; i++) {
        lo par;
        cin>>par;
        if(par != 0)
             g[par].pb(i);
    }
    solve(n);
    return 0;
}


#include<iostream>
#include<algorithm>
#include<math.h>
#include<string>
#include<map>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<numeric>
#include<iomanip>
#define MAX 500005
#define MOD 1000000007
#define lo long long
#define INF 1e9
#define pp pair<long long , long long>
#define ppp pair<long long, pair<long long, long long> >
#define pb(a) push_back(a)
#define fi first
#define se second
#define PI 3.14159265
using namespace std;
vector<lo> g[MAX];
lo type[MAX];
lo dp[MAX];
lo visited[MAX];

lo dfs(lo node) {
    visited[node] = 1;

    if(type[node] == 1) {
        dp[node] = 1;
        return 1;
    }

    lo ans = 0;
    lo sz = g[node].size();
    for(int i=0; i<g[node].size(); i++) {
        lo temp = 0;
        if(!visited[g[node][i]]) {
            temp = dfs(g[node][i]);
            if(sz == 1) {
                ans += 1 + temp;
            }
        }

    }
    dp[node] = ans;
    return ans;
}

void solve(lo n) {
    for(int i=1; i<=n; i++) {
        if(!visited[i]) {
            dfs(i);
        }
    }
    lo ans = 0;
    lo anode;
    for(int i=1; i<=n; i++) {
        //cout<<i<<" "<<dp[i]<<endl;
        if(dp[i] > ans) {
            ans = dp[i];
            anode = i;
        }
    }
    cout<<ans<<endl;

    lo tnode = anode;

    lo temp = ans;
    cout<<anode<<" ";
    while(temp > 1) {
        for(int i=0; i<g[tnode].size(); i++) {
            cout<<g[tnode][i]<<" ";
            tnode = g[tnode][i];
            temp--;
        }
    }
}
int main() {

    lo n;
    cin>>n;

    for(int i=1; i<=n; i++) {
        cin>>type[i];
    }

    for(int i=1; i<=n; i++) {
        lo par;
        cin>>par;
        if(par != 0)
             g[par].pb(i);
    }
    solve(n);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define vl vector<long long int>
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vb vector<bool>
#define vvl vector<vector<ll> >
#define vvi vector<vector<int> >
#define pl pair<ll,ll>
#define pb push_back
#define PI 3.14159265
#define mod 998244353
#define pb push_back
#define mp make_pair
#define fri(s,n) for(int i=s;i<n;i++)
#define frj(s,n) for(int j=s;j<n;j++)
#define T(i) int i=1;cin>>i;while(i--)
#define vsi vector<set<int> >
#define pii pair<int,int>
#define inf 1e18
#define vpii vector<pair<int,int> >

vvi graph;
vi outgoing;

int main(){
    int n;
    cin>>n;
    vi src;
    fri(1,n+1){
        int x;
        cin>>x;
        if(x==1)
            src.pb(i);}
    graph.resize(n+1);
    outgoing.resize(n+1);
    vi parent(n+1);
    fri(1,n+1){
        int x;
        cin>>x;
        if(x==0)
            continue;
        parent[i]=x;
        outgoing[x]++;
        }
    vi ans;
    for(auto x:src){
         vi temp;
         while(outgoing[x]<=1 && x!=0){
            temp.pb(x);
            x=parent[x];
         }
         if(temp.size()>ans.size())
                ans=temp;}
    cout<<ans.size()<<"\n";
    for(int i=ans.size()-1;i>=0;i--)
        cout<<ans[i]<<" ";

    }