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 C++14 (judge verdict: Accepted)

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
#define N 10010
#define M 100010
ll n,m,a[M],b[M];
vector<ll> g[N],inv[N];
ll belong[N],de[N],ce[N];
void dfs(ll x,ll root){
    belong[x]=root;
    for(auto y:inv[x]){
	if(~belong[y])continue;
	dfs(y,root);
    }
}
bool vis[N];
vector<ll> v;
void bfs(ll x){
    if(vis[x])return;
    vis[x]=1;
    for(auto y:g[x]){
	bfs(y);
    }
    v.push_back(x);
}
int main(){
    cin>>n>>m;
    for(int i=0;i<m;i++){
	cin>>a[i]>>b[i];
	g[a[i]].push_back(b[i]);
	inv[b[i]].push_back(a[i]);
    }
    for(int i=0;i<n;i++)vis[i]=0,belong[i]=-1;
    for(int i=0;i<n;i++){
	if(vis[i]==0)bfs(i);
    }
    ll k=0; reverse(v.begin(),v.end());
    for(auto x:v){
	if(~belong[x])continue;
	dfs(x,k);
	k++;
    }
    //for(int i=0;i<n;i++)cout<<i<<":"<<belong[i]<<endl;
    for(int i=0;i<k;i++)ce[i]=de[i]=0;
    for(int i=0;i<m;i++){
	if(belong[a[i]]!=belong[b[i]]){
	    ce[belong[a[i]]]++;
	    de[belong[b[i]]]++;
	}
    }
    if(k==1){
	cout<<0<<endl;
	return 0;
    }
    ll ca=0,cb=0;
    for(int i=0;i<k;i++){
	if(ce[i]==0)ca++;
	if(de[i]==0)cb++;
    }
    cout<<max(ca,cb)<<endl;
    return 0;
}