#!/usr/bin/env python
#copyright Igor Rivin, 2005, all rights reserved.
#computes the 1-skeleton of the dual of a simplicial complex (in
#english: vertices correspond to simplices, two are connected by an
#edge if the two simplices share a top-dimensional face.
#Each line in the input represent a simplex, which is simply a list of
#ordinal numbers of vertices. The output is: first, the number of
#simplices, then a set of lines giving the numbering of the simplices,
#then the adjacency list.

import sys
import string
import os
import math
import random


def nless1subs(seq):
	return [seq[:i] + seq[(i+1):] for i in xrange(len(seq))]

# makes the list of faces of a simplex.
def makentrys(thesimplex):
	faces = nless1subs(thesimplex)
	[i.sort() for i in faces]
	return map(tuple, faces)

#facedict is a hashtable indexed by the n-1 dimensional faces, where
#each bucket contains the list of the n-dimensional simplices incident
#to the n-1-dimensional face.

def incidence(simplist):
	ifaces = [ makentrys(x) for x in simplist] #the list of faces for each simplex.
        facedict = {}
	for i in xrange(len(simplist)):
		for f in ifaces[i]:
			if f in facedict:
				facedict[f].append(i)
			else:
				facedict[f] = [i]
	return ifaces, facedict

def flatten(listolists):
	if len(listolists) == 0:
		return listolists
	return reduce(lambda x, y : x+y, listolists)


#we have the option of rejecting some faces using the filter given by
#filterfunc.

def getfriends(thefacenum, ifaces, facedict, filterfunc = None):
	if filterfunc == None:
		facedicts = [facedict[i] for i in ifaces[thefacenum]]
	else:
		facedicts = [facedict[i] for i in ifaces[thefacenum]
			     if filterfunc(i)]
	unsorted = flatten([[j for j in i if j!= thefacenum] for i in facedicts])
	unsorted.sort()
	return unsorted

def makeadjacency(ifaces, facedict, filterfunc = None):
	return [x for x in [(i, getfriends(i, ifaces, facedict, filterfunc))
		      for i in xrange(len(ifaces))] if len(x[1]) > 0]

#prints a 2-ple consisting of a simplex and its neighbors.

def simpconvert(simpline):
	thesimp = simpline[0]
	friends = simpline[1]
	outstr = '%d' % thesimp
	for i in friends:
		outstr += '\t%d' % i
	print outstr

def main(argv=None):
	if argv is None:
		argv = sys.argv
	prepresimps = [x.strip() for x in sys.stdin.readlines()]
	print len(prepresimps)
	for i in xrange(len(prepresimps)):
		newstr = '%d\t' % i + prepresimps[i]
		print newstr
	presimps = [x.split() for x in  prepresimps]
	simps = map(lambda x: map(string.atoi, x), presimps)
	d1, d2 = incidence(simps)
	map(simpconvert, makeadjacency(d1, d2))
	return 0

if __name__ == '__main__':
	sys.exit(main(sys.argv))
