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

/**
 * Alipay.com Inc. Copyright (c) 2004-2020 All Rights Reserved.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * ABBYY Cup 3.0
 *
 * B2. EKG
 *
 * @author linwanying
 * @version $Id: B316.java, v 0.1 2020年03月07日 5:07 PM linwanying Exp $
 */
public class B316 {
    public static void main(String[] args) {
        new B316().run();
    }

    private void run() {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int x = scanner.nextInt();
        int[] a = new int[n];
        int[] b = new int[n];
        boolean[] vis = new boolean[n];
        for (int i = 0; i < n; ++i) {
            int cur = scanner.nextInt();
            a[i] = cur;
            if (cur != 0) {
                b[cur-1] = i+1;
                vis[i] = true;
            }
        }

        int xi = 1;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; ++i) {
           if (!vis[i]) {
               int curi = i, len = 1, j = 0;
               boolean xf = i+1 == x;
               while (b[curi] != 0) {
                   len++;
                   j++;
                   if (b[curi] == x) {
                       xi = j + 1;
                       xf = true;
                   }
                   curi = b[curi] - 1;
               }
               if (!xf) {
                   list.add(len);
               }
           }
        }
        //System.out.println(JSON.toJSONString(list));

        boolean[] dp = new boolean[n+1];
        dp[xi] = true;
        for (int i = 0; i < list.size(); ++i) {
            for (int j = n; j >= 0; --j) {
                if (dp[j]) {
                    dp[j+list.get(i)] = true;
                }
            }
        }
        for (int j = 0; j <= n; ++j) {
            if (dp[j]) {
                System.out.println(j);
            }
        }

    }
}