#include using namespace std; int main(){ int N, M; cin >> N >> M; vector> E(N), E2(N); for (int i = 0; i < M; i++){ int U, V; cin >> U >> V; U--; V--; E[U].push_back(V); E2[U].push_back(V); E2[V].push_back(U); } vector d(N, 0); for (int i = 0; i < N; i++){ for (int j : E[i]){ d[j]++; } } queue Q; for (int i = 0; i < N; i++){ if (d[i] == 0){ Q.push(i); } } vector t; while (!Q.empty()){ int v = Q.front(); Q.pop(); t.push_back(v); for (int w : E[v]){ d[w]--; if (d[w] == 0){ Q.push(w); } } } int cnt = t.size(); vector pos(N, -1); for (int i = 0; i < cnt; i++){ pos[t[i]] = i; } int K = 0; vector A, B; vector used(N, false); for (int i = 0; i < N; i++){ if (!used[i]){ used[i] = true; vector id; queue Q2; Q2.push(i); while (!Q2.empty()){ int v = Q2.front(); Q2.pop(); id.push_back(v); for (int w : E2[v]){ if (!used[w]){ used[w] = true; Q2.push(w); } } } int cnt2 = id.size(); if (cnt2 > 1){ bool s = false; for (int j = 0; j < cnt2; j++){ if (pos[id[j]] == -1){ s = true; } } if (!s){ vector> P(cnt2); for (int j = 0; j < cnt2; j++){ P[j] = make_pair(pos[id[j]], id[j]); } sort(P.begin(), P.end()); for (int j = 0; j < cnt2 - 1; j++){ K++; A.push_back(P[j].second); B.push_back(P[j + 1].second); } } else { for (int j = 0; j < cnt2; j++){ int j2 = j + 1; if (j2 == cnt2){ j2 = 0; } K++; A.push_back(id[j]); B.push_back(id[j2]); } } } } } cout << K << endl; for (int i = 0; i < K; i++){ cout << A[i] + 1 << ' ' << B[i] + 1 << endl; } }