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.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
	public static void main(String[] args) throws IOException {
		(new Main()).solve();
	}

	public Main() {
	}
	
	MyReader in = new MyReader();
	PrintWriter out = new PrintWriter(System.out);
	
	void solve() throws IOException {
		//BufferedReader in = new BufferedReader(new
		//InputStreamReader(System.in));
		//Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int x = in.nextInt() - 1;
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = in.nextInt() - 1;
		}
		boolean[] use = new boolean[n];
		List<Integer> counts = new ArrayList<Integer>();
		int before = 0, after = 0;
		int len = 0;
		for (int i = 0; i < n; i++) {
			if (!use[i]) {
				int cur = i;
				boolean flag = true;
				while (flag) {
					flag = false;
					for (int j = 0; j < n; j++) {
						if (a[j] == cur) {
							cur = j;
							flag = true;
							break;
						}
					}
				}				
				
				int count = 1;
				boolean bef = false;

				while (a[cur] > -1) {
					if (cur == x) {
						after = count - 1;
						bef = true;
						count = 0;
					} 
					if (bef) {
						before++;
					} else {
						count++;
					}
					use[cur] = true;
					cur = a[cur];
				}
				use[cur] = true;
				if (count > 0 && cur != x) {
					counts.add(count);
					len += count;
				}
			}
		}
		len = len + before + after + 1;
		int rest = n - len;
		for (int i = 0; i < rest; i++) {
			counts.add(1);
		}
		boolean[] can = new boolean[n];
		boolean[] can2 = new boolean[n];
		can[0] = true;
		for (int t : counts) {
			for (int i = 0; i < n; i++) {
				if (can[i] && i + t < n) {
					can2[i + t] = true;
				}
			}
			for (int i = 0; i < n; i++) {
				can[i] = can[i] || can2[i];
			}
		}
		for (int i = 0; i < n; i++) {
			if (can[i]) {
				out.println(i + before + 1);
			}
		}
		
		out.close();
		
	}

	
};

class MyReader {
	private BufferedReader in;
	String[] parsed;
	int index = 0;

	public MyReader() {
		in = new BufferedReader(new InputStreamReader(System.in));
	}

	public int nextInt() throws NumberFormatException, IOException {
		if (parsed == null || parsed.length == index) {
			read();
		}
		return Integer.parseInt(parsed[index++]);
	}

	public long nextLong() throws NumberFormatException, IOException {
		if (parsed == null || parsed.length == index) {
			read();
		}
		return Long.parseLong(parsed[index++]);
	}
	
	public double nextDouble() throws NumberFormatException, IOException {
		if (parsed == null || parsed.length == index) {
			read();
		}
		return Double.parseDouble(parsed[index++]);
	}
	
	public String nextString() throws IOException {
		if (parsed == null || parsed.length == index) {
			read();
		}
		return parsed[index++];
	}

	private void read() throws IOException {
		parsed = in.readLine().split(" ");
		index = 0;
	}

	public String readLine() throws IOException {
		return in.readLine();
	}
};

