#include "bits/stdc++.h"

using namespace std;
using uint = unsigned int;
using int64 = long long;
using i64 = int64;
using uint64 = unsigned long long;
using vint = vector<int>;
using vint64 = vector<int64>;
template<typename T>
using p_que = priority_queue<T>;
#define rep(i,N) for(int i=0;i<(int)(N);++i)
#define rep1(i,N) for(int i=1;i<=(int)(N);++i)
#define fs first
#define sc second
#define e_b emplace_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()

const int INF = (1 << 30) - 1;
const long long INF64 = (1ll << 60) - 1;

template<typename T>
vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
	return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

template<class T> ostream& operator<<(ostream& os, const vector<T>& v) {
	os << "[";
	for (auto& e : v) os << e << ", ";
	return os << "]";
}

template<class T, class U> ostream& operator<<(ostream& os, const pair<T, U> p) {
	return os << "P(" << p.first << ", " << p.second << ")";
}

template<class T, class U> ostream& operator<<(ostream& os, const map<T, U>& mp) {
	os << "{";
	for (auto& e : mp) os << e << " ";
	return os << "}";
}

void Main();
signed main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(20);
	Main();
}

/* Cut from here! */

template <uint mod> struct ModInt {
	uint x;
	ModInt(int64_t y = 0) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
	ModInt& operator+=(const ModInt& p) {
		if ((x += p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt& operator-=(const ModInt& p) {
		x = (int)(1ll * x * p.x % mod);
		return *this;
	}
	ModInt& operator/=(const ModInt& p) {
		*this *= p.inverse();
		return *this;
	}
	ModInt operator-() const {
		return ModInt(-x);
	}
	ModInt operator+(const ModInt& p) const {
		return ModInt(*this) += p;
	}
	ModInt operator-(const ModInt& p) const {
		return ModInt(*this) -= p;
	}
	ModInt operator*(const ModInt& p) const {
		return ModInt(*this) *= p;
	}
	ModInt operator/(const ModInt& p) const {
		return ModInt(*this) /= p;
	}
	bool operator==(const ModInt& p) const {
		return x == p.x;
	}
	bool operator!=(const ModInt& p) const {
		return x != p.x;
	}
	ModInt inverse() const {
		int a = x, b = mod, u = 1, v = 0, t;
		while (b > 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(u -= t * v, v);
		}
		return ModInt(u);
	}
	ModInt pow(int64_t n) const {
		ModInt ret(1), mul(x);
		while (n > 0) {
			if (n & 1) ret *= mul;
			mul *= mul;
			n >>= 1;
		}return ret;
	}
	friend ostream& operator<<(ostream& os, const ModInt& p) {
		return os << p.x;
	}
};

template <typename Monoid> 
struct SegmentTree {
	using F = function<Monoid(Monoid, Monoid)>;
	int sz;
	vector<Monoid> seg;

	const F f;
	const Monoid M1;

	SegmentTree(int n, const F f, const Monoid& M1) : f(f), M1(M1) {
		sz = 1;
		while (sz < n) sz <<= 1;
		seg.assign(2 * sz, M1);
	}

	void set(int k, const Monoid& x) {
		seg[k + sz] = x;
	}

	void build() {
		for (int k = sz - 1; k > 0; --k) {
			seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
		}
	}

	void update(int k, const Monoid& x) {
		k += sz;
		seg[k] = x;
		while (k >>= 1) {
			seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
		}
	}

	Monoid query(int a, int b) {
		Monoid L = M1, R = M1;
		for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
			if (a & 1) L = f(L, seg[a++]);
			if (b & 1) R = f(seg[--b], R);
		}
		return f(L, R);
	}

	Monoid operator[](const int& k) const {
		return seg[k + sz];
	}
};

//#define int long long
const uint MOD = 1e9 + 7;

template<typename T>
T gcd(T x, T y) {
	if (x == 0 || y == 0) return 1;
	T r;
	while ((r = y % x) != 0) {
		y = x;
		x = r;
	}
	return x;
}

template <typename T>
struct edge {
	int src, to;
	T cost;

	edge(int to, T cost) : src(-1), to(to), cost(cost) {}
	edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
	edge& operator=(const int& x) {
		to = x;
		return *this;
	}
	operator int() const {
		return to;
	}
};

template <typename T>
using Edges = vector<edge<T>>;
template <typename T>
using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T>
using Matrix = vector<vector<T>>;


template <typename G>
struct StronglyConnectedComponents {
	const G& g;
	UnWeightedGraph gg, rg;
	vector<int> comp, order, used;

	StronglyConnectedComponents(G &g): g(g), gg(g.size()), rg(g.size()), comp(g.size(), -1), used(g.size()) {
		for(int i=0; i<g.size(); ++i) {
			for(auto &e : g[i]) {
				gg[i].e_b((int)e);
				rg[(int)e].emplace_back(i);
			}
		}
	}
	
	int operator[](int k) {
		return comp[k];
	}
	
	void dfs(int idx) {
		if(used[idx]) return;
		used[idx] = true;
		for(int to : gg[idx]) dfs(to);
		order.emplace_back(idx);
	}
	
	void rdfs(int idx, int cnt) {
		if(comp[idx] != -1) return;
		comp[idx] = cnt;
		for(int to : rg[idx]) rdfs(to, cnt);
	}
	
	void build(UnWeightedGraph &t) {
		for(int i= 0; i < gg.size(); ++i) dfs(i);
		reverse(begin(order), end(order));
		int ptr = 0;
		for(int i : order) if(comp[i] == -1) rdfs(i, ptr), ptr++;
		
		t.resize(ptr);
		for(int i=0; i<g.size(); ++i) {
			for(auto &to : g[i]) {
				int x = comp[i], y = comp[to];
				if(x == y) continue;
				t[x].e_b(y);
			}
		}
	}
};

void Main() {
	int N, M; cin >> N >> M;
	UnWeightedGraph g(N);
	rep(i, M) {
		int s, t; cin >> s >> t;
		g[s].e_b(t);
	}
	
	StronglyConnectedComponents<UnWeightedGraph> scc(g);
	UnWeightedGraph buff;
	scc.build(buff);
	
	vector<int> a(buff.size()), b(buff.size());
	for(int i=0; i<buff.size(); ++i) {
		for(auto &e : buff[i]) {
			a[e] = 1;
			b[i] = 1;
		}
	}
	
	int acnt = 0, bcnt = 0;
	rep(i, buff.size()) {
		if(!a[i]) acnt++;
		if(!b[i]) bcnt++;
	}
	
	if(buff.size() == 1) cout << 0 << endl;
	else cout << max(acnt, bcnt) << endl;
}


Measured memory_bytes: 6820