Feline Friendship
Time limit2sMemory limit1024 MB
Given a permutation of n cats, find the fewest favourite-partner changes needed so that some cycle has length exactly k.
Statement
There is a large community of cats in Delft. The cats are numbered from to . Each cat has a favourite playing partner . Cats can be very egocentric, so is allowed. No two cats share the same favourite playing partner, so the are distinct.
You are organising a big game of Cats versus Coatis football, for which you need exactly cats in a team.
To get cats to join, you appoint one cat as team captain. Then the following process repeats, starting with the captain. Cat selects its favourite partner and adds to the team. Then cat selects its favourite partner, adds to the team, and so on. The process stops only when a cat tries to invite a cat already on the team. If, for some choice of captain, the team has exactly cats, the game can be played.
Sometimes no team of cats can be found this way. So you decide to convince some cats to change their favourite playing partner. Formally, you repeatedly select a cat (), choose (), and update . After a change, the may no longer be distinct, which is fine.
What is the minimum number of times you need to convince a cat to change its favourite playing partner so that the game can be played?
Non-American.
Input
The first line contains two integers and (, ), the number of cats and the team size. The second line contains integers (), where the th integer is the favourite playing partner of cat . It is guaranteed that the are all distinct.
Output
Output the minimum number of times you need to convince a cat to change its favourite playing partner so that the game can be played.