def SCC_Tarjan(g): n = len(g) order = [-1]*n # 負なら未処理、[0,n) ならpre-order, n ならvisited low = [0]*n ord_now = 0 parent = [-1]*n gp = [0]*n gp_num = 0 S = [] q = [] for i in range(n): if order[i] == -1: q.append(i) while q: v = q.pop() if v >= 0: if order[v] != -1: continue order[v] = low[v] = ord_now ord_now += 1 S.append(v) q.append(~v) for c in g[v]: if order[c] == -1: q.append(c) parent[c] = v else: low[v] = min(low[v], order[c]) else: v = ~v if parent[v] != -1: low[parent[v]] = min(low[parent[v]], low[v]) if low[v] == order[v]: while True: w = S.pop() order[w] = n gp[w] = gp_num if w==v: break gp_num += 1 scc = [[] for _ in range(gp_num)] for i in range(n): gp[i] = gp_num-gp[i]-1 scc[gp[i]].append(i) #print(gp) return scc class UnionFind: def __init__(self, n): self.parent = list(range(n)) #親ノード self.size = [1]*n #グループの要素数 def root(self, x): #root(x): xの根ノードを返す. while self.parent[x] != x: self.parent[x] = self.parent[self.parent[x]] x = self.parent[x] return x def merge(self, x, y): #merge(x,y): xのいる組とyのいる組をまとめる x, y = self.root(x), self.root(y) if x == y: return False if self.size[x] < self.size[y]: x,y=y,x #xの要素数が大きいように self.size[x] += self.size[y] #xの要素数を更新 self.parent[y] = x #yをxにつなぐ return True def issame(self, x, y): #same(x,y): xとyが同じ組ならTrue return self.root(x) == self.root(y) def getsize(self,x): #size(x): xのいるグループの要素数を返す return self.size[self.root(x)] import sys readline = sys.stdin.readline n,m = map(int,readline().split()) g = [[] for _ in range(n)] UF = UnionFind(n) for _ in range(m): a,b = map(int,readline().split()) UF.merge(a-1,b-1) g[a-1].append(b-1) scc = SCC_Tarjan(g) top = [] ans = [] for lst in scc: top.append(lst[0]) if len(lst) >= 2: for i in range(len(lst)): ans.append((lst[i],lst[i-1])) for i,j in zip(top,top[1:]): if UF.issame(i,j): ans.append((i,j)) print(len(ans)) for i,j in ans: print(i+1,j+1)