#include "bits/stdc++.h" #include "atcoder/all" using namespace std; using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; //using mint = modint998244353; //const int mod = 998244353; //const int INF = 1e9; //const long long LINF = 1e18; //const bool debug = false; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" #define P pair template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } vectorto[200005]; map>mp; int h[200005]; std::vectortopological_sort(int leader) { for (auto v : mp[leader]) for (auto e : to[v]) h[e]++; std::stackst; std::vectorans; for (auto v : mp[leader])if (h[v] == 0) st.push(v); while (!st.empty()) { int v = st.top(); st.pop(); ans.push_back(v); for (auto u : to[v]) { --h[u]; if (h[u] == 0) st.push(u); } } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vectoru(m), v(m); dsu uf(n); rep(i, m) { cin >> u[i] >> v[i]; u[i]--; v[i]--; to[u[i]].push_back(v[i]); uf.merge(u[i], v[i]); } rep(i, m) { mp[uf.leader(u[i])].insert(u[i]); mp[uf.leader(u[i])].insert(v[i]); } vector>ans; rep(i, n) if (i == uf.leader(i)) { auto v = topological_sort(i); if (mp[i].size() == v.size()) { rep(j, (int)v.size() - 1) ans.push_back({ v[j] + 1,v[j + 1] + 1 }); } else { vectorv; for (auto j : mp[i])v.push_back(j); rep(j, v.size())ans.push_back({ v[j] + 1,v[(j + 1) % mp[i].size()] + 1 }); } /*rep(i, v.size()) { cout << v[i] << " "; } cout << endl;*/ } cout << ans.size() << endl; rep(i, ans.size()) cout << ans[i].first << " " << ans[i].second << endl; return 0; }