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

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


import collections


def find(parents, i):
    if parents[i] == i:
        return i
    result = find(parents, parents[i])
    parents[i] = result
    return result


def union(parents, i, j):
    i, j = find(parents, i), find(parents, j)
    parents[i] = j


if __name__ == '__main__':
    n, x = map(int, input().split())
    a = {i + 1: int(ai) for i, ai in enumerate(input().split())}

    parents = {i: i for i in a}
    x_order = 1
    x_current = x
    while a[x_current] != 0:
        x_order += 1
        x_current = a[x_current]

    for source, target in a.items():
        if target != 0:
            union(parents, source, target)

    del a

    x_representative = find(parents, x)

    sizes = collections.Counter()
    for i in parents:
        i_representative = find(parents, i)
        if i_representative != x_representative:
            sizes[i_representative] += 1

    del parents

    adds = collections.Counter()
    for size in sizes.values():
        adds[size] += 1

    del sizes

    sieve = {0: 1}
    for add, count in adds.items():
        sieve_update = {}
        for i, j in sieve.items():
            if j != 0:
                for k in range(1, count + 1):
                    if i + add * k <= n:
                        sieve_update[i + add * k] = 1
        sieve.update(sieve_update)

    del adds

    for position, value in sorted(sieve.items()):
        if value:
            print(position + x_order)
