Certainly, everyone is familiar with tic-tac-toe game. The rules are very simple indeed. Two players take turns marking the cells in a 3 × 3 grid (one player always draws crosses, the other — noughts). The player who succeeds first in placing three of his marks in a horizontal, vertical or diagonal line wins, and the game is finished. The player who draws crosses goes first. If the grid is filled, but neither Xs, nor 0s form the required line, a draw is announced.

You are given a 3 × 3 grid, each grid cell is empty, or occupied by a cross or a nought. You have to find the player (first or second), whose turn is next, or print one of the verdicts below: 
 -  illegal — if the given board layout can't appear during a valid game; -  the first player won — if in the given board layout the first player has just won; -  the second player won — if in the given board layout the second player has just won; -  draw — if the given board layout has just let to a draw.
The input consists of three lines, each of the lines contains characters ".", "X" or "0" (a period, a capital letter X, or a digit zero).
def transpose(board):
	zipped_rows = zip(*board) 
	transpose_matrix = [list(row) for row in zipped_rows]
	return transpose_matrix
def isFull(board):
	for i in range(len(board)):
		for j in range(len(board[i])):
			if board[i][j] == ".":
				return False
	return True
def won(board):
	if all(list(map(lambda x: x=="X",board[0]))) or all(list(map(lambda x: x=="X",board[1]))) or all(list(map(lambda x: x=="X",board[2]))) or all(list(map(lambda x: x=="X",transpose(board)[0]))) or all(list(map(lambda x: x=="X",transpose(board)[1]))) or all(list(map(lambda x: x=="X",transpose(board)[2]))) or (board[0][0] == "X" and board[1][1]=="X" and board[2][2] == "X") or (board[0][2] == "X" and board[1][1]=="X" and board[2][0] == "X"): return "the first player won"
	elif all(list(map(lambda x: x=="0",board[0]))) or all(list(map(lambda x: x=="0",board[1]))) or all(list(map(lambda x: x=="0",board[2]))) or all(list(map(lambda x: x=="0",transpose(board)[0]))) or all(list(map(lambda x: x=="0",transpose(board)[1]))) or all(list(map(lambda x: x=="0",transpose(board)[2]))) or (board[0][0] == "0" and board[1][1]=="0" and board[2][2] == "0") or (board[0][2] == "0" and board[1][1]=="0" and board[2][0] == "0"): return "the second player won"
	elif (all(list(map(lambda x: x=="X",board[0]))) or all(list(map(lambda x: x=="X",board[1]))) or all(list(map(lambda x: x=="X",board[2]))) or all(list(map(lambda x: x=="X",transpose(board)[0]))) or all(list(map(lambda x: x=="X",transpose(board)[1]))) or all(list(map(lambda x: x=="X",transpose(board)[2]))) or (board[0][0] == "X" and board[1][1]=="X" and board[2][2] == "X") or (board[0][2] == "X" and board[1][1]=="X" and board[2][0] == "X")) and (all(list(map(lambda x: x=="0",board[0]))) or all(list(map(lambda x: x=="0",board[1]))) or all(list(map(lambda x: x=="0",board[2]))) or all(list(map(lambda x: x=="0",transpose(board)[0]))) or all(list(map(lambda x: x=="0",transpose(board)[1]))) or all(list(map(lambda x: x=="0",transpose(board)[2]))) or (board[0][0] == "0" and board[1][1]=="0" and board[2][2] == "0") or (board[0][2] == "0" and board[1][1]=="0" and board[2][0] == "0")):
		return "both"
	elif isFull(board): return "draw"
	else: return None
def firstOrSecond(board):
	x = 0
	o = 0
	for i in range(len(board)):
		for j in range(len(board[i])):
			if board[i][j]=="X":
				x+=1
			elif board[i][j]=="0":
				o+=1
	return "first" if x==o else "second"
def illegal(board):
	x = 0
	o = 0
	if won(board) == "both" and not isFull(board):
		return True
	for i in range(len(board)):
		for j in range(len(board[i])):
			if board[i][j]=="X":
				x+=1
			elif board[i][j]=="0":
				o+=1
	if o>x: return True
	return False
li = []
for _ in range(3):
	li.append(list(input()))
if illegal(li):
	print("illegal")
elif won(li) is None:
	print(firstOrSecond(li))
else:
	print(won(li))