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
