from collections import deque class Unionfind: def __init__(self,n): self.uf = [-1]*n def find(self,x): if self.uf[x] < 0: return x else: self.uf[x] = self.find(self.uf[x]) return self.uf[x] def same(self,x,y): return self.find(x) == self.find(y) def union(self,x,y): x = self.find(x) y = self.find(y) if x == y: return False if self.uf[x] > self.uf[y]: x,y = y,x self.uf[x] += self.uf[y] self.uf[y] = x return True def size(self,x): x = self.find(x) return -self.uf[x] n,m = map(int,input().split()) uf = Unionfind(n+1) e = [[] for i in range(n+1)] deg = [0]*(n+1) use = [0]*(n+1) for _ in range(m): u,v = map(int,input().split()) e[u].append(v) uf.union(u,v) use[u] = 1 use[v] = 1 deg[v] += 1 group = [[] for i in range(n+1)] for i in range(n+1): if use[i]: group[uf.find(i)].append(i) ans = [] for i in range(n+1): if group[i] == []: continue q = deque([]) loop = 0 for ind in group[i]: if deg[ind]: continue q.append(ind) order = [] while q: now = q.pop() order.append(now) for nex in e[now]: deg[nex] -= 1 if deg[nex] == 0: q.append(nex) if len(order) == len(group[i]): for x,y in zip(order,order[1:]): ans.append((x,y)) else: for j in range(len(group[i])): x = group[i][j] y = group[i][(j+1)%len(group[i])] ans.append((x,y)) print(len(ans)) for x,y in ans: print(x,y)