20 20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 10 4
0 1 4 2 7 0 10 0 5 8
 10 7
10 8 6 5 0 0 0 4 3 9
 10 1
8 7 0 2 0 10 0 0 3 5
 10 7
7 9 2 10 0 0 0 3 5 1
 10 2
10 0 9 0 0 4 2 6 8 0
 10 2
0 7 0 10 8 0 4 2 3 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 3
4
8
9
 1
5
6
10
 2
4
5
7
8
10
 1
2
6
7
 1
2
3
4
6
7
8
9
 4
5
6
7
8

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Sol22 {

	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;

	String nextToken() throws IOException {
		while (st == null || !st.hasMoreTokens()) {
			String s = br.readLine();
			if (s == null) {
				return "-1";
			}
			st = new StringTokenizer(s);
		}
		return st.nextToken();
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	void run() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);

		int n = nextInt();
		int x = nextInt() - 1;
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt() - 1;
		}

		int[] c = new int[n];
		for (int i = 0; i < n; i++) {
			if (a[i] >= 0) {
				c[a[i]] = 1;
			}
		}
		int p = 0;
		int[] ans = new int[n];
		for (int i = 0; i < n; i++) {
			if (c[i] == 0) {
				boolean bol = false;
				int j = i;
				int k = 1;
				while (a[j] >= 0) {
					if (j == x) {
						bol = true;
					}
					j = a[j];
					k++;
				}
				if (j == x) {
					bol = true;
				}
				if (!bol) {
					ans[p] = k;
					p++;
				}

			}
		}
		int j = x;
		int k = 1;
		while (a[j] >= 0) {
			j = a[j];
			k++;
		}
		Arrays.sort(ans,0,p);
		int[] b = new int[n + 1];
		b[0] = 1;
		for (int i = 0; i < p; i++){
			for ( int t = n; t >=0; t--){
				if (b[t]>0){
					b[t+ans[i]]=1;
				}
			}
		}
		
		
		for (int i = 0; i <= n; i++) {
			if (b[i] > 0) {
				out.println(i+k);
			}
		}

		out.println();

		br.close();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new Sol22().run();
	}

}
