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).

board = []
verdict = 'draw'
for i in range(3):
	board.append(input())
xwon = 'the first player won'
owon = 'the second player won'
err = 'illegal'
fst = 'first'
snd = 'second'

x = 'X'
o = '0'
b = '.'

cx, co, cb = 0, 0 ,0
#Count X's and O's and blanks
for row in board:
	for letter in row:
		if letter == x:
			cx += 1
		elif letter == o:
			co += 1
		elif letter == b:
			cb += 1

#First or (x) is next
if (cx == co or cb == 9):
	verdict = fst

#Second or (o) is next
elif cx == co+1:
	verdict = snd
#Illegal
else:
	verdict = err

#Winning Horizontal
if verdict != err:
	xhorri = 0
	ohorri = 0
	for row in board:
		if row == (x+x+x):
			verdict = xwon
			xhorri += 1
		if row == (o+o+o):
			verdict = owon
			ohorri += 1
	if xhorri > 1 or ohorri > 1 or (xhorri+ohorri > 1):
		verdict = err

#Winning Vertical
if verdict != err:
	xvert = 0
	overt = 0
	for i in range(3):
		if board[0][i] == x and board[1][i] == x and board[2][i] == x:
			verdict = xwon
			xvert += 1
		if board[0][i] == o and board[1][i] == o and board[2][i] == o:
			verdict = owon
			overt += 1
	if xvert > 1 or overt > 1 or (xvert + overt > 1):
		verdict = err

#Winning Diagonal
if verdict != err:
	if board[0][0] == o and board[1][1] == o and board[2][2] == o:
		verdict = owon
	if board[0][2] == o and board[1][1] == o and board[2][1] == o:
		verdict = owon
	if board[0][0] == x and board[1][1] == x and board[2][2] == x:
		verdict = xwon
	if board[0][2] == x and board[1][1] == x and board[2][1] == x:
		verdict = xwon

#Draw
if verdict != err and verdict != xwon and verdict != owon and cb == 0:
	verdict = 'draw'

#First player plays after second wins
if verdict == owon and cx > co:
	verdict = err

#Second player plays after first wins
if verdict == xwon and cx <= co:
	verdict = err

print(verdict)





