Link

Time limit2sMemory limit64 MB

Summary
Given a functional graph where each node has exactly one outgoing edge, find the minimum number of edges to add so every node is reachable from node 1 within K hops.
Level

Medium7 of 10

Topics
Graph, Greedy, DFS
Solved
No attempts yet

Problem

A school website is being reorganized. It has NN pages, numbered from 11 to NN, and the homepage is page 11. The content is fine, but the pages are linked poorly: every page contains exactly one link, and that link points to some other (different) page. Because of this, a visitor who starts at the homepage often has to follow many links to reach a given page, and some pages may not be reachable from the homepage at all.

To improve this, new links may be added anywhere: a new link may be placed on any page and may point to any page. For an integer KK, the website is called KK-reachable if every page other than the homepage can be reached from the homepage by following at most KK links.

Given the website and the integer KK, compute the minimum number of links that must be added to make the website KK-reachable.

Input

The first line contains two integers NN and KK (2≤N≤500 0002 \le N \le 500\,000, 1≤K≤20 0001 \le K \le 20\,000): the number of pages and the maximum number of links a visitor may follow.

Each of the next NN lines contains two different integers AA and BB (1≤A,B≤N1 \le A, B \le N), meaning that the single link on page AA points to page BB.

Output

Print a single integer: the minimum number of additional links needed to make the website KK-reachable.

Examples2

  1. Example 1

    Input
    8 3
    1 2
    2 3
    3 5
    4 5
    5 6
    6 7
    7 8
    8 5
    
    Expected output
    2
    
  2. Example 2

    Input
    14 4
    1 2
    2 3
    3 4
    4 5
    7 5
    5 6
    6 3
    8 10
    10 9
    9 8
    14 13
    13 12
    12 11
    11 14
    
    Expected output
    3