Problem:
Road Improvement

Aizu is a country famous for its rich tourism resources and has N cities, each of which is uniquely identified with a number (0 to N-1). It has a road network consisting of M one-way roads connecting a city to another.

All the roads connecting the cities in Aizu have a row of cherry trees along their routes. For enhancing the cherry-viewing experience, a proposal was made to modify the road network so that a tourist can travel around all the roads. To achieve this target, it was decided to construct several one-way roads, each connecting two cities and abiding by the following rules.

The newly constructed road is for one-way traffic

Starting from any city, a tourist is able to make a roundtrip and return to the city, whereby he/she drives all the roads exhaustively, including the newly constructed ones. Multiple passages of some of the roads are allowed.

You, as a tourism promotion officer, are assigned with the task of writing a program for the road construction project.

Write a program to determine the minimum number of roads to be constructed given the road network information in Aizu.

Input

The input is given in the following format.

N M
s_1 t_1
s_2 t_2
:
s_M t_M

The first line provides the number of cities N (1 ≤ N ≤ 104) and roads M (0 ≤ M ≤ 105). Each of the subsequent M lines provides the numbers assigned to start and destination cities for the i-th road: s_i, t_i (0 ≤ s_i, t_i ≤ N-1) , where s_i ≠ t_i. (no duplicate appearance of a road)

Output

Output the minimum number of roads to be newly constructed.

Sample Input 1

6 7
0 2
2 1
1 0
2 3
4 3
4 5
5 4

Sample Output 1

2

Sample Input 2

6 9
0 2
2 1
1 0
2 3
4 3
4 5
5 4
5 2
3 4

Sample Output 2

0

Solution in Ruby (judge verdict: Runtime Error)

require 'tsort'
class Hash
    include TSort
    alias tsort_each_node each_key
    def tsort_each_child(node, &block)
        fetch(node).each(&block)
    end
end

n, m = gets.split.map(&:to_i)
g = {}
g_inv = {}
(0..n-1).each do |i|
    g[i] = []
    g_inv[i] = []
end
edges = []
m.times do
    i, j = gets.split.map(&:to_i)
    edges << [i, j]
    g[i] << j
    g_inv[j] << i
end

count = 0
top = bottom = 0
g.each_strongly_connected_component do |scc|
    count += 1
    bottom += 1 if scc.all? {|i| (g[i] - scc).empty?}
    top += 1 if scc.all? {|i| (g_inv[i] - scc).empty?}
end
puts count == 1 ? 0 : [top, bottom].max