#include using namespace std; #define rep(i, a, n) for(int i=(a); i<(n); ++i) #define per(i, a, n) for(int i=(a); i>(n); --i) #define pb emplace_back #define mp make_pair #define clr(a, b) memset(a, b, sizeof(a)) #define all(x) (x).begin(),(x).end() #define lowbit(x) (x & -x) #define fi first #define se second #define lson o<<1 #define rson o<<1|1 #define gmid l[o]+r[o]>>1 using LL = long long; using ULL = unsigned long long; using pii = pair; using PLL = pair; using UI = unsigned int; const int mod = 1e9 + 7; const int inf = 0x3f3f3f3f; const double EPS = 1e-8; const double PI = acos(-1.0); const int N = 2e5 + 10; vector V[N], G[N], A[N]; vector ans; int n, m, deg[N], idx[N]; int com, sz[N]; void bfs(int x){ queue Q; Q.emplace(x); idx[x] = ++com; sz[com] = 1; while(!Q.empty()){ x = Q.front(); Q.pop(); G[com].pb(x); for(int j : G[x]){ if(!idx[j]){ idx[j] = com; ++sz[com]; Q.emplace(j); } } } } int main(){ scanf("%d %d", &n, &m); int x, y; clr(deg, 0); while(m--){ scanf("%d %d", &x, &y); V[x].pb(y); ++deg[y]; G[x].pb(y); G[y].pb(x); } clr(idx, 0); com = 0; rep(i, 1, n + 1){ if(!idx[i]){ bfs(i); } } queue Q; rep(i, 1, n + 1){ if(deg[i] == 0){ Q.emplace(i); } } while(!Q.empty()){ x = Q.front(); Q.pop(); --sz[idx[x]]; A[idx[x]].pb(x); for(int j : V[x]){ if(--deg[j] == 0){ Q.emplace(j); } } } rep(i, 1, com + 1){ if(sz[i] == 0){ rep(j, 1, A[i].size()){ ans.pb(A[i][j-1], A[i][j]); } } else { rep(j, 1, G[i].size()){ ans.pb(G[i][j-1], G[i][j]); } ans.pb(G[i].back(), G[i][0]); } } printf("%d\n", (int)ans.size()); for(pii p : ans) printf("%d %d\n", p.fi, p.se); return 0; }