Under construction

Still being built. Things may change

Highways of the Future

Interview

Time limit6sMemory limit2048 MB

Summary
Given a directed graph of sectors and one-way highways, find the fewest new directed highways needed so every sector can reach every other sector.
Level

Medium6 of 10

Topics
Graph, DFS, Topological sort
Solved
No attempts yet

Problem

Midgard is a city of mana energy and the center of the world's economy. Under President Shinda, chair of the Shinda Electric Power Company, the great city has enjoyed an age of prosperity. Recently, however, reports say that a group of bandits called Snowfall has been attacking and shutting down its mana reactors. To prepare for another attack, President Shinda has assigned you the role of chief engineer in a restructuring plan.

The city consists of nn sectors, and each sector has a mana reactor: an enormous facility that extracts energy from deep within the earth and transforms it into electricity that powers the whole sector. Currently, there are mm highways between sectors. Each highway can transport electric power from one sector to another, and it has only one direction. The president has instructed you to build new highways between sectors so that, no matter which reactors are shut down, the entire city still has electrical power as long as at least 11 reactor is functioning.

Each reactor has unlimited capacity for mana energy and can supply any number of sectors as long as it is directly or indirectly connected to them. Building a highway is expensive, so the president has also instructed you to build as few highways as possible while still meeting the previous condition.

Input

The input consists of:

  • A line containing two integers nn (1≤n≤1051\leq n\leq 10^5) and mm (0≤m≤2⋅1050\leq m\leq 2\cdot 10^5), the number of sectors in Midgard and the number of existing highways.
  • Then follow mm lines containing two integers each, xx and yy (1≤x,y≤n1 \leq x,y \leq n), which indicate a one-directional highway from sector xx to sector yy.

Output

Output the minimum number of highways that must be added so that any reactor can power every sector.

Examples3

  1. Example 1

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

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

    Input
    9 9
    1 3
    5 1
    2 5
    2 6
    3 4
    3 5
    8 5
    7 3
    3 7
    
    Expected output
    3