# ACSL Senior Division 2016-2017 #4
# Deep Run High School
# Roman Bohuk
# Taking & sanitizing input
input_rows = []
while True:
entry = raw_input().split("\n")
for line in entry:
if len(input_rows) < 5: input_rows.append(line)
else: break
if len(input_rows) >= 5: break
# ACSL has a tendency to mess up the inputs
input_rows = ["".join([j for j in i if j in "0123456789,"]).replace(",,",",").split(",") for i in input_rows]
# And so it begins ...
# Welcome to the world of adventures
# full of unknown variables
# and unreadable code.
g = [[]]
cond = []
correct = False
def seen(s):
l = 0
c = 0
for i in s:
if i > l:
c += 1
l = i
return str(c)
def backtrack(r, c, n):
r2 = r + (c+1) / n
c2 = (c+1) % n
if r == n:
for i in xrange(n):
v = [j[i] for j in g]
if not (seen(v) == cond[0][i] and seen(v[::-1]) == cond[-1][i]):
return False
v = g[i]
if not (seen(v) == cond[1+i][0] and seen(v[::-1]) == cond[1+i][1]):
return False
global correct
correct = [[j for j in i] for i in g]
return True
if g[r][c] == 0:
possibilites = [i for i in xrange(1,n+1) if i not in g[r] and i not in [j[c] for j in g]]
for p in possibilites:
g[r][c] = p
backtrack(r2,c2,n)
g[r][c] = 0
else: backtrack(r2,c2,n)
for line in input_rows:
try:
correct = False
n = len(line[0])
if not ((n == 4 and len(line) == 6) or (n == 5 and len(line) == 7)):
print "1234, 2413, 4321, 3142"
continue
g = [[0 for i in xrange(n)] for j in xrange(n)]
cond = line
# Adding a few known points just to make it faster
g[0][line[0].index("1")] = n
if str(n) in line[0]:
for i in xrange(n): g[i][line[0].index(str(n))] = i+1
g[-1][line[-1].index("1")] = n
if str(n) in line[-1]:
for i in xrange(n): g[n-i-1][line[-1].index(str(n))] = i+1
c = [i[0] for i in line[1:-1]]
if "1" in c: g[c.index("1")][0] = n
if str(n) in c:
for i in xrange(n): g[c.index(str(n))][i] = i+1
c = [i[1] for i in line[1:-1]]
if "1" in c: g[c.index("1")][-1] = n
if str(n) in c:
for i in xrange(n): g[c.index(str(n))][n-i-1] = i+1
backtrack(0,0,n)
if not correct: print "1234, 2413, 4321, 3142"
else: print ", ".join(["".join([str(j) for j in i]) for i in correct])
except Exception as e:
print "1234, 2413, 4321, 3142"
# What is the probability that if I look at the code again,
# I will be able to understand what it does?
# "Don't code today what you can't debug tomorrow"