program teleporter
  implicit none
  integer :: n, k, a(0:30,100000) = 0, i, j, b, u(100000) = 0
  read(*,*) n, k
  read(*,*) a(0,1:n)
  do i = 1, 30
    do j = 1, n
      a(i,j) = a(i-1,a(i-1,j))
    end do
  end do
  do i = 1, n
    b = i
    do j = 30, 0, -1
      if (btest(k,j)) b = a(j,b)
    end do
    u(b) = 1
  end do
  write(*,'(i0)') sum(u(2:n))
  stop
end program teleporter