CD exchange
In a computer club, there is a CD disc exchange market where you can trade any disc for another one from the catalog, either directly or through a series of intermediate exchanges. The catalog features N
different discs, numbered from 1 to N. For each disc i
in the catalog (i=1..N), there is a list of disc numbers that you can obtain in exchange, with an additional cost of 1 UAH for each exchange.
Determine the minimum amount of UAH required to acquire all the discs, given that you already have an unlimited supply of the first K
discs in the catalog.
Input Data
The first line of the input contains the integers N
and K
. The following N
lines provide information about the possible exchanges for each disc i
.
Output Data
Output the minimum amount of UAH needed to exchange for all the discs in the catalog. All numbers are natural numbers and do not exceed 100.