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).
import math
import random 
import time
from collections import defaultdict
 
 
alfabet = {'a': 1, 'b': 2,'c': 3,'d': 4,'e': 5,'f': 6,'g': 7,'h': 8,'i': 9,'j': 10,'k': 11,'l': 12,'m': 13,'n': 14,'o': 15,'p': 16,'q': 17,'r': 18,'s': 19,'t': 20,'u': 21,'v': 22,'w': 23,'x': 24,'y': 25,'z': 26}
alfabet_2={'1':"a", '2':"b", '3':"c", '4':"d", '5':"e", '6':"f", '7':"g", '8':"h", '9':"i", '10':"j", '11':"k", '12':"l", '13':"m", '14':"n", '15':"o", '16':"p", '17':"q", '18':"r", '19':"s", '20':"t", '21':"u", '22':"v", '23':"w", '24':"x", '25':"y", '26':"z"}
 
 
 
def prime_generator(nr_elemente_prime):
 
 vector_prime=[-1]*nr_elemente_prime
 vector_rasp=[0]*nr_elemente_prime
 
 vector_prime[1]=1
 
 vector_rasp[1]=1
#primes sieve 
 contor=2
 
 for i in range(2,nr_elemente_prime):
  if vector_prime[i]==-1:
   vector_prime[i]=1
   vector_rasp[contor]=i
   contor=contor+1
  for j in range(i+i,nr_elemente_prime,i):
    if vector_prime[j]==-1:
     vector_prime[j]=i
   #print(i,j)
   
   
 
 set_prime=set(vector_rasp)
 set_prime.remove(0)
 set_prime.remove(1)
 
 answ_prime=list(set_prime)
 answ_prime.sort()
 return answ_prime
 
def binary_search(vector,valoarea):
 
 left=0
 right=len(vector)-1
 
 while left<=right:
  
  
  centru=(left+right)//2
 # print(left,right,centru,vector[centru])
  
  if vector[centru]<=valoarea:
    left=centru+1
  else:
    right=centru-1
    
# print(left,right,centru,vector[centru])  
 return left
 
 
 
 
 
 
 
 
 
def main():
 answ=[]
 pp=10**9
# teste=int(input())
 
 matrice=[[] for x in range(3)]
 for gg in range(1):
  
  for tt in range(3):
   stringul=input()
  #n=len(stringul)
  #answer=''
  #n=int(input())
   for j in stringul:
    matrice[tt].append(j)
  
 # lista=list(map(int,input().split()))
  
  #print(matrice)
  
  cate_x=0
  cate_0=0
  
  win_x=0
  win_0=0
  
  for randuri in range(3):
   partial_x=0
   partial_0=0
   
   for j in range(3):
    if matrice[randuri][j]=='X':
     cate_x+=1
     partial_x+=1
     
    elif matrice[randuri][j]=='0':
     cate_0+=1
     partial_0+=1
     
   if partial_x==3:
    win_x+=1
   if partial_0==3:
    win_0+=1
    
  for coloane in range(3):
   partial_x=0
   partial_0=0
   for i in range(3):
    if matrice[i][coloane]=='X':
     partial_x+=1
     
    elif matrice[i][coloane]=='0':
     partial_0+=1
     
   if partial_x==3:
    win_x+=1
   if partial_0==3:
    win_0+=1  
    
  #print(cate_x,cate_0)
  #print(win_x,win_0)
 
 if matrice[0][0]=='X' and matrice[1][1]=='X' and matrice[2][2]=='X':
  win_x+=1
  
 if matrice[0][0]=='0' and matrice[1][1]=='0' and matrice[2][2]=='0':
  win_0+=1 
  
 if matrice[0][2]=='X' and matrice[1][1]=='X' and matrice[2][0]=='X':
  win_x+=1
  
 if matrice[0][2]=='0' and matrice[1][1]=='0' and matrice[2][0]=='0':
  win_0+=1  
 
  
 if win_x>2 or win_0>2 or win_x*win_0>=1 or cate_x>cate_0+1 or cate_0 >cate_x:
  print("illegal")
 else:
  if win_x>0 and cate_0>=cate_x:
   print("illegal")
  elif win_0>0 and cate_x>cate_0:
   print("illegal")
   
  
  elif win_x>0:
   print("the first player won")
  elif win_0>0:
   print("the second player won")
  elif cate_x+cate_0==9:
   print("draw") 
  elif cate_x>cate_0:
   print("second")
  else:
   print("first")
   
  
main()