Under construction

Still being built. Things may change

Feline Friendship

Time limit2sMemory limit1024 MB

Summary
Given a permutation of n cats, find the fewest favourite-partner changes needed so that some cycle has length exactly k.
Level

Medium6 of 10

Topics
Graph, Greedy, Math
Solved
No attempts yet

Statement

There is a large community of nn cats in Delft. The cats are numbered from 11 to nn. Each cat has a favourite playing partner pip_i. Cats can be very egocentric, so pi=ip_i = i is allowed. No two cats share the same favourite playing partner, so the pip_i are distinct.

You are organising a big game of Cats versus Coatis football, for which you need exactly kk cats in a team.

To get kk cats to join, you appoint one cat as team captain. Then the following process repeats, starting with the captain. Cat ii selects its favourite partner pip_i and adds pip_i to the team. Then cat pip_i selects its favourite partner, adds ppip_{p_i} 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 kk cats, the game can be played.

Sometimes no team of kk 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 ii (1≤i≤n1 \leq i \leq n), choose xx (1≤x≤n1 \leq x \leq n), and update pi:=xp_i := x. After a change, the pip_i 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 nn and kk (1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5, 1≤k≤n1 \leq k \leq n), the number of cats and the team size. The second line contains nn integers p1,p2,…,pnp_1, p_2, \dots, p_n (1≤pi≤n1 \leq p_i \leq n), where the iith integer is the favourite playing partner of cat ii. It is guaranteed that the pip_i 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.

Examples2

  1. Example 1

    Input
    2 1
    2 1
    
    Expected output
    1
    
  2. Example 2

    Input
    5 5
    3 4 1 2 5
    
    Expected output
    2