#include /* #include #include namespace mp = boost::multiprecision; using bint = mp::cpp_int; */ #include #include #include #include #include #include #include #include #include #include #define rep(i,n) for (int i = 0; i < int(n); ++i) #define repp(i,n,m) for (int i = m; i < int(n); ++i) #define repb(i,n) for (int i = int(n)-1; i >= 0; --i) using namespace std; using namespace atcoder; using namespace internal; //alias g++='g++ -I/mnt/c/Users/Owner/Desktop/ac-library' using ll = long long; using ld = long double; using P = pair; using PI = pair,int>; using PL = pair; using PLL = pair, long long>; using Pxy = pair; const int INF = 1001001007; const int modd = 1000000007; const long long modl = 1000000007LL; const long long mod = 998244353LL; const ll inf = 2e18; template void priv(vector &ar){ if (ar.size() == 0) cout << endl; else { rep(i,ar.size()-1) cout << ar[i] << " "; cout << ar[ar.size()-1] << endl; } } template void privv(vector> &ar){ rep(i,ar.size()){ rep(j,ar[i].size()-1) cout << ar[i][j] << " "; cout << ar[i][ar[i].size()-1] << endl; } } template bool range(SC a, SC b, SC x){return (a <= x && x < b);} bool rrange(P a, P b, P xy){ bool s = range(a.first,b.first,xy.first); bool t = range(a.second,b.second,xy.second); return (s && t); } template void rev(vector &ar){reverse(ar.begin(),ar.end());} template void sor(vector &ar, int f = 0){sort(ar.begin(),ar.end()); if (f!=0) rev(ar);} template bool chmin(SF &a, const SF &b){if(a>b){a = b; return true;} return false;} template bool chmax(SG &a, const SG &b){if(a void eru(vector &ar){sor(ar);ar.erase(unique(ar.begin(),ar.end()),ar.end());} template SI last(vector &ar){return ar[ar.size()-1];} template SJ cel(SJ a, SJ b){if (a % b == 0) return a/b; return a/b +1;} template void pout(pair p) {cout << p.first << " " << p.second << endl;} void yes(){cout << "Yes" << endl;} void no (){cout << "No" << endl;} void yn (bool t){if(t)yes();else no();} void Yes(){cout << "YES" << endl;} void No (){cout << "NO" << endl;} void YN (bool t){if(t)Yes();else No();} void dout() {cout << setprecision(20);} vector dx = {0,1,0,-1}; vector dy = {1,0,-1,0}; const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const string alp = "abcdefghijklmnopqrstuvwxyz"; ll gcds(ll a, ll b){ ll c = a % b; while (c != 0){ a = b; b = c; c = a % b; } return b; } ll tentou(vector ar){ int n = ar.size(); set st; rep(i,n) st.insert(ar[i]); map mp; int ind = 0; for (ll x : st){ mp[x] = ind; ind++; } fenwick_tree fw(ind); ll ans = 0; rep(i,n){ int a = mp[ar[i]]; ans += i - fw.sum(0,a+1); fw.add(a,1); } return ans; } int main(){ int n, m; cin >> n >> m; dsu d(n); vector to(n,0); vector fr(n,0); rep(i,m){ int a, b; cin >> a >> b; a--; b--; fr[a]++; to[b]++; d.merge(a,b); } auto vec = d.groups(); set

ans; for (vector v : vec){ if (v.size() == 1) continue; int f = -1; int t = -1; for (int x : v){ if (to[x] == 0) f = x; if (fr[x] == 0) t = x; } vector p(0); if (f != -1) p.emplace_back(f); for (int x : v) if (x != f && x != t) p.emplace_back(x); if (t != -1) p.emplace_back(t); rep(i,p.size()-1) ans.insert(P(p[i],p[i+1])); if (f == -1 && t == -1) ans.insert(P(last(p),p[0])); } cout << ans.size() << endl; for (P x : ans) cout << x.first + 1 << " " << x.second + 1 << endl; }