#include #include #include #include using namespace std; vector v[200009]; vector > r[200009], ans; int id[200009], ds[200009], p[200009]; queue q; int uf(int n) { if (ds[n] == n) return n; return ds[n] = uf(ds[n]); } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) ds[i] = i; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; v[a].push_back(b); id[b]++; int ua = uf(a), ub = uf(b); if (ua != ub) ds[ua] = ub; } for (int i = 1; i <= n; i++) if (!id[i]) q.push(i); int c = 0; while (!q.empty()) { int hn = q.front(); q.pop(); c++; p[hn] = c; for (int i = 0; i < v[hn].size(); i++) { int tn = v[hn][i]; id[tn]--; if (!id[tn]) q.push(tn); } } for (int i = 1; i <= n; i++) r[uf(i)].push_back(make_pair(p[i], i)); for (int i = 1; i <= n; i++) { if (ds[i] != i) continue; sort(r[i].begin(), r[i].end()); for (int j = 0; j + 1 < r[i].size(); j++) ans.push_back(make_pair(r[i][j].second, r[i][j + 1].second)); if (!r[i][0].first) ans.push_back(make_pair(r[i].back().second, r[i][0].second)); } cout << ans.size() << '\n'; for (int i = 0; i < ans.size(); i++) cout << ans[i].first << " " << ans[i].second << '\n'; return 0; }