from collections import defaultdict
x, _ =[int(i)for i ininput().split()]
pair =[]for _ inrange(x):
tmp =[int(i)for i ininput().split()]
pair.append(tmp)# 用来记录每个结点的父亲
memo = defaultdict(int)# 递归查找父亲结点deffather(b):if b notin memo:return b
else:return father(memo[b])for a, b in pair:# 保证a比较小if a>b:
a, b = b, a
# 查找b的父亲
tmp = father(a)# memo[b] = tmp
memo[b]= tmp
# 把同一个父亲的整理到一起
res = defaultdict(list)for sun, father in memo.items():if res.get(father,0)==0:if father != sun:
res[father]=[father, sun]else:
res[father]=[sun]else:
res[father].append(sun)
ans =[]# print('res:', res)for key, val in res.items():# 同一个小区内的升序
ans.append(sorted(val))# 不同小区间的升序
ans.sort(key=lambda x:x[0])
count =len(ans)print(count)for item in ans:print(' '.join(map(str,item)))