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

Print one of the six verdicts: first, second, illegal, the first player won, the second player won or draw.

#include<bits/stdc++.h>
using namespace std;
bool win(vector<string>v,char c)
{
	for(int i=0;i<3;i++)
	{
		if(v[i][0]==c && v[i][0]==v[i][1] && v[i][0]==v[i][2])
		{
			return true;
		}
	}
	for(int i=0;i<3;i++)
	{
		if(v[0][i]==c && v[0][i]==v[1][i] && v[0][i]==v[2][i])
		{
			return true;
		}
	}
	if(v[0][0]==c && v[0][0]==v[1][1] && v[0][0]==v[2][2])
	{
		return true;
	}
	if(v[0][2]==c && v[0][2]==v[1][1] && v[0][2]==v[2][0])
	{
		return true;
	}
	return false;
}
bool dr(vector<string>v)
{
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
		{
			if(v[i][j]=='.')
			{
				return false;
			}
		}
	}
	return true;
}
set<vector<string> >l;
char ws(vector<string>v)
{
	int cnt=0;
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
		{
			if(v[i][j]!='.')
			{
				cnt++;
			}
		}
	}
	if(cnt&1)
	{
		return '0';
	}
	else
	{
		return 'X';
	}
}
void dfs(vector<string>v)
{
	if(l.count(v))
	{
		return;
	}
	l.insert(v);
	if(win(v,'X') || win(v,'0'))
	{
		return;
	}
	if(dr(v))
	{
		return;
	}
	char c=ws(v);
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
		{
			if(v[i][j]=='.')
			{
				v[i][j]=c;
				dfs(v);
				v[i][j]='.';
			}
		}
	}
}
int main()
{
	vector<string>v;
	for(int i=0;i<3;i++)
	{
		v.push_back("...");
	}
	dfs(v);
	for(int i=0;i<3;i++)
	{
		string s;
		cin>>s;
		v[i]=s;
	}
	if(l.count(v))
	{
		if(win(v,'X'))
		{
			puts("the first player won");
		}
		else if(win(v,'0'))
		{
			puts("the second player won");
		}
		else if(dr(v))
		{
			puts("draw");
		}
		else if(ws(v)=='X')
		{
			puts("first");
		}
		else
		{
			puts("second");
		}
	}
	else
	{
		puts("illegal");
	}
	return 0;
}

#include<iostream>
#include<string>
#include<set>
using namespace std;

struct pos
    {
     int a[3][3];
     pos()
         {
          for(int i=0;i<3;i++)
          for(int j=0;j<3;j++)
              a[i][j]=0;
         }
     int typ(pos p)
         {
          int res=0,c=0;
          for(int i=0;i<3;i++)
          for(int j=0;j<3;j++)
              {
               res^=a[i][j]<<(2*c);
               c++;
              }
          return res;
         }
     int win(pos p)
         {
          for(int i=0;i<3;i++)
              if(a[i][0] and a[i][0]==a[i][1] and a[i][0]==a[i][2]) return a[i][0];
          for(int i=0;i<3;i++)
              if(a[0][i] and a[0][i]==a[1][i] and a[0][i]==a[2][i]) return a[0][i];
          if(a[0][0] and a[0][0]==a[1][1] and a[0][0]==a[2][2]) return a[0][0];
          if(a[0][2] and a[0][2]==a[1][1] and a[0][2]==a[2][0]) return a[0][2];
          return 0;
         }
     bool draw(pos p)
         {
          for(int i=0;i<3;i++)
          for(int j=0;j<3;j++)
              if(!a[i][j]) return false;
          return true;
         }
     int who(pos p)
         {
          int b=0;
          for(int i=0;i<3;i++)
          for(int j=0;j<3;j++)
              {
               if(a[i][j]==1) b++;
               if(a[i][j]==2) b--;
              }
          return b==0?1:2;
         }
    };

set<int> fi,se,fiw,sew,dr;
void dfs(pos p)
    {
     int typ=p.typ(p);
     int win=p.win(p);
     if(win) {
              if(win==1) fiw.insert(typ);
              if(win==2) sew.insert(typ);
              return;
             }
     int draw=p.draw(p);
     if(draw) {
               dr.insert(typ);
               return;
              }
     int who=p.who(p);
     if(who==1) fi.insert(typ);
     else se.insert(typ);
     for(int i=0;i<3;i++)
     for(int j=0;j<3;j++)
         {
          if(p.a[i][j]) continue;
          p.a[i][j]=who;
          dfs(p);
          p.a[i][j]=0;
         }
    }

int main()
{

pos p;
dfs(p);
pos q;
for(int i=0;i<3;i++)
    {
     string s;
     cin>>s;
     for(int j=0;j<3;j++)
         {
          if(s[j]=='X') q.a[i][j]=1;
          else if(s[j]=='0') q.a[i][j]=2;
          else q.a[i][j]=0;
         }
    }

int x=q.typ(q);
if(fi.count(x)) puts("first");
else if(se.count(x)) puts("second");
else if(fiw.count(x)) puts("the first player won");
else if(sew.count(x)) puts("the second player won");
else if(dr.count(x)) puts("draw");
else puts("illegal");

return 0;
}

//////////directories//////////

#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <cmath>

//////////defines//////////

//#define tests
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define s short
#define no cout<<"NO\n"
#define yes cout<<"YES\n"
#define c1 cout << 1 << endl
#define c0 cout << 0 << endl

//////////namespace//////////

using namespace std;

/////////program///////////

void solve()
{
	int i, j;

	int x = 0;
	int o = 0;

	int xwin = 0;
	int owin = 0;

	string t[3];
	cin >> t[0] >> t[1] >> t[2];

	for (i = 0; i < 3; i++)
	{
		if (t[i] == "XXX") xwin++;
		if (t[i] == "000") owin++;
		if (t[0][i] == t[1][i] && t[0][i] == t[2][i])
		{
			if (t[0][i] == 'X') xwin++;
			else if (t[0][i] == '0') owin++;
		}
	}

	if (t[0][0] == t[1][1] && t[0][0] == t[2][2])
	{
		if (t[0][0] == 'X')xwin++;
		else if (t[0][0] == '0') owin++;
	}

	if (t[0][2] == t[1][1] && t[0][2] == t[2][0])
	{
		if (t[0][2] == 'X')xwin++;
		else if (t[0][2] == '0')owin++;
	}

	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 3; j++)
		{
			if (t[i][j] == 'X') x++;
			else if (t[i][j] == '0')o++;
		}
	}

	if ( (xwin && owin) || o > x || (x > o && x != o + 1))
	{
		cout << "illegal";
		return;
	}

	if (x + o == 9 && !xwin && !owin)
	{
		cout << "draw";
		return;
	}

	if (xwin)
	{
		if( o != x) cout << " the first player won";
		else cout << "illegal";
		return;
	}

	if (owin)
	{
		if( o == x) cout << " the second player won";
		else  cout << "illegal";
		return;
	}

	if (x == o) cout << "first";
	else  cout << "second";

}

//////////main func////////

int main()
{
	fastio;
#ifdef tests
	int t;
	cin >> t;
	while (t--)
		solve();
#else
	solve();
#endif

}

#include<bits/stdc++.h>
using namespace std;
#define err "illegal"
#define nexta "first"
#define nextb "second"
#define awin "the first player won"
#define bwin "the second player won"
#define dr "draw"
#define ret(x) return x
using namespace std;
char c[3][3];
int tota,totb;
bool as,bs,ah,bh,ax1,ax2,bx1,bx2,ga,gb;
int main(){
	for(int i=0;i<3;++i)
		for(int j=0;j<3;++j){
			cin>>c[i][j];
			if(c[i][j]=='X') ++tota;
			if(c[i][j]=='0') ++totb;
		}
	if(tota<totb){
		cout<<err;ret(0);
	}
	if(totb+1<tota){
		cout<<err;ret(0);
	}
	for(int i=0;i<3;++i)
		if(c[0][i]=='X'&&c[1][i]=='X'&&c[2][i]=='X'){
			as=true;break;
		}
	for(int i=0;i<3;++i)
		if(c[0][i]=='0'&&c[1][i]=='0'&&c[2][i]=='0'){
			bs=true;break;
		}
	for(int i=0;i<3;++i)
		if(c[i][0]=='X'&&c[i][1]=='X'&&c[i][2]=='X'){
			ah=true;break;
		}
	for(int i=0;i<3;++i)
		if(c[i][0]=='0'&&c[i][1]=='0'&&c[i][2]=='0'){
			bs=true;break;
		}
	if(c[0][0]=='X'&&c[1][1]=='X'&&c[2][2]=='X')ax1=true;
	if(c[0][0]=='0'&&c[1][1]=='0'&&c[2][2]=='0')bx1=true;
	if(c[0][2]=='X'&&c[1][1]=='X'&&c[2][0]=='X')ax2=true;
	if(c[0][2]=='0'&&c[1][1]=='0'&&c[2][0]=='0')bx2=true;
	ga=as||ah||ax1||ax2;
	gb=bs||bh||bx1||bx2;
	if(ga&&gb||ga&&tota==totb||gb&&tota>totb){
		cout<<err;ret(0);
	}
	if(ga||gb){
		if(ga) cout<<awin;
		else cout<<bwin;
		ret(0);
	}
	if(tota+totb==9){
		cout<<dr;ret(0);
	}
	if(tota==totb) cout<<nexta;
	else cout<<nextb;
	ret(0);
}

#include<bits/stdc++.h>
using namespace std;
char a[3][3];
bool bo=false,bx=false;
int io,ix;
bool f(int i,char c)
{
	return (a[i][0]==c&&a[i][1]==c&&a[i][2]==c)||(a[0][i]==c&&a[1][i]==c&&a[2][i]==c)||(a[0][0]==c&&a[1][1]==c&&a[2][2]==c)||(a[0][2]==c&&a[1][1]==c&&a[2][0]==c);
}
int main()
{
    for(int i=0;i<3;i++)
    for(int j=0;j<3;j++)
	{
		cin>>a[i][j];
		if(a[i][j]=='0')
		io++;
		if(a[i][j]=='X')
		ix++;
	}
    for(int i=0;i<3;i++)
	{
		bo=bo||f(i,'0');
		bx=bx||f(i,'X');
	}
    if((bx&&ix!=io+1)||(bo&&ix!=io)||(ix!=io+1&&ix!=io))
	puts("illegal");
    else if(bx)
	puts("the first player won");
    else if(bo)
	puts("the second player won");
    else if(ix+io==9)
	puts("draw");
    else if(ix==io)
	puts("first");
    else
	puts("second");
	return 0;
}

#include<iostream>
#include<string>
#include<map>
#define rep(i,j) for(int i=0;i<j;i++)
using namespace std;
map<string,string> ans;
string s,g=".........";
bool judge(string t,char p)
{
    for(int i=0;i<3;i++)
    if(g[i*3]==p && g[i*3+1]==p && g[i*3+2]==p)return true;
    for(int i=0;i<3;i++)
    if(g[i]==p&&g[i+3]==p&&g[i+6]==p)return true;
    if(g[0]==p&&g[4]  ==p&&g[8]==p)return true;
    if(g[2]==p&&g[4]  ==p&&g[6]==p)return true;
    return false;
}
void search(int t)
{
    if(judge(g,'X')){ans[g]="the first player won";return;}
    if(judge(g,'0')){ans[g]="the second player won";return;}
    if(t==9){ans[g]="draw";return;}
    if(t%2==0)ans[g]="first";
    if(t%2==1)ans[g]="second";
    for(int i=0;i<9;i++)
        if(g[i]=='.'){
        if(t%2==1)g[i]='0'; else g[i]='X';
        search(t+1);
        g[i]='.';   }
}

int main()
{
    search(0);
    for(int i=0;i<3;i++)
    {
        string ss;
        cin>>ss;
        s+=ss;
    }
    if(ans.find(s)==ans.end())cout<<"illegal";else
    cout<<ans[s]<<endl;
    return 0;
}
//用9格数组，想得我累死了

#include<bits/stdc++.h>
using namespace std;

 char c[3][3];

bool vjudge(char x) {
	if (c[0][0]==x && c[1][1]==x && c[2][2]==x) return true;
	if (c[0][2]==x && c[1][1]==x && c[2][0]==x) return true;
	for (int i=0; i<3; i++) if ((c[0][i]==x && c[1][i]==x && c[2][i]==x)||(c[i][0]==x && c[i][1]==x && c[i][2]==x)) return true;
	return false;
}

int main()
{

    int a=0,b=0;
    bool g,f;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            cin>>c[i][j];
            if(c[i][j]=='0')b++;
            else if(c[i][j]=='X')a++;
        }
    }
    if(b>a||abs(b-a)>1)
    {
        cout<<"illegal"<<endl;
        return 0;
    }

     g=vjudge('X');
    f=vjudge('0');
    // cout<<g<<" "<<f<<endl;
    if(g&&f)
    {
        cout<<"illegal";
        return 0;
    }
    else if(g)
    {
         if(b>=a)
        {
            cout<<"illegal"<<endl;
            return 0;
        }
        cout<<"the first player won";
        return 0;
    }
    else if(f)
    {
        if(b<a)
        {
            cout<<"illegal"<<endl;
            return 0;
        }
        cout<<"the second player won";
        return 0;
    }

    if(a+b==9)
    {
       cout<<"draw"<<endl;
    }
    else if(a>b)
    {
        cout<<"second"<<endl;
    }
    else
    {
        cout<<"first"<<endl;
    }

}

/***********************
*  Author: xuziyuan    *
***********************/

#include <bits/stdc++.h>

#define rt0 return 0
#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define replet(c) for(char c='a';c<='z';c++)
#define LL long long
#define pii pair <int,int>
#define pb push_back
#define fi first
#define se second
#define mpr make_pair
#define sqr(a) ((a)*(a))

using namespace std;

const LL MOD=1e9+7;

string s[3];

int win(char c)
{
	int mask=(1<<9)-1,mask2=0;
	vector <int> v;
	rep(i,3) v.pb((1<<i)+(1<<(i+3))+(1<<(i+6)));
	for(int i=0;i<8;i+=3) v.pb((1<<i)+(1<<(i+1))+(1<<(i+2)));
	v.pb((1<<0)+(1<<4)+(1<<8));
	v.pb((1<<2)+(1<<4)+(1<<6));
	rep(i,3) rep(j,3) if(s[i][j]==c) mask2|=1<<(i*3+j);
	rep(i,v.size()) if((mask2&v[i])==v[i]) return true;
	return false;
}

int main()
{
	int x=0,n0=0;
	rep(i,3)
	{
		cin>>s[i];
		rep(j,3)
		{
			if(s[i][j]=='X') x++;
			if(s[i][j]=='0') n0++;
		}
	}
	int wx=win('X'),w0=win('0');
	if(x!=n0&&x!=n0+1) puts("illegal"),exit(0);
	if((wx&&x!=n0+1)||(w0&&x!=n0)) puts("illegal");else
	if(wx) puts("the first player won");else
	if(w0) puts("the second player won");else
	if(x+n0==9) puts("draw");else
	if(x==n0) puts("first");else
	puts("second");
	rt0;
}

#include <bits/stdc++.h>
using namespace std;
char ch[4][4];
int main() {
	int sum_f=0,sum_s=0;
	for(int i=0;i<3;i++) {
		cin>>ch[i];
		for(int j=0;j<3;j++) {
			if(ch[i][j]=='X') sum_f++;
			if(ch[i][j]=='0') sum_s++;
		}
	}
	if(sum_s>sum_f || abs(sum_f-sum_s)>=2) cout<<"illegal"<<endl;
	else {
		bool ff=0,fs=0;
		if(ch[0][0]==ch[1][1] && ch[0][0]==ch[2][2]) {
			if(ch[0][0]=='X') ff=1;
			if(ch[0][0]=='0') fs=1;
		}
		if(ch[2][0]==ch[1][1] && ch[2][0]==ch[0][2]) {
			if(ch[2][0]=='X') ff=1;
			if(ch[2][0]=='0') fs=1;
		}
		for(int i=0;i<3;i++) {
//			cout<<ch[i][0]<<ch[i][1]<<ch[i][2]<<endl;
			if(ch[i][0]==ch[i][1] && ch[i][0]==ch[i][2]) {
				if(ch[i][0]=='X') ff=1;
				if(ch[i][0]=='0') fs=1;
			}
		}
		for(int j=0;j<3;j++) {
			if(ch[0][j]==ch[1][j] && ch[0][j]==ch[2][j]) {
				if(ch[0][j]=='X') ff=1;
				if(ch[0][j]=='0') fs=1;
			}
		}
//		cout<<ff<<" "<<fs<<endl;
		if(ff || fs) {
			if(ff && fs) cout<<"illegal"<<endl;
			else if(ff) {
				if(sum_f==sum_s) cout<<"illegal"<<endl;
				else cout<<"the first player won"<<endl;
			}
			else {
				if(sum_f>sum_s) cout<<"illegal"<<endl;
				else cout<<"the second player won"<<endl;
			}
		}
		else {
			if(sum_f+sum_s==9) cout<<"draw"<<endl;
			else if(sum_f>sum_s) cout<<"second"<<endl;
			else cout<<"first"<<endl;
		}
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
string board[3];
int two(int i){
	return 1<<i;
}
int win(char ch){
	int i,j,mask=(1<<9)-1,cnt=0;
	vector<int> v;
	for(int i=0;i<3;i++)v.push_back(two(i*3)+two(i*3+1)+two(i*3+2));
	for(int i=0;i<3;i++)v.push_back(two(i)+two(i+3)+two(i+6));
	v.push_back(two(0)+two(4)+two(8));
	v.push_back(two(2)+two(4)+two(6));
	int mask2=0;
	for(int i=0;i<3;i++)for(int j=0;j<3;j++)
		if(board[i][j]==ch)mask2|=two(i*3+j);
	for(int i=0;i<v.size();i++){
		if((mask2&v[i])==v[i]){
			cnt++;
			mask&=v[i];
		}
	}
	if(cnt==0)return 0;
	if(mask==0)return 2;
	return 1;
}
int count(char ch){
	int i,j,ans=0;
	for(i=0;i<3;i++)for(j=0;j<3;j++)
		if(board[i][j]==ch)ans++;
	return ans;
}
string func()
{
	int A=count('X'),B=count('0');
	if(A<B||A>B+1)return "illegal";
	int player=(A==B?0:1);
	int wA=win('X'),wB=win('0');
	if(wA==2||wB==2||(player==0&&wA==1)||(player==1&&wB==1))return "illegal";
	if(wA)return "the first player won";
	if(wB)return "the second player won";

	if(A+B==9)return "draw";
	if(player==0)return "first";
	return "second";
}
int main()
{
	int i;
	for(int i=0;i<3;i++){
		cin>>board[i];
	}
	string ans=func();
	cout<<ans;
	return 0;
}

#include<iostream>
using namespace std;
char ch[4][4],x,o;

bool sl(char a)
{
	for(int i=1;i<=3;i++)
	{
		if(ch[i][1]==a&&ch[i][2]==a&&ch[i][3]==a)	return true;
		if(ch[1][i]==a&&ch[2][i]==a&&ch[3][i]==a)	return true;
	}
	if(ch[1][1]==a&&ch[2][2]==a&&ch[3][3]==a)	return true;
	if(ch[1][3]==a&&ch[2][2]==a&&ch[3][1]==a)	return true;
	return false;
}

int main()
{
	for(int i=1;i<=3;i++)
		for(int j=1;j<=3;j++)
		{
			cin>>ch[i][j];
			if(ch[i][j]=='X')	x++;
			if(ch[i][j]=='0')	o++;
		}
	if(x-o!=0&&x-o!=1)	{cout<<"illegal";return 0;}
	if(sl('X'))
	{
		if(sl('0'))	{cout<<"illegal";return 0;}
		if(x-o==0)	{cout<<"illegal";return 0;}
		cout<<"the first player won";return 0;
	}
	if(sl('0'))
	{
		if(sl('X'))	{cout<<"illegal";return 0;}
		if(x-o==1)	{cout<<"illegal";return 0;}
		cout<<"the second player won";return 0;
	}
	if(x+o==9)	{cout<<"draw";return 0;}
	if(x-o==0)	{cout<<"first";return 0;}
	if(x-o==1)	{cout<<"second";return 0;}
}

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int winning_combinations(int field[3][3], int player) {//[row][col]
	int result = 0;
	bool is_combination;
	for (int i = 0; i < 3; i++) {
		is_combination = true;
		for (int j = 0; j < 3; j++) {
			if (field[i][j] != player) {
				is_combination = false;
			}
		}
		if (is_combination) {
			result++;
		}
	}
	for (int i = 0; i < 3; i++) {
		is_combination = true;
		for (int j = 0; j < 3; j++) {
			if (field[j][i] != player) {
				is_combination = false;
			}
		}
		if (is_combination) {
			result++;
		}
	}
	is_combination = true;
	for (int i = 0; i < 3; i++) {
		if (field[i][i] != player) {
			is_combination = false;
		}
	}
	if (is_combination) {
		result++;
	}
	is_combination = true;
	for (int i = 0; i < 3; i++) {
		if (field[i][2 - i] != player) {
			is_combination = false;
		}
	}
	if (is_combination) {
		result++;
	}
	return result;
}
int main() {
	int field[3][3];
	string a;
	int count_x_won = 0, count_y_won = 0;
	int count_x = 0, count_o = 0;
	//x ���� ���� �� 1 ����� � ���� ��� ���� ����
	for (int i = 0; i < 3; i++) {
		cin >> a;
		for (int j = 0; j < 3; j++) {
			if (a[j] == '.') {
				field[i][j] = 0;
			}
			if (a[j] == '0') {
				field[i][j] = 2;
				count_o++;
			}
			if (a[j] == 'X') {
				field[i][j] = 1;
				count_x++;
			}
		}
	}
	if (count_x - count_o > 1 || count_o > count_x ) {
		cout << "illegal" << endl;
	}
	else if (winning_combinations(field, 2) == 1) {
		if (winning_combinations(field, 1) == 0 && count_x == count_o) {
			cout << "the second player won" << endl;
		}
		else {
			cout << "illegal" << endl;
		}
	}
	else if (winning_combinations(field, 1) >= 1) {
		if (winning_combinations(field, 2) == 0 && count_x - count_o == 1) {
			cout << "the first player won" << endl;
		}
		else {
			cout << "illegal" << endl;
		}
	}
	else if (count_x + count_o == 9) {
		cout << "draw" << endl;
	}
	else {
		if (count_x == count_o) {
			cout << "first" << endl;
		}
		if (count_x - count_o == 1) {
			cout << "second" << endl;
		}
	}
}

#include <iostream>
using namespace std;

char b[3][3], t;
int X, O;

bool win(){
    for(int i=0; i<3; i++)
        if(b[i][0]!='.' && b[i][0]==b[i][1] && b[i][1]==b[i][2])
            return true;
    for(int j=0; j<3; j++)
        if(b[0][j]!='.' && b[0][j]==b[1][j] && b[1][j]==b[2][j])
            return true;
    if(b[1][1]=='.') return false;
    return (b[0][0]==b[1][1] && b[1][1]==b[2][2])
        || (b[0][2]==b[1][1] && b[1][1]==b[2][0]);
}

int main(){
    for(int i=0; i<9; i++){
        cin >> t;
        b[i/3][i%3] = t;
        if(t == 'X') X++;
        if(t == '0') O++;
    }

    if(O>X || X-O>1){ cout << "illegal" << endl; return 0; }
    if(win()){
        char l = (X>O)?'X':'0';
        bool valid = false;

        for(int i=0; i<3; i++)
            for(int j=0; j<3; j++)
                if(b[i][j]==l){
                    b[i][j] = '.';
                    valid |= !win();
                    b[i][j] = l;
                }

        if(!valid) cout << "illegal" << endl;
        else if(l=='X') cout << "the first player won" << endl;
        else cout << "the second player won" << endl;
        return 0;
    }

    if(O+X == 9) cout << "draw" << endl;
    else if(O==X) cout << "first" << endl;
    else cout << "second" << endl;

    return 0;
}

#include <string>
#include <iostream>
#include <map>
using namespace std;
map <int,string> Find;
string str,st;
bool win(string s,char p)
{
	for(int i=0;i<3;i++)
	{
		if(s[i]==p and s[i+3]==p and s[i+6]==p)return true;
	}
	for(int i=0;i<9;i+=3)
	{
		if(s[i]==p and s[i+1]==p and s[i+2]==p)return true;
	}
	if(s[0]==p and s[4]==p and s[8]==p)return true;
	if(s[2]==p and s[4]==p and s[6]==p)return true;
	return false;
}
int getHash(string s)
{
	int gethash=0;
	for(int i=0;i<9;i++)
	{
		gethash=gethash<<2;
		if(s[i]=='X')
		{
			gethash=gethash+1;
		}
		else if(s[i]=='0')
		{
			gethash=gethash+2;
		}
	}
	return gethash;
}
int cnt(string s,char c)
{
	int ans=0;
	for(int i=0;i<9;i++)
	{
		if(s[i]==c)ans++;
	}
	return ans;
}
void dfs(string s,int turn)
{
	if(win(s,'X'))
	{
		Find[getHash(s)]="the first player won";
	}
	else if(win(s,'0'))
	{
		Find[getHash(s)]="the second player won";
	}
	else if(cnt(s,'0')+cnt(s,'X')==9)
	{
		Find[getHash(s)]="draw";
	}
	else
	{
		string temp=s;
		if(turn%2)Find[getHash(s)]="second";
		else Find[getHash(s)]="first";
		char c=turn%2?'0':'X';
		for(int i=0;i<9;i++)
		{
			if(temp[i]!='.')continue;
			temp[i]=c;
			dfs(temp,turn+1);
			temp=s;
		}
	}
}
int main()
{
	for(int i=0;i<3;i++)
	{
		getline(cin,st);
		str+=st;
	}
	dfs(".........",0);
	if(Find[getHash(str)]!="")cout<<Find[getHash(str)]<<endl;
	else cout<<"illegal"<<endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<int(n);i++)
using namespace std;
int wins(const string& grid,char p)
{
    rep(i,3) if(grid[i*3]==p&&grid[i*3+1]==p&&grid[i*3+2]==p) return true;
    rep(i,3) if(grid[i]==p&&grid[i+3]==p&&grid[i+6]==p) return true;
    if(grid[0]==p&&grid[4]==p&&grid[8]==p) return true;
    if(grid[2]==p&&grid[4]==p&&grid[6]==p) return true;
    return false;
}
string grid=".........";
map<string,string>ans;
void search(int t)
{
    if(wins(grid,'X')) {ans[grid]="the first player won"; return;}
    else if(wins(grid,'0')) {ans[grid]="the second player won"; return;}
    else if(t==9) {ans[grid]="draw"; return;};
    if(t%2==0) ans[grid]="first";
    else ans[grid]="second";
    rep(i,9) if(grid[i]=='.')
    {
        grid[i]=t%2?'0':'X';
        search(t+1);
        grid[i]='.';
    }
}
int main()
{
    search(0);
    string s,t;
    rep(i,3) {cin>>t; s+=t;}
    if(ans.find(s)==ans.end())
        cout<<"illegal"<<endl;
    else
        cout<<ans[s]<<endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
char p[6][6];//棋盘
int f,s,pf,ps;//f是'X'的个数，s是'0'的个数，pf是判断第一个人有没有赢，ps则是判断第二个人；
void dfs(int a,int b,int c,int d,int e,char t)//a,b是方向,c,d是当前的坐标,e是个数,t是棋子的类型('X'或'0')
{
    if(e==3){//如果3个连成了一条线
        if(t=='X')pf=1;//第一个人赢
        else ps=1;//第二个人赢
        return;//返回
    }
    else if(a+c<4&&a+c>0&&b+d<4&&b+d>0&&p[a+c][b+d]==t)//如果没出界，并且是同样的棋子
        dfs(a,b,a+c,b+d,e+1,t);//访问下一个坐标
}
int main()
{
    for(int x=1;x<4;x++)
        for(int y=1;y<4;y++){
            cin>>p[x][y];//输入
            if(p[x][y]=='X')f++;//第一个人的棋子
            if(p[x][y]=='0')s++;//第二个人的棋子
        }
    if(s>f||f-s>1){//s>f是第二个人多下了，f-s>1是第一个人多下了
        cout<<"illegal";//非法情况
        return 0;//返回
    }
    for(int x=1;x<4;x++)
        for(int y=1;y<4;y++)
            if(p[x][y]!='.')//如果这个地方不是空的
                for(int a=-1;a<2;a++)
                    for(int b=-1;b<2;b++)
                        if(a!=0||b!=0)//并且方向正确
                            dfs(a,b,x,y,1,p[x][y]);//访问这一个坐标，记录类型
    if((pf&&ps)||(pf&&f==s)||(ps&&f>s))cout<<"illegal";//如果两个人都赢了，或者其中一个人赢了之后又下了一枚棋子（很坑！）都是非法的
    else if(pf)cout<<"the first player won";//第一个人赢
    else if(ps)cout<<"the second player won";//第二个人赢
    else if(f+s==9)cout<<"draw";//没有人赢，并且不非法，就是平局
    else if(f>s)cout<<"second";//没下满，第二个人下棋
    else cout<<"first";//第一个人下棋
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

	int P[8][3]={{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
int result(string &g,char c){

		for(int i=0;i<8;i++){
			if(g[P[i][0]]==c && g[P[i][1]]==c && g[P[i][2]]==c)
				return 1;
		}
		return 0;
	}

int main(){
	string s;
	for(int i=0;i<3;i++){
		string t;
		cin>>t;
		s+=t;
	}
	queue<string>q;
	q.push(".........");
	int si=q.size();
	// 0 means x
	int c=0;
	for(int turn =0;!q.empty();si=q.size(),turn^=1,c++){

		while(si--){
			string g=q.front();
			//if(c==3)
			//cout<<g<<"\n";
			q.pop();
			if(s==g){
				if(result(g,'X')){
					cout<<"the first player won";
					return 0;
				}
				if(result(g,'0')){
					cout<<"the second player won";
					return 0;
				}
				if(c==9){
					cout<<"draw";
					return 0;
				}
				if(turn==0){
					cout<<"first";
					return 0;
				}
				if(turn==1){
					cout<<"second";
					return 0;
				}

			}
			if(result(g,'X')||result(g,'0'))
				continue;
			for(int i=0;i<9;i++){
				char curr=(turn==0)?'X':'0';
				if(s[i]==curr&&g[i]=='.'){
					string t=g;
					t[i]=curr;
					q.push(t);
				}

			}

		}

	}

cout<<"illegal";

}

#include <stdio.h>
#define ch0(s) in[i][0]+in[i][1]+in[i][2]==s*3
#define ch1(s) in[0][i]+in[1][i]+in[2][i]==s*3
#define ch2(s) in[1][1]==s && (in[0][0]+in[2][2]==s*2 || in[2][0]+in[0][2]==s*2)
int i,j,a,b,wa,wb;
char in[3][4];
void main() {
    for(i=0; i<3; i++) {
        gets(in[i]);
        for (j=0;j<3;j++) { a+=in[i][j]=='X'; b+=in[i][j]=='0'; }
        wa=ch0('X')||wa; wb=ch0('0')||wb;
    }
    if ((a-b)*(a-b)>1 || b>a || (wa && wb)) printf("illegal\n");
    else {
        for(i=0;i<3;i++) { wa=ch1('X')||wa; wb=ch1('0')||wb; }
        wa = ch2('X')||wa; wb = ch2('0')||wb;
        if (wa && wb || wb && a>b || wa && a==b) printf("illegal\n");
        else
            if (wa) printf("the first player won\n");
            else
                if (wb) printf("the second player won\n");
                else
                    if (a+b==9) printf("draw\n");
                    else
                        if (a==b) printf("first\n");
                        else printf("second\n");
    }
}

#include<iostream>
#include<cstdio>
#define err "illegal"
#define nexta "first"
#define nextb "second"
#define awin "the first player won"
#define bwin "the second player won"
#define dr "draw"
#define r(x) return x
using namespace std;
char c[3][3];
int tota,totb;
bool as,bs,ah,bh,ax1,ax2,bx1,bx2,ga,gb;
int main(){
	for(int i=0;i<3;++i)
		for(int j=0;j<3;++j){
			cin>>c[i][j];
			if(c[i][j]=='X')++tota;
			if(c[i][j]=='0')++totb;
		}
	if(tota<totb||totb+1<tota){
		cout<<err;
		r(0);
	}
	for(int i=0;i<3;++i)
		if(c[0][i]=='X'&&c[1][i]=='X'&&c[2][i]=='X'){
			as=true;
			break;
		}
	for(int i=0;i<3;++i)
		if(c[0][i]=='0'&&c[1][i]=='0'&&c[2][i]=='0'){
			bs=true;
			break;
		}
	for(int i=0;i<3;++i)
		if(c[i][0]=='X'&&c[i][1]=='X'&&c[i][2]=='X'){
			ah=true;
			break;
		}
	for(int i=0;i<3;++i)
		if(c[i][0]=='0'&&c[i][1]=='0'&&c[i][2]=='0'){
			bs=true;
			break;
		}
	if(c[0][0]=='X'&&c[1][1]=='X'&&c[2][2]=='X')ax1=true;
	if(c[0][0]=='0'&&c[1][1]=='0'&&c[2][2]=='0')bx1=true;
	if(c[0][2]=='X'&&c[1][1]=='X'&&c[2][0]=='X')ax2=true;
	if(c[0][2]=='0'&&c[1][1]=='0'&&c[2][0]=='0')bx2=true;
	ga=as||ah||ax1||ax2;
	gb=bs||bh||bx1||bx2;
	if(ga&&gb||ga&&tota==totb||gb&&tota>totb){
		cout<<err;
		r(0);
	}
	if(ga||gb){
		if(ga) cout<<awin;
		else cout<<bwin;
		r(0);
	}
	if(tota+totb==9){
		cout<<dr;
		r(0);
	}
	if(tota==totb)cout<<nexta;
	else cout<<nextb;
	r(0);
}

#include <bits/stdc++.h>
#define For(i,a,b) for(int i=a;i<=b;++i)
#define Dec(i,b,a) for(int i=b;i>=a;--i)
#define file() freopen("v.txt","r",stdin)
#define inf 0x3f3f3f3f
using namespace std;

typedef long long ll;
inline ll qr(){
	ll x=0,f=1; char ch;
	while(!isdigit(ch=getchar())) if(ch=='-') f=-1;
	for(;isdigit(ch);x=x*10+ch-48,ch=getchar());
	return x*f;
}
char a[5][5];
int check(int x,int d)
{
	if(d!=4&&a[x][1]=='.') return 0;
	if(d==4&&a[1][x]=='.') return 0;
	if(d==1)
	{
		return a[x][1]==a[x][2]&&a[x][1]==a[x][3];
	}
	if(d==2)
	{
		return a[x][1]==a[x+1][2]&&a[x][1]==a[x+2][3];
	}
	if(d==3)
	{
		return a[x][1]==a[x-1][2]&&a[x][1]==a[x-2][3];
	}
	if(d==4)
	{
		return a[1][x]==a[2][x]&&a[1][x]==a[3][x];
	}
}
int cx,co;
int main()
{
	// file();
	For(i,1,3) scanf("%s",a[i]+1);
	For(i,1,3) For(j,1,3)
	{
		if(a[i][j]=='X') cx++;
		if(a[i][j]=='0') co++;
	}
	if(cx<co||cx>co+1) puts("illegal");
	else
	{
		int s=0;
		For(i,1,3) s+=check(i,1)+check(i,4);
		s+=check(1,2)+check(3,3);
		// printf("%d\n", s);
		if(s>2) return puts("illegal"),0;
		if(s)
		{
			For(i,1,3) if(check(i,1))
			{
				if(a[i][1]=='X'&&cx==co) return puts("illegal"),0;
				if(a[i][1]=='0'&&co<cx) return puts("illegal"),0;
				return printf("the %s player won\n", a[i][1]=='X'?"first":"second"),0;
			}
			For(i,1,3) if(check(i,4))
			{
				if(a[1][i]=='X'&&cx==co) return puts("illegal"),0;
				if(a[1][i]=='0'&&co<cx) return puts("illegal"),0;
				return printf("the %s player won\n", a[1][i]=='X'?"first":"second"),0;
			}
			if(check(1,2))
			{
				if(a[1][1]=='X'&&cx==co) return puts("illegal"),0;
				if(a[1][1]=='0'&&co<cx) return puts("illegal"),0;
				return printf("the %s player won\n", a[1][1]=='X'?"first":"second"),0;
			}
			if(check(3,3))
			{
				if(a[3][1]=='X'&&cx==co) return puts("illegal"),0;
				if(a[3][1]=='0'&&co<cx) return puts("illegal"),0;
				return printf("the %s player won\n", a[3][1]=='X'?"first":"second"),0;
			}
		}
		else
		{
			if(cx==5&&co==4) return puts("draw"),0;
			puts(cx==co?"first":"second");
		}
	}
	return 0;
}

#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;

const int MAXN = 100005;

int chessBoard[4][4];

int Test(int x1, int x2, int x3)
{
   if(x2 == x1 && x2 == x3)
       return x2;
   return 0;
}

int main()
{
#ifdef _LOCAL
    freopen("F://input.txt", "r", stdin);
#endif
    int countX = 0, countO = 0;
    for(int i = 1; i <= 3; ++i)
    {
        for(int j = 1; j <= 3; ++j)
        {
            char ch;
            scanf("%c", &ch);
            if(ch == 'X')
            {
                countX++;
                chessBoard[i][j] = 1;
            }
            else if(ch == '0')
            {
                countO++;
                chessBoard[i][j] = 2;
            }
        }
        getchar();
    }
    if(countO > countX || countX - countO > 1)
    {
        printf("illegal\n");
        return 0;
    }
    vector<int> ans;
    ans.push_back(Test(chessBoard[1][1], chessBoard[1][2], chessBoard[1][3]));
    ans.push_back(Test(chessBoard[2][1], chessBoard[2][2], chessBoard[2][3]));
    ans.push_back(Test(chessBoard[3][1], chessBoard[3][2], chessBoard[3][3]));
    ans.push_back(Test(chessBoard[1][1], chessBoard[2][1], chessBoard[3][1]));
    ans.push_back(Test(chessBoard[1][2], chessBoard[2][2], chessBoard[3][2]));
    ans.push_back(Test(chessBoard[1][3], chessBoard[2][3], chessBoard[3][3]));
    ans.push_back(Test(chessBoard[1][1], chessBoard[2][2], chessBoard[3][3]));
    ans.push_back(Test(chessBoard[1][3], chessBoard[2][2], chessBoard[3][1]));
    sort(ans.begin(), ans.end());
    int last = ans.size() - 1;
    if(ans[last - 1] == 1 && ans[last] == 2 || ans[last] == 2 && countX > countO || ans[last] == 1 && countX == countO)
    {
        printf("illegal\n");
        return 0;
    }
    if(ans[last] == 1)
        printf("the first player won\n");
    else if(ans[last] == 2)
        printf("the second player won\n");
    else if(countX + countO == 9)
        printf("draw\n");
    else if(countX == countO)
        printf("first\n");
    else
        printf("second\n");
    return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define fl(n) for(ll i=0;i<n;i++)
#define rfl(n) for(ll i=n-1;i>=0;i--)
#define all(v) v.begin(),v.end()
#define pb push_back
#define L (ll)(1e9)
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define f first
#define s second
#define mp make_pair
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
#define Case(i) cout<<"Case #"<<i+1<<": ";
using namespace std;
void solve()
{
	vector<string> v(3);
	ll zc=0,xc=0;
	for(ll i=0;i<3;i++)
	{
		cin>>v[i];
		for(ll j=0;j<3;j++)
			v[i][j]=='0'?zc++:v[i][j]=='X'?xc+=1:xc+=0;
	}
	ll fc=0,sc=0;
	for(ll i=0;i<3;i++)
		v[i]=="000"?sc++:v[i]=="XXX"?fc+=1:fc+=0;
	string str="";
	for(ll i=0;i<3;i++)
	{
		str+=v[0][i];str+=v[1][i];str+=v[2][i];
		str=="000"?sc++:str=="XXX"?fc+=1:fc+=0;
		str="";
	}
	str+=v[0][0];str+=v[1][1];str+=v[2][2];
	str=="000"?sc++:str=="XXX"?fc+=1:fc+=0;
	str="";
	str+=v[0][2];str+=v[1][1];str+=v[2][0];
	str=="000"?sc++:str=="XXX"?fc+=1:fc+=0;
	string ans="illegal";
	if(zc==xc)
	{
		if(fc==0&&sc==0)
			ans="first";
		else if(fc==0)
			ans="the second player won";
	}
	else if(xc-1==zc)
	{
		if(sc==0&&(fc==1||(fc==2&&xc==5)))
			ans="the first player won";
		else if(xc==5)
			ans="draw";
		else if(fc==0&&sc==0)
			ans="second";
	}
	cout<<ans<<endl;
}
int main(void)
{
	IOS;
   	solve();
	return 0;
}

#include<cstdio>
using namespace std;
int o,x,ow,xw,k;
char c[9];
#define t(x) if(c[3*i]==x&&c[3*i+1]==x&&c[3*i+2]==x || c[i]==x&&c[i+3]==x&&c[i+6]==x || c[0]==x&&c[4]==x&&c[8]==x || c[2]==x&&c[4]==x&&c[6]==x)
inline void f(int x,int y,int z) {
    if((x==y&&y==z&&x!='.'))x=='0'?++ow:++xw;
}
int main()
{
    for(int i=0;i<3;++i) {
        for(int j=0;j<3;++j) {
            c[k]=getchar();
            if(c[k]!='.')c[k]=='0'?++o:++x;
            ++k;
        }
        getchar();
    }
    for(int i=0;i<3;i++){t('0')ow=1;t('X')xw=1;}
#if 1
    if((xw&&x!=o+1)||(ow&&x!=o)||(x!=o+1&&x!=o))puts("illegal");
    else if(xw)puts("the first player won");
    else if(ow)puts("the second player won");
    else if(x==5&&o==4)puts("draw");
    else if(x==o)puts("first");
    else puts("second");
#endif
    return 0;
}

#include <iostream>
# include<cstdio>
#include <algorithm>
using namespace std;

const int Maxn = 3;

char B[Maxn][Maxn];

int Calc(char c)
{
    int res = 0;
    for (int i = 0; i < Maxn; i++)
       for (int j = 0; j < Maxn; j++)
          res += B[i][j] == c;
    return res;
}

bool Win(char c)
{
     for (int i = 0; i < Maxn; i++) {
         int j;
         for (j = 0; j < Maxn; j++) if (B[i][j] != c) break;
         if (j == Maxn) return true;
     }
     for (int j = 0; j < Maxn; j++) {
         int i;
         for (i = 0; i < Maxn; i++) if (B[i][j] != c) break;
         if (i == Maxn) return true;
     }
     int i;
     for (i = 0; i < Maxn; i++) if (B[i][i] != c) break;
     if (i == Maxn) return true;
     for (i = 0; i < Maxn; i++) if (B[i][Maxn-1-i] != c) break;
     return i == Maxn;
}

int main()
{
    for (int i = 0; i < Maxn; i++)
       for (int j = 0; j < Maxn; j++) cin >> B[i][j];
    int X = Calc('X'), O = Calc('0');
    bool winX = Win('X'), winO = Win('0');
    if (X < O || X > O + 1 || winX && X == O || winO && X == O+1) cout << "illegal\n";
    else if (winX && winO) cout << "illegal\n";
    else if (winX) cout << "the first player won\n";
    else if (winO) cout << "the second player won\n";
    else if (X + O == Maxn*Maxn) cout << "draw\n";
    else if (X == O) cout << "first\n";
    else if (X == O + 1) cout << "second\n";
    else cout << "illegal\n";
    return 0;
}

#include<stdio.h>
char map[3][3];
int count(char a)//统计字符. 0 X的个数
{
    int s=0;
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
        if(a==map[i][j])
            s++;
return s;
}
int jude(char a)//判断是否初现X/0三子连珠
{
    int i,j;
    for(i=0;i<3;i++)//横竖
    {
        for(j=0;j<3;j++)
        if(map[i][j]!=a)
            break;
        if(j==3)
            return 1;
        for(j=0;j<3;j++)
        if(map[j][i]!=a)
            break;
        if(j==3)
            return 1;
    }
    for(i=0;i<3;i++)//斜
    {
        if(map[i][i]!=a)
            break;
    }
    if(i==3)
        return 1;
    for(i=0;i<3;i++)//斜2
    {
        if(map[i][2-i]!=a)
            break;
    }
    if(i==3)
        return 1;
return 0;
}
int main()
{

    for(int i=0;i<3;i++)
        scanf("%s",map[i]);

    if(jude('X')&&jude('0'))//X&0胜
        printf("illegal\n");
    else if(jude('X'))//X三子连珠
    {
        if(count('X')==count('0')+1)
            printf("the first player won\n");
        else
            printf("illegal\n");
    }
    else if(jude('0'))
    {
        if(count('0')==count('X'))
            printf("the second player won\n");
        else
            printf("illegal\n");
    }
    else
    {
        if(count('.')==0)//棋盘下满
        {
            if(count('0')+1==count('X'))
                printf("draw\n");
            else
                printf("illegal\n");
        }
        else
        {
            if(count('X')==count('0'))
                printf("first\n");
            else if(count('X')==count('0')+1)
                printf("second\n");
            else
                printf("illegal\n");
        }
    }
return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
	vector<string> cnt;
	for(int i=0;i<3;i++)
	{
		string a;
		cin>>a;
		cnt.push_back(a);
	}
	int X=0,z=0,xw=0,w=0;
	for(int i=0;i<3;i++)
	{
		if(cnt[i] == "XXX")
		xw++;
		else if(cnt[i] == "000")
		w++;
	}
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
		{
			if(cnt[i][j] == 'X')
			X++;
			else if(cnt[i][j] == '0')
			z++;
		}
	}
	bool tm = false;
	bool y = true;
	for(int i=0;i<3;i++)
	for(int j=0;j<3;j++)
	{
		if(cnt[i][j] == '.')
		{
			y = false;
			break;
		}
	}
	string x,l;
	x+=cnt[0][0];x+=cnt[1][1];x+=cnt[2][2];
	l+=cnt[0][2];l+=cnt[1][1];l+=cnt[2][0];
	if(x=="XXX")
	xw++;
	else if(l=="XXX")
	xw++;
	 if(l=="000")
	 w++;
	 else if(x=="000")
	w++;
	if( y )
	tm = true;
	for(int j = 0;j<3;j++)
	{
		if(cnt[0][j] == 'X'&&cnt[1][j] == 'X'&&cnt[2][j] == 'X'&&!xw)
		xw++;
		else if(cnt[0][j] == '0'&&cnt[1][j] == '0'&&cnt[2][j] == '0'&&!w)
		w++;
	}
	//cout<<xw<<" "<<w<<endl;
	if( xw == 1 )
	{
		if( X == z+1 && w == 0)
		{
			cout<<"the first player won"<<endl;
			return 0;
		}
		cout<<"illegal"<<endl;
		return 0;
	}
	else if( w == 1 )
	{
		if( z == X && xw == 0)
		{
			cout<<"the second player won"<<endl;
			return 0;
		}
		cout<<"illegal"<<endl;
		return 0;
	}
	else if( X == z && !tm )
	{
		cout<<"first"<<endl;
		return 0;
	}
	else if( X == z+1 && !tm )
	{
		cout<<"second"<<endl;
		return 0;
	}
	else if(tm&&X == z+1)
	{
		cout<<"draw"<<endl;
		return 0;
	}
	cout<<"illegal"<<endl;
	return 0;
 }

#include <bits/stdc++.h>
#define two(x) (1<<(x))
using namespace std;
string board[3];
int win(char ch)
{
	int i,j,mask=(1<<9)-1,cnt=0;
	vector <int> v;
	for(int i=0;i<3;i++)
	v.push_back(two(i*3)+two(i*3+1)+two(i*3+2));
	for(int i=0;i<3;i++)
	v.push_back(two(i)+two(i+3)+two(i+6));
	v.push_back(two(0)+two(4)+two(8));
	v.push_back(two(2)+two(4)+two(6));
	int mask2=0;
	for(int i=0;i<3;i++)
	for(int j=0;j<3;j++)
	if(board[i][j]==ch)
	mask2|=two(i*3+j);
	for(int i=0;i<v.size();i++)
	if((mask2&v[i])==v[i])
	{
		cnt++;
		mask&=v[i];
	}
	if(cnt==0)
	return 0;
	if(mask==0)
	return 2;
	return 1;
}
int count(char ch)
{
	int i,j,ans=0;
	for(int i=0;i<3;i++)
	for(int j=0;j<3;j++)
	if(board[i][j]==ch)
	ans++;
	return ans;
}
string func(void)
{
	int A=count('X'),B=count('0');
	if(A<B||A>B+1)
	return "illegal";
	int player=((A==B)?0:1);
	int wa=win('X'),wb=win('0');
	if(wa==2||wb==2||(player==0&&wa==1)||(player==1&&wb==1))\
	return "illegal";
	if(wa==1)
	return "the first player won";
	if(wb==1)
	return "the second player won";
	if(A+B==9)
	return "draw";
	if(player)
	return "second";
	return "first";
}
int main()
{
	for(int i=0;i<3;i++)
	cin >>board[i];
	string ans=func();
	cout <<ans<<endl;
	return 0;
}

#include<iostream>
#include<vector>
#include<queue>
#include<set>
#include<stdio.h>
#include<algorithm>
#include<string>
#include<map>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;

vector<string> a;

bool nulls_win(vector<string> a) {
	int i;
	bool result=false;
	for (i=0;i<3;i++)
		if (a[i][0]=='0' && a[i][1]=='0' && a[i][2]=='0') return true;
	for (i=0;i<3;i++)
		if (a[0][i]=='0' && a[1][i]=='0' && a[2][i]=='0') return true;
	if (a[0][0]=='0' && a[1][1]=='0' && a[2][2]=='0') return true;
	if (a[0][2]=='0' && a[1][1]=='0' && a[2][0]=='0') return true;
	return false;
}
bool xx_win(vector<string> a) {
	int i;
	bool result=false;
	for (i=0;i<3;i++)
		if (a[i][0]=='X' && a[i][1]=='X' && a[i][2]=='X') return true;
	for (i=0;i<3;i++)
		if (a[0][i]=='X' && a[1][i]=='X' && a[2][i]=='X') return true;
	if (a[0][0]=='X' && a[1][1]=='X' && a[2][2]=='X') return true;
	if (a[0][2]=='X' && a[1][1]=='X' && a[2][0]=='X') return true;
	return false;
}

int i,j;

int main() {
	//freopen("input.txt", "rt", stdin);
	//freopen("output.txt", "wt", stdout);
	for (i=0;i<3;i++) {
		string str;
		cin >> str;
		a.push_back(str);
	}
	int nulls=0;
	int xx=0;
	for (i=0;i<3;i++)
		for (j=0;j<3;j++) {
			if (a[i][j]=='X') xx++;
			if (a[i][j]=='0') nulls++;
		}
	if (xx-nulls>1 || nulls-xx>0) {cout << "illegal"; return 0;}
	if (nulls_win(a) && xx_win(a)) {cout << "illegal"; return 0;}
	if (nulls_win(a)) {
		if (xx==nulls) {cout << "the second player won"; return 0;}
		else {cout << "illegal"; return 0;}
	}
	if (xx_win(a)) {
		if (xx>nulls) {cout << "the first player won"; return 0;}
		else {cout << "illegal"; return 0;}
	}
	if (xx+nulls==9) {cout << "draw"; return 0;}
	if (xx>nulls) {cout << "second"; return 0;}
	if (xx==nulls) {cout << "first"; return 0;}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;

 char c[3][3];

bool vjudge(char x) {
	if (c[0][0]==x && c[1][1]==x && c[2][2]==x) return true;
	if (c[0][2]==x && c[1][1]==x && c[2][0]==x) return true;
	for (int i=0; i<3; i++) if ((c[0][i]==x && c[1][i]==x && c[2][i]==x)||(c[i][0]==x && c[i][1]==x && c[i][2]==x)) return true;
	return false;
}

int main()
{

    int a=0,b=0;
    bool g,f;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            cin>>c[i][j];
            if(c[i][j]=='0')b++;
            else if(c[i][j]=='X')a++;
        }
    }
    if(b>a||abs(b-a)>1)
    {
        cout<<"illegal"<<endl;
        return 0;
    }

     g=vjudge('X');
    f=vjudge('0');
    // cout<<g<<" "<<f<<endl;
    if(g&&f)
    {
        cout<<"illegal";
        return 0;
    }
    else if(g)
    {
         if(b>=a)
        {
            cout<<"illegal"<<endl;
            return 0;
        }
        cout<<"the first player won";
        return 0;
    }
    else if(f)
    {
        if(b<a)
        {
            cout<<"illegal"<<endl;
            return 0;
        }
        cout<<"the second player won";
        return 0;
    }

    if(a+b==9)
    {
       cout<<"draw"<<endl;
    }
    else if(a>b)
    {
        cout<<"second"<<endl;
    }
    else
    {
        cout<<"first"<<endl;
    }

}

#include<iostream>
#include<math.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
 const int x=3;
int main()
{
	char str[x][x];
	int f=0,s=0,p=0;
	bool first=false, second=false;
	for(int i=0;i<x;i++)
	{
		for(int j=0;j<x;j++)
		{
			cin>>str[i][j];
			if(str[i][j]=='X')
			{
				f++;
			}
			else if(str[i][j]=='.')
			{
				p++;
			}
			else s++;
		}
	}
	for(int i=0;i<x && (!first || !second);i++)
	{
		if(str[i][0]==str[i][1] && str[i][1]==str[i][2] && str[i][0]=='X')
		{
				first=true;
		}
		else if(str[i][0]==str[i][1] && str[i][1]==str[i][2] && str[i][0]=='0')
		{
				second=true;
		}
		else if(str[0][i]==str[1][i] && str[1][i]==str[2][i] && str[0][i]=='X')
		{
			first=true;
		}
		else if(str[0][i]==str[1][i] && str[1][i]==str[2][i] && str[0][i]=='0')
		{
			second=true;
		}
	}
	if(str[0][0]==str[1][1] && str[1][1]==str[2][2] && str[0][0]=='0')
		{
			second=true;
		}
	if(str[0][0]==str[1][1] && str[1][1]==str[2][2] && str[0][0]=='X')
		{
			first=true;
		}
	if(str[0][2]==str[1][1] && str[1][1]==str[2][0] && str[0][2]=='0')
		{
			second=true;
		}
	if(str[0][2]==str[1][1] && str[1][1]==str[2][0] && str[0][2]=='X')
		{
			first=true;
		}
	if(first&&second || (f-s>1) || (s-f>0) || (second && (f>s)) || (first && f<=s))
	{
		cout<<"illegal"<<endl;
	}
	else if(first && !second)
		cout<<"the first player won"<<endl;
	else if(!first && second)
		cout<<"the second player won"<<endl;
	else if(p==0)
		cout<<"draw"<<endl;
	else if(f==s)
		cout<<"first"<<endl;
	else cout<<"second"<<endl;

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main(){
    int xx=0, oo=0, dot=0;
    char x[3][3];
    bool l=1, xxx=0, ooo=0;
    for (int i=0 ; i<3 ; i++)
        for (int j=0 ; j<3 ; j++){
            cin >> x[i][j];
            xx+=(x[i][j]=='X');
            oo+=(x[i][j]=='0');
            dot+=(x[i][j]=='.');
        }
    if (xx-oo>1 || oo>xx){
        cout << "illegal";
        return 0;
    }
    for (int i=0 ; i<3 ; i++){
        l=1;
        for (int j=1 ; j<3 ; j++)
            if (x[i][j]!=x[i][j-1] || x[i][j]=='.'){
                l=0;
                break;
            }
        if (l){
            if (x[i][0]=='0')
                ooo=1;
            else
                xxx=1;
        }
    }
    for (int i=0 ; i<3 ; i++){
        l=1;
        for (int j=1 ; j<3 ; j++)
            if (x[j][i]!=x[j-1][i] || x[j][i]=='.'){
                l=0;
                break;
            }
        if (l){
            if (x[0][i]=='0')
                ooo=1;
            else
                xxx=1;
        }
    }
    l=1;
    for (int i=1 ; i<3 ; i++)
        if (x[i][i]!=x[i-1][i-1] || x[i][i]=='.'){
            l=0;
            break;
        }
    if (l){
        if (x[0][0]=='0')
            ooo=1;
        else
            xxx=1;
    }
    l=1;
    for (int i=1 ; i<3 ; i++)
        if (x[i][2-i]!=x[i-1][3-i] || x[i][2-i]=='.'){
            l=0;
            break;
        }
    if (l){
        if (x[2][0]=='0')
            ooo=1;
        else
            xxx=1;
    }
    if (ooo && xxx)cout << "illegal";
    else if (xxx && xx==oo)cout << "illegal";
    else if (ooo && xx>oo)cout << "illegal";
    else if (xxx)cout << "the first player won";
    else if (ooo)cout << "the second player won";
    else if (dot==0)cout <<"draw";
    else cout << (xx==oo ? "first" : "second");
    return 0;
}

#include <stdio.h>
#include <iostream>
#include <stack>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>

using namespace std;

bool check(string s[],char c)
{
    int i,j;
    bool fl1,fl2;
    for (i=0; i<3; i++)
    {
        fl1=0;
        fl2=0;
        for (j=0; j<3; j++)
        {
            if (s[i][j]!=c) fl1=1;
            if (s[j][i]!=c) fl2=1;
        }
        if (!fl1 || !fl2) return 1;
    }
    fl1=0;
    for (i=0; i<3; i++) if (s[i][i]!=c) fl1=1;
    if (!fl1) return 1;
    fl1=0;
    for (i=0,j=2; i<3; j--,i++) if (s[i][j]!=c) fl1=1;
    if (!fl1) return 1;
    return 0;
}

bool draw(string s[])
{
    int c=0,i,j;
    for (i=0; i<3; i++) for (j=0; j<3; j++) if (s[i][j]=='.') return 0;
    return 1;
}

int main()
{
    string s[5];
    int i,j;
	while (cin>>s[0])
	{
	    for (i=1; i<3; i++) cin>>s[i];
	    int x=0,c=0;
	    for (i=0; i<3; i++) for (j=0; j<3; j++)
	    {
	        x+=s[i][j]=='X';
	        c+=s[i][j]=='0';
	    }
	    if (abs(x-c)>1 || c>x || (check(s,'X') && check(s,'0')) || (check(s,'0') && x>c) || (check(s,'X') && c>=x))
	    {
	        printf("illegal\n");
	    }
	    else if (check(s,'X'))
	    {
	        printf("the first player won\n");
	    }
	    else if (check(s,'0'))
	    {
	        printf("the second player won\n");
	    }
	    else if (draw(s))
	    {
	        printf("draw\n");
	    }
	    else
	    {
            if (x<=c) printf("first\n");
            else printf("second\n");
	    }
	}

    return 0;
}

#include <stdio.h>
#include<string.h>
using namespace std;

FILE *f=fopen("date.in", "r");
FILE *g=fopen("date.out", "w");

char s[3][4], *p;

int castig(char p){
    char k[4], cate=0;
    k[0]=p; k[1]=p; k[2]=p; k[3]=NULL;
    for(int i=0; i<3; ++i){
        if(strcmp(s[i], k)==0)
            ++cate;
        if(s[0][i]==s[1][i]&&s[1][i]==s[2][i]&&s[2][i]==p)
            ++cate;
    }
    if(s[0][0]==s[1][1]&&s[1][1]==s[2][2]&&s[2][2]==p)
        ++cate;
    if(s[0][2]==s[1][1]&&s[1][1]==s[2][0]&&s[2][0]==p)
        ++cate;
    return cate;
}

int main()
{
    //fscanf(f, "%s%s%s", s[0], s[1], s[2]);
    scanf("%s%s%s", s[0], s[1], s[2]);
    int catex=0, cate0=0, catep=0;
    for(int i=0; i<3; ++i){
        p=strchr(s[i], 'X');
        while(p!=NULL){
            p=strchr(p+1, 'X');
            ++catex;
        }
        p=strchr(s[i], '0');
        while(p!=NULL){
            p=strchr(p+1, '0');
            ++cate0;
        }
        p=strchr(s[i], '.');
        while(p!=NULL){
            p=strchr(p+1, '.');
            ++catep;
        }
    }
    int okx=castig('X');
    int oko=castig('0');
    if(oko!=0&&okx!=0){
        printf("illegal");
        return 0;
    }
    if(okx!=0&&cate0!=catex-1){
        printf("illegal");
        return 0;
    }
    else
        if(okx==0&&oko!=0&&catex!=cate0){
            printf("illegal");
            return 0;
        }
    if(oko!=0){
        printf("the second player won");
        return 0;
    }
    if(okx!=0){
        printf("the first player won");
        return 0;
    }
    if(catep!=0){
        if(catex==cate0)
            //fprintf(g, "first");
            printf("first");
        else
            if(catex==cate0+1)
                //fprintf(g, "second");
                printf("second");
            else
                //fprintf(g, "illegal");
                printf("illegal");
        return 0;
    }
    if(catex<cate0||catex-1>cate0||(cate0==catex&&catex==catep)){
        //fprintf(g, "illegal");
        printf("illegal");
        return 0;
    }
    if(okx==1)
        //fprintf(g, "the first player won");
        printf("the first player won");
    else
        if(oko==1)
            //fprintf(g, "the second player won");
            printf("the second player won");
    //fprintf(g, "draw");
        else
            printf("draw");
    //fprintf(g, "x: %d\n0: %d\n.: %d", catex, cate0, catep);*/
return 0;
}

# include<iostream>
# include<algorithm>
using namespace std;
const int Maxn=3;
char B[Maxn][Maxn];
int Calc(char c)
{
	int res=0;
	for(int i=0;i<Maxn;i++)
	  for(int j=0;j<Maxn;j++)
	  	res += B[i][j]==c;
	return res;
}
bool Win(char c)
{
	for(int i=0;i<Maxn;i++){
		int j;
	  for(j=0;j<Maxn;j++)  if (B[i][j] != c) break;
      if (j == Maxn) return true;
	}
	for(int j=0;j<Maxn;j++){
		int i;
	  for(i=0;i<Maxn;i++)if (B[i][j] != c) break;
      if (i == Maxn) return true;
	}
	int i;
	for(i=0;i<Maxn;i++)
	  if(B[i][i]!=c) break;
		if(i==Maxn)  return true;
	for(i=0;i<Maxn;i++)
	  if(B[i][Maxn-1-i]!=c) break;
    return i==Maxn;
}
int main()
{
	for(int i=0;i<Maxn;i++)
	  for(int j=0;j<Maxn;j++)
	  	cin>>B[i][j];
    int X=Calc('X'),O=Calc('0');
	bool winX=Win('X'),winO=Win('0');
    if (X < O || X > O + 1 || winX && X == O || winO && X == O+1) cout << "illegal\n";
    else if (winX && winO) cout << "illegal\n";
    else if (winX) cout << "the first player won\n";
    else if (winO) cout << "the second player won\n";
    else if (X + O == Maxn*Maxn) cout << "draw\n";
    else if (X == O) cout << "first\n";
    else if (X == O + 1) cout << "second\n";
    else cout << "illegal\n";
    return 0;
}

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int countChars(string str, char c){
	int numChars = 0;
	for (int i = 0; i < str.size(); i++){
		if (str[i] == c) numChars++;
	}
	return numChars;
}

int checkWon(string str, char c){
	vector<bool> cases(8);
	//vertical cases:
	cases[0] = str[0] == c && str[1] == c && str[2] == c;
	cases[1] = str[3] == c && str[4] == c && str[5] == c;
	cases[2] = str[6] == c && str[7] == c && str[8] == c;

	//horizontal cases:
	cases[3] = str[0] == c && str[3] == c && str[6] == c;
	cases[4] = str[1] == c && str[4] == c && str[7] == c;
	cases[5] = str[2] == c && str[5] == c && str[8] == c;

	//diagonal cases:
	cases[6] = str[0] == c && str[4] == c && str[8] == c;
	cases[7] = str[2] == c && str[4] == c && str[6] == c;

	//return num of win cases (if greater than 1, then illegal)
	int sum = 0;
	for (int i = 0; i < cases.size(); i++){
		if (cases[i]) sum++;
	}
	return sum;
}

//cf_3c main
void main(){
	string game;
	for (int i = 0; i < 3; i++){
		string input;
		cin >> input;
		game += input;
	}
	//count "x" & "0"
	int numX = countChars(game, 'X');
	int num0 = countChars(game, '0');

	int diff = numX - num0;
	//cout << "diff: " << diff << " numx: " << numX << " num0: " << num0 << endl;
	//first illegal check
	if (diff != 0 && diff != 1){
		cout << "illegal" << endl;
		//system("pause");
		return;
	}

	//calculate wins
	int winsX = checkWon(game, 'X');
	int wins0 = checkWon(game, '0');
	//impossible cases:
	//it can have more than one win case... just realized
	//but can't both win
	//(winsX > 1 || wins0 > 1)
	if (winsX == 1 && wins0 == 1)	{
		cout << "illegal" << endl;
		//system("pause");
		return;
	}
	//someone has possibly won
	//if first won, then difference must be 1
	else if(winsX > 0){
		if (diff != 1){
			cout << "illegal" << endl;
			//system("pause");
			return;
		}
		cout << "the first player won" << endl;
		//system("pause");
		return;
	}
	//if second won, then difference must be 0
	else if (wins0 > 0){
		if (diff != 0){
			cout << "illegal" << endl;
			//system("pause");
			return;
		}
		cout << "the second player won" << endl;
		//system("pause");
		return;
	}
	//draw case
	if (numX == 5){
		cout << "draw" << endl;
		//system("pause");
		return;
	}
	//going next
	if (diff == 1){
		cout << "second" << endl;
		//system("pause");
		return;
	}
	else if (diff == 0){
		cout << "first" << endl;
		//system("pause");
		return;
	}
}

#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<functional>
#include<algorithm>
#include<bitset>
#include<utility>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
using namespace std;
struct pos
{
	int a[3][3];
	pos()
	{
		memset(a,0,sizeof(a));
	};
	int in_binaray()
	{
		int res=0,cur=0;
		for(int i=0;i<3;++i)
			for(int j=0;j<3;++j)
			{
				res^=(a[i][j]<<(2*cur));
				++cur;
			}
		return res;
	}
	int winner()
	{
		for(int i=0;i<3;++i)
		{
			if(!a[i][0])
				continue;
			bool eq=1;
			for(int j=0;j<3;++j)
				if(a[i][j]!=a[i][0])
					eq=0;
			if(eq)
				return a[i][0];
		}
		for(int i=0;i<3;i++)
		{
			if(!a[0][i])
				continue;
			bool eq=1;
			for(int j=0;j<3;j++)
				if(a[j][i]!=a[0][i])
					eq=0;
			if(eq)
				return a[0][i];
		}
		if(a[0][0]==a[1][1]&&a[1][1]==a[2][2])
			return a[0][0];
		if(a[0][2]==a[1][1]&&a[1][1]==a[2][0])
			return a[0][2];
		return 0;
	}
	bool draw()
	{
		int c=0;
		for(int i=0;i<3;++i)
			for(int j=0;j<3;++j)
				if(!a[i][j])
					c++;
		return (c==0);
	}
	int who()
	{
		int b=0;
		for(int i=0;i<3;++i)
			for(int j=0;j<3;++j)
				if(a[i][j]==1)
					b++;
				else if(a[i][j]==2)
					b--;
		return (b==0?1:2);
	}
};
set<int> fi,se,fiw,sew,dr;
void dfs(pos p)
{
	int x=p.in_binaray();
	int w=p.winner();
	if(w)
	{
		if(w==1)
			fiw.insert(x);
		else
			sew.insert(x);
		return;
	}
	if(p.draw())
	{
		dr.insert(x);
		return;
	}
	int who=p.who();
	if(who==1)
		fi.insert(x);
	else
		se.insert(x);
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++)
		{
			if(p.a[i][j])
				continue;
			p.a[i][j]=who;
			dfs(p);
			p.a[i][j]=0;
		}
}
int main()
{
	pos p;
	dfs(p);
	pos q;
	for(int i=0;i<3;i++)
	{
		string s;
		cin>>s;
		for(int j=0;j<3;j++)
		{
			if(s[j]=='X')
				q.a[i][j]=1;
			else if(s[j]=='0')
				q.a[i][j]=2;
			else
				q.a[i][j]=0;
		}
	}
	int x=q.in_binaray();
	if(fi.count(x))
		puts("first");
	else if(se.count(x))
		puts("second");
	else if(fiw.count(x))
		puts("the first player won");
	else if(sew.count(x))
		puts("the second player won");
	else if(dr.count(x))
		puts("draw");
	else
		puts("illegal");
	return 0;
}

#include <iostream>
#include <algorithm>
#include <math.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
char page[6][6];
int O_num;
int X_num;
int point_num;
int flag_X=0;
int flag_O=0;
inline bool win_x(){
    for(register int i=1;i<=3;i++){
        for(register int j=1;j<=3;j++){
            if(page[i][j]=='X'){
                if((page[i-1][j-1]=='X'&&page[i+1][j+1]=='X')||(page[i-1][j]=='X'&&page[i+1][j]=='X')||(page[i][j-1]=='X'&&page[i][j+1]=='X')||(page[i+1][j-1]=='X'&&page[i-1][j+1]=='X')){
                    return 1;
                }
            }
        }
    }
    return 0;
}
inline bool win_o(){
    for(register int i=1;i<=3;i++){
        for(register int j=1;j<=3;j++){
            if(page[i][j]=='0'){
                if((page[i-1][j-1]=='0'&&page[i+1][j+1]=='0')||(page[i-1][j]=='0'&&page[i+1][j]=='0')||(page[i][j-1]=='0'&&page[i][j+1]=='0')||(page[i+1][j-1]=='0'&&page[i-1][j+1]=='0')){
                    return 1;
                }
            }
        }
    }
    return 0;
}
int main(int argc, char const *argv[]) {
    //freopen("tic.in","r",stdin);
    //freopen("tic.out","w",stdout);
    for(register int i=1;i<=3;i++){
        for(register int j=1;j<=3;j++){
            cin>>page[i][j];
            switch(page[i][j]){
                case 'X':X_num++;break;
                case '0':O_num++;break;
                case '.':point_num++;break;
            }
        }
    }
    //printf("%d %d\n",win_x(),win_o());
    if(((X_num==O_num)||(X_num==O_num+1))&&( (win_x()!=win_o()) || (win_x()!=1) )){

        if(win_x()){
            if(X_num==O_num+1){
                printf("the first player won");
                return 0;
            }
            else {
                printf("illegal");
                return 0;
            }
        }
        if(win_o()){
            if(X_num==O_num){
                printf("the second player won");
                return 0;
            }
            else {
                printf("illegal");
                return 0;
            }
        }
        if(X_num+O_num==9){
            printf("draw");
            return 0;
        }

        if(X_num==O_num+1){
            printf("second");
            return 0;
        }
        if(X_num==O_num){
            printf("first");
            return 0;
        }
    }
    else {
        printf("illegal");
        return 0;
    }

    return 0;
}
/*

X0X
.0.
.X.

XX.
X00
0X0

0X0
X0X
X0X

XXX
XXX
XXX

X0X
0X0
X0X

XXX
X00
00.

XXX
...
000

XXX
X00
X00

0X0
X0X
X0X

000
XXX
XXX
*/

#include<bits/stdc++.h>
using namespace std;
char a[9];
bool wo=false,wx=false;
int co,cx;
bool f(int i,char c)
{
	return (a[3*i]==c&&a[3*i+1]==c&&a[3*i+2]==c)||(a[i]==c&&a[i+3]==c&&a[i+6]==c)||(a[0]==c&&a[4]==c&&a[8]==c)||(a[2]==c&&a[4]==c&&a[6]==c);
}
int main()
{
    for(int i=0;i<9;i++)
	{
		cin>>a[i];
		if(a[i]=='0')
		co++;
		if(a[i]=='X')
		cx++;
	}
    for(int i=0;i<3;i++)
	{
		wo=wo||f(i,'0');
		wx=wx||f(i,'X');
	}
    if((wx&&cx!=co+1)||(wo&&cx!=co)||(cx!=co+1&&cx!=co))
	puts("illegal");
    else if(wx)
	puts("the first player won");
    else if(wo)
	puts("the second player won");
    else if(cx+co==9)
	puts("draw");
    else if(cx==co)
	puts("first");
    else
	puts("second");
	return 0;
}

/*
 *                          C. Tic-tac-toe
 *
 * 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.
 *
 *
 * Input
 * -----
 * 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).
 *
 * Output
 * ------
 * Print one of the six verdicts: first, second, illegal, the first player
 * won, the second player won or draw.
 *
 * Examples
 * --------
 *
 * Input
 * -----
 * X0X
 * .0.
 * .X.
 *
 * Output
 * ------
 * second
 */

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <iostream>

template <typename T>
auto input() {
    T result;

    std::cin >> result;

    return result;
}

struct problem {
    enum class player {
        cross = 'X',
        nought = '0',
        empty = '.',
    };

    auto static constexpr board_size = 9;
    using game_board = std::array<player, board_size>;

    explicit problem(game_board const& board);

    game_board const board;

    unsigned const num_of_play_X{};
    unsigned const num_of_play_Y{};

    bool const cross_is_winner{};
    bool const nought_is_winner{};
};

auto player_won(problem::game_board const& board, problem::player player) -> bool;

problem::problem(problem::game_board const& board)
    : board{board}, num_of_play_X(count(begin(board), end(board), player::cross)),
      num_of_play_Y(count(begin(board), end(board), player::nought)),
      cross_is_winner{player_won(board, problem::player::cross)},
      nought_is_winner{player_won(board, problem::player::nought)} {}

struct solution {
    char const* answer{};
};

auto solve(problem const& tic_tac_toe) -> solution;

auto main() -> int {
    problem::game_board board;

    for (auto& cell : board) {
        cell = static_cast<problem::player>(input<char>());
    }

    auto const tic_tac_toe = problem{board};

    auto const solution = solve(tic_tac_toe);

    std::cout << solution.answer;
}

auto plays_are_legal(problem const& tic_tac_toe) -> bool;
auto game_draw(problem const& tic_tac_toe) -> bool;

auto solve(problem const& tic_tac_toe) -> solution {
    if (!plays_are_legal(tic_tac_toe)) {
        return {"illegal"};
    }

    if (game_draw(tic_tac_toe)) {
        return {"draw"};
    }

    if (tic_tac_toe.cross_is_winner) {
        return {"the first player won"};
    }

    if (tic_tac_toe.nought_is_winner) {
        return {"the second player won"};
    }

    if (tic_tac_toe.num_of_play_X <= tic_tac_toe.num_of_play_Y) {
        return {"first"};
    }

    return {"second"};
}

auto game_draw(problem const& tic_tac_toe) -> bool {
    return tic_tac_toe.num_of_play_X + tic_tac_toe.num_of_play_Y == problem::board_size &&
           !tic_tac_toe.cross_is_winner && !tic_tac_toe.nought_is_winner;
}

auto plays_are_legal(problem const& tic_tac_toe) -> bool {
    auto const double_winner = tic_tac_toe.cross_is_winner && tic_tac_toe.nought_is_winner;

    auto const if_nought_is_winner_plays_should_equal =
        tic_tac_toe.nought_is_winner ? tic_tac_toe.num_of_play_X == tic_tac_toe.num_of_play_Y
                                     : true;

    auto const if_cross_is_winner_plays_should_differ_by_one =
        tic_tac_toe.cross_is_winner ? tic_tac_toe.num_of_play_X - tic_tac_toe.num_of_play_Y == 1
                                    : true;

    auto const valid_turn_of_plays = tic_tac_toe.num_of_play_X == tic_tac_toe.num_of_play_Y ||
                                     tic_tac_toe.num_of_play_X - tic_tac_toe.num_of_play_Y == 1;

    return !double_winner && valid_turn_of_plays && if_nought_is_winner_plays_should_equal &&
           if_cross_is_winner_plays_should_differ_by_one;
}

template <typename Container, typename Element, typename... Index>
auto equal(Container const& cont, Element ele, Index const&... index) -> bool {
    return (
        (all_of(begin(index), end(index), [ele, &cont](auto const i) { return cont[i] == ele; })) ||
        ...);
}

auto player_won(problem::game_board const& board, problem::player const player) -> bool {
    using check = std::array<std::size_t, 3>;

    return equal(board, player,
                 check{
                     0,
                     1,
                     2,
                 },
                 check{
                     3,
                     4,
                     5,
                 },
                 check{
                     6,
                     7,
                     8,
                 },
                 check{
                     0,
                     3,
                     6,
                 },
                 check{
                     1,
                     4,
                     7,
                 },
                 check{
                     2,
                     5,
                     8,
                 },
                 check{
                     0,
                     4,
                     8,
                 },
                 check{
                     2,
                     4,
                     6,
                 });
}

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

#define end_of_board 3

char board[4][4];

void init_board (void)
{
    for (int i = 0; i < end_of_board; i++)
        for (int j = 0; j < end_of_board; j++)
            std::cin >> board[i][j];
}

int num_of_elements (char element)
{
    int num = 0;
    for (int i = 0; i < end_of_board; i++)
        for (int j = 0; j < end_of_board; j++)
            if (board[i][j] == element)
                num++;
    return num;
}

int check_coloumn (char element, int coloumn)
{
    int num = 0;
    for (int i = 0; i < end_of_board; i++)
        if (board[i][coloumn] == element)
            num++;
    if (num == 3)
        return 1;
    return 0;
}

int check_all_coloumns (char element)
{
    for (int i = 0; i < end_of_board; i++)
        if (check_coloumn(element, i) == 1)
            return 1;
    return 0;
}

int check_row (char element, int row)
{
    int num = 0;
    for (int j = 0; j < end_of_board; j++)
        if (board[row][j] == element)
            num++;
    if (num == 3)
        return 1;
    return 0;
}

int check_all_rows (char element)
{
    for (int i = 0; i < end_of_board; i++)
        if (check_row(element, i) == 1)
            return 1;
    return 0;
}

int check_all_diagonals (char element)
{
    int num = 0;
    for (int i = 0; i < end_of_board; i++)
        if (board[i][i] == element)
            num++;
    if (num == 3)
        return 1;
    num = 0;
    for (int i = 0; i < end_of_board; i++)
        if (board[i][end_of_board - i - 1] == element)
            num++;
    if (num == 3)
        return 1;
    return 0;
}

int check_win (char element)
{
    if (check_all_coloumns(element) == 1 || check_all_rows(element) == 1 || check_all_diagonals(element) == 1)
        return 1;
    return 0;
}

void print_board ()
{
    for (int i = 0; i < end_of_board; i++)
    {
        for (int j = 0; j < end_of_board; j++)
            std::cout << board[i][j] << " ";
        std::cout << "\n";
    }
}

int main (int argc, char *argv[])
{
    init_board();
 /*
    std::cout<< "rows X " << check_all_rows('X') <<"\n";
    std::cout<< "rows O " << check_all_rows('0') <<"\n";

    std::cout<< "colimns X " << check_all_coloumns('X') <<"\n";
    std::cout<< "colimns O " << check_all_coloumns('0') <<"\n";

    std::cout<< "diagonals O " <<check_all_diagonals('0') <<"\n";
    std::cout<< "diagonals X " <<check_all_diagonals('X') <<"\n";
*/
    if (check_win('X') == 1 && check_win('0') == 0 && num_of_elements('X') - num_of_elements('0') == 1)
    { std::cout << "the first player won"; return 0; }
    if (check_win('X') == 0 && check_win('0') == 1 && num_of_elements('X') - num_of_elements('0') == 0)
    {std::cout << "the second player won"; return 0; }
    if (check_win('X') == 0 && check_win('0') == 0 && (num_of_elements('X') == 5 && num_of_elements('0') == 4))
    { std::cout << "draw"; return 0; }
    if (check_win('X') == 0 && check_win('0') == 0 && num_of_elements('X') + num_of_elements('0') < 9)
    {
        if (num_of_elements('X') - num_of_elements('0') == 1)
        { std::cout << "second"; return 0; }
        else if (num_of_elements('X') - num_of_elements('0') == 0)
        { std::cout << "first"; return 0; }
        else
        { std::cout << "illegal"; return 0; }
    }
    else
    { std::cout << "illegal"; return 0; }
}

#include<bits/stdc++.h>
using namespace std;
bool r(int cr,char t,string b){
	for(int i=0;i<3;i++)if(b[cr*3+i]!=t)return 0;
	return 1;
}
bool c(int cc,char t,string b){
	for(int i=0;i<3;i++)if(b[i*3+cc]!=t)return 0;
	return 1;
}
bool d(char t,string b){
	bool temp=1;
	for(int i=0;i<3;i++)if(b[i*3+i]!=t){
		temp=0;
		break;
	}
	if(temp)return 1;
	for(int i=0;i<3;i++)if(b[i*3+2-i]!=t)return 0;
	return 1;
}
map<string,int>h;
queue<string>q;
int main(){
	string start=".........";
	q.push(start);
	h[start]=0;
	char turnchar[2]{'X','0'};
	while(!q.empty()){
		string cur=q.front();
		q.pop();
		bool isntfull=0;
		int turn=h[cur];
		for(int i=0;i<3;i++){
			for(int j=0;j<3;j++){
				if(cur[i*3+j]=='.'){
					isntfull=1;
					string next=cur;
					next[i*3+j]=turnchar[turn];
					if(!h.count(next)){
						bool iswin=0;
						for(int k=0;k<3;k++){
							if(r(k,turnchar[turn],next))iswin=1;
							if(c(k,turnchar[turn],next))iswin=1;
							if(d(turnchar[turn],next))iswin=1;
						}
						if(iswin)h[next]=turn+2;
						else{
							h[next]=(turn+1)%2;
							q.push(next);
						}
					}
				}
			}
		}
		if(!isntfull)h[cur]=4;
	}
	string str[3];
	cin>>str[0]>>str[1]>>str[2];
	string c;
	for(int i=0;i<3;i++)c+=str[i];
	if(!h.count(c))cout<<"illegal"<<endl;
	else if(h[c]==0)cout<<"first"<<endl;
	else if(h[c]==1)cout<<"second"<<endl;
	else if(h[c]==2)cout<<"the first player won"<<endl;
	else if(h[c]==3)cout<<"the second player won"<<endl;
	else cout<<"draw"<<endl;
	return 0;
}

#include <iostream>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <map>
#include <unordered_map>
#include <stack>
#include <algorithm>
#include <stdio.h>
#include <cstring>					//  For memset
#include <sstream>

using namespace std;

#define lop(i,n) for(int i = 0 ; i < n ;i++)
#define VI	vector<int>
#define VLL	vector<ll>
#define VPI	vector<pair<int , int>>
#define VII	vector<VI>
#define all(v)	v.begin() , v.end()
#define SORT(v)	sort(all(v))
#define ll	long long
#define ull unsigned long long
#define scni(x)	scanf("%d",&x)				//  int
#define scnll(x)	scanf("%lld",&x)			//  ll
#define scnd(x)	scanf("%lf",&x)			//  double
#define prnti(x)	printf("%d\n",x)				//  int
#define prntll(x)	printf("%lld",x)				//  int
#define prntd(x)	printf("%.9f",x)				//  int
#define sz(x)  x.size()
#define SPC  <<" "<<
#define INF int(1e9)
#define pp push_back
#define P pair<int , int>
#define mp make_pair

const double EPS = 1e-9;
const double PI = 3.14159265359;

//  O( n * log(log(n)) )
vector<ll> primes;
void GeneratingPrimes(int top){
	vector<bool> p (top+1 , 1);
//	vector<ll> primes;
	p[0] = p[1] = 0;

	for(int i = 2; i <= sqrt(top) ; i++){
		if(!p[i])continue;
		if(p[i]){
			for(int j = i*i ; j <= top ; j+=i){
				p[j]=0;
			}
		}
	}
	lop(i , top+1)
		if(p[i])primes.push_back(i);
//	return primes;
}

ll gcd(ll a ,ll b){return (b==0)?a : gcd(b , a%b);}
ll lcm(ll a, ll b){return (a * b) / gcd(a , b);}

void EXTgcd(ll a , ll b , int & x0 , int & y0){
	if(b==0){ x0 =1 , y0 = 0; return; }
	EXTgcd( b , a%b , x0 , y0 );
	ll x1 = y0;
	ll y1 = x0 - (a/b) * y0;
	x0 = x1;
	y0 = y1;
}

ull power(ull x, ull y)
{
    ll res = 1;
//    x = x % p;
    while (y > 0)
    {
        if (y & 1) res = (res*x);
        y = y>>1;
		x = (x*x);
    }
    return res;
}

double dist(double a , double b , double x, double y){ return sqrt( pow( a-x ,2) + pow( b-y ,2) ); }

double dist3(double a , double b , double c , double x , double y ,double z ){ return sqrt( pow( a-x ,2) + pow( b-y ,2) + pow( c-z ,2)); }

int xadd[9]= { 1 , -1 , 0 , 1 , -1 , 0 , 1 , -1 , 0 };
int yadd[9]= { 1 , -1 , 0 ,-1 ,  0 , 1 , 0 ,  1, -1 };

#define no  { cout<<"NO\n"; }
#define yes { cout<<"YES\n";}
#define _1 { cout<<"-1"; return 0;}

ll const N = 10007;
ll mod = 1e9 + 7;
///////////////////////////////////////////////////////////////////

#define d {cout << "draw"; return 0;}
#define f  { cout << "first"			; return 0;}
#define s  { cout << "second"			; return 0;}
#define fw { cout << "the first player won"	; return 0;}
#define sw { cout << "the second player won"; return 0;}
#define il { cout << "illegal"			; return 0;}

int rs[3];
int cs[3];
int di[2];
bool won[2];

int main(){

	vector<string> v(3);
	lop(i , 3)cin >> v[i];

	int x =0,  o=0;
	lop(i , 3)lop(j , 3){
		if(v[i][j] == 'X')x++;
		else if(v[i][j] == '0')o++;
	}

	if(o > x || x - o > 1)il;
	lop(i , 3)lop(j , 3){
		if(v[i][j]=='X')rs[j]++;
		else if(v[i][j]=='0')rs[j]--; }
	lop(j , 3)lop(i , 3){ if(v[i][j]=='X')cs[i]++; else if(v[i][j]=='0')cs[i]--; }
	lop(i , 3)if(v[i][i] == 'X')di[0]++; else if(v[i][i]=='0')di[0]--;
	lop(i , 3)if(v[i][2-i] == 'X')di[1]++; else if(v[i][2-i]=='0')di[1]--;

	lop(i , 3)if(rs[i]==3 || cs[i]==3)won[0]=1;
	lop(i , 3)if(rs[i]==-3 || cs[i]==-3)won[1]=1;
	if(di[0]==3 || di[1]==3)won[0]=1;
	if(di[0]==-3 || di[1]==-3)won[1]=1;

	if(won[0] && won[1])il;
	if(won[1] && x > o)il;
	if(won[0] && x == o)il;
	if(won[0])fw;
	if(won[1])sw;
	if(x+o == 9)d;
	if(x == o){f;}
	else {s;}

return 0;}

// 1: Forgot about situation when second player won but first did a move
// 2: Forgot about symmetric situation
// 3: Did mistyping while changing formulas

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <string>

using namespace std;

char mat[3][10];

const int LINE_NUM = 8;

char lines(int ln, int pos)
{
    switch (ln) {
        case 0:
            return pos == 0 ? mat[0][0] : (pos == 1 ? mat[1][1] : mat[2][2]);
        case 1:
            return pos == 0 ? mat[0][2] : (pos == 1 ? mat[1][1] : mat[2][0]);
        case 2:
            return pos == 0 ? mat[0][0] : (pos == 1 ? mat[0][1] : mat[0][2]);
        case 3:
            return pos == 0 ? mat[1][0] : (pos == 1 ? mat[1][1] : mat[1][2]);
        case 4:
            return pos == 0 ? mat[2][0] : (pos == 1 ? mat[2][1] : mat[2][2]);
        case 5:
            return pos == 0 ? mat[0][0] : (pos == 1 ? mat[1][0] : mat[2][0]);
        case 6:
            return pos == 0 ? mat[0][1] : (pos == 1 ? mat[1][1] : mat[2][1]);
        default:
            return pos == 0 ? mat[0][2] : (pos == 1 ? mat[1][2] : mat[2][2]);
    }
}

void illegal()
{
    printf("illegal\n");
    exit(0);
}

int main()
{
    scanf("%s\n", mat[0]);
    scanf("%s\n", mat[1]);
    scanf("%s\n", mat[2]);

    int Xs = 0, Os = 0;

    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++) {
            Xs += mat[i][j] == 'X' ? 1 : 0;
            Os += mat[i][j] == '0' ? 1 : 0;
        }

    if (Xs != Os && Xs != Os + 1)
        illegal();

    int XLines = 0, OLines = 0;

    for (int ln = 0; ln < LINE_NUM; ln++) {
        bool XLine = true, OLine = true;
        for (int i = 0; i < 3; i++) {
            auto symbol = lines(ln, i);
            XLine &= symbol == 'X';
            OLine &= symbol == '0';
        }

        XLines += XLine ? 1 : 0;
        OLines += OLine ? 1 : 0;
    }

    if (XLines > 0 && OLines > 0)
        illegal();

    if (Xs != Os && OLines > 0)
        illegal();

    if (Xs == Os && XLines > 0)
        illegal();

    if (XLines > 0) {
        printf("the first player won\n");
        return 0;
    }

    if (OLines > 0) {
        printf("the second player won\n");
        return 0;
    }

    if (Xs + Os == 9) {
        printf("draw\n");
        return 0;
    }

    printf("%s\n", Xs > Os ? "second" : "first");
    return 0;
}

// Author : Muhammad Rifayat Samee
// Problem : C
// Algorithm:
#pragma warning (disable : 4786)
#pragma comment(linker, "/STACK:16777216")

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<cstring>
#include<algorithm>
#include<string>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#define MAX(a,b) (a>b)?a:b
#define MIN(a,b) (a<b)?a:b
#define INF 987654321
#define pi (2*acos(0.0))
#define eps 1e-7

#ifdef ONLINE_JUDGE
#define i64 long long
#else
#define i64 __int64
#endif

using namespace std;
char str[4][4];
int isvalid(int a,int b){
	if(a>=0 && a<3 && b>=0 && b<3)return 1;
	return 0;
}
int main(){

	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int i,j,k,x,o,wx,wo;
	while(scanf("%s",str[0])!=EOF){
		for(i=1;i<3;i++){
			scanf("%s",str[i]);
		}
		x = 0;
		o = 0;
		for(i=0;i<3;i++){
			for(j=0;j<3;j++){
				if(str[i][j] == 'X')x++;
				else if(str[i][j] =='0')o++;
			}
		}
		if(o>x){
			printf("illegal\n");
		}
		else if(x>o+1){
			printf("illegal\n");
		}
		else{
			wx = 0;
			wo = 0;
			if(str[0][0] == str[0][1] && str[0][0] == str[0][2]){
				if(str[0][0] == 'X')wx++;
				else if(str[0][0] == '0')
					wo++;
			}
			if(str[1][0] == str[1][1] && str[1][0] == str[1][2]){
				if(str[1][0] == 'X')wx++;
				else if(str[1][0] == '0')
					wo++;

			}
			if(str[2][0] == str[2][1] && str[2][0] == str[2][2]){
				if(str[2][0] == 'X')wx++;
				else if(str[2][0] == '0')
					wo++;

			}
			if(str[0][0] == str[1][0] && str[0][0] == str[2][0]){
				if(str[0][0] == 'X')wx++;
				else if(str[0][0] == '0')
					wo++;

			}
			if(str[0][1] == str[1][1] && str[1][1] == str[2][1]){
				if(str[0][1] == 'X')wx++;
				else if(str[0][1] == '0')
					wo++;

			}
			if(str[0][2] == str[1][2] && str[1][2] == str[2][2]){
				if(str[0][2] == 'X')wx++;
				else if(str[0][2] == '0')
					wo++;

			}
			if(str[0][0] == str[1][1] && str[1][1] == str[2][2]){
				if(str[0][0] == 'X')wx++;
				else if(str[0][0] == '0')
					wo++;

			}
			if(str[0][2] == str[1][1] && str[1][1] == str[2][0]){
				if(str[0][2] == 'X')wx++;
				else if(str[0][2] == '0')
					wo++;

			}
			if(wx && wo){
				printf("illegal\n");
			}
			else{
				if(wx){
					if(o==x)
						printf("illegal\n");
					else
						printf("the first player won\n");
				}
				else if(wo){
					if(x>o)
						printf("illegal\n");
					else
						printf("the second player won\n");
				}
				else{
					if(o+x==9){
						printf("draw\n");

					}
					else if(o==x){
						printf("first\n");
					}
					else
						printf("second\n");
				}
			}
		}

	}
	return 0;
}

#include<iostream>
#include <string>
#include<vector>
using namespace std;
int IsXWinner(vector<vector<string>> field)
{
	bool dioganalWin = false;
	bool verOrHorWin = false;
	for (int i = 0; i < 3; i++)
	{
		if (field[i][0] == "X")
		{
			if (field[i][1] == "X")
			{
				if (field[i][2] == "X")
				{
					verOrHorWin = true;
					break;
				}

			}
		}
		if (field[0][i] == "X")
		{
			if (field[1][i] == "X")
			{
				if (field[2][i] == "X")
				{
					verOrHorWin = true;
					break;
				}
			}
		}

	}

	if (field[1][1] == "X")
	{
		if (field[0][0] == "X")
		{
			if (field[2][2] == "X")
				dioganalWin = true;
		}
		if (field[0][2] == "X")
		{
			if (field[2][0] == "X")
				dioganalWin = true;
		}
	}

	/*if (dioganalWin && verOrHorWin)
		return 3;*/
	if (dioganalWin || verOrHorWin)
		return 1;
	return 0;
}
int Is0Winner(vector<vector<string>> field)
{
	bool dioganalWin = false;
	bool verOrHorWin = false;
	for (int i = 0; i < 3; i++)
	{
		if (field[i][0] == "0")
		{
			if (field[i][1] == "0")
			{
				if (field[i][2] == "0")
				{
					verOrHorWin = true;
					break;
				}
			}
		}
		if (field[0][i] == "0")
		{
			if (field[1][i] == "0")
			{
				if (field[2][i] == "0")
				{
					verOrHorWin = true;
					break;
				}
			}
		}
	}
	if (field[1][1] == "0")
	{
		if (field[0][0] == "0")
		{
			if (field[2][2] == "0")
				dioganalWin = true;
		}
		if (field[0][2] == "0")
		{
			if (field[2][0] == "0")
				dioganalWin = true;
		}
	}

	//if (dioganalWin && verOrHorWin)
	//	return 3;
	if (dioganalWin || verOrHorWin)
		return 1;
	return 0;
}

int main()
{
	int crosses = 0, cells = 0;
	vector<vector<string>> field(3, vector<string>(3));
	for (int i = 0; i < 3; i++)
	{
		string buffer;
		cin >> buffer;
		for (int j = 0; j < 3; j++)
		{
			field[i][j] = buffer[j];
		}
	}
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			if (field[i][j] == "X")
				crosses++;
			if (field[i][j] == "0")
				cells++;
		}
	}
	if (abs(crosses - cells) >= 2 || crosses - cells < 0)
	{
		cout << "illegal";
		cin.get();
		cin.get();
		return 0;
	}
	if (Is0Winner(field) == 1 && IsXWinner(field) == 1)
	{
		cout << "illegal";
		cin.get();
		cin.get();
		return 0;
	}
	if (Is0Winner(field) == 3 || IsXWinner(field) == 3)
	{
		cout << "illegal";
		cin.get();
		cin.get();
		return 0;
	}
	if (Is0Winner(field) == 1)
	{
		if (crosses - cells == 1)
		{
			cout << "illegal";
			cin.get();
			cin.get();
			return 0;
		}
		else
		{
			cout << "the second player won";
			cin.get();
			cin.get();
			return 0;
		}
	}
	if (IsXWinner(field) == 1)
	{
		if (crosses - cells == 0)
		{
			cout << "illegal";
			cin.get();
			cin.get();
			return 0;
		}
		else
		{
			cout << "the first player won";
			cin.get();
			cin.get();
			return 0;
		}
	}
	bool gameIsOver = false;
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			if (field[i][j] == ".")
			{
				gameIsOver = false;
				break;
			}
			else
			{
				gameIsOver = true;
			}
		}
		if (!gameIsOver)
			break;
	}
	if (gameIsOver)
	{
		cout << "draw";
		cin.get();
		cin.get();
		return 0;
	}
	if (crosses - cells == 1)
	{
		cout << "second";
		cin.get();
		cin.get();
		return 0;
	}
	else
	{
		cout << "first";
		cin.get();
		cin.get();
		return 0;
	}
	cin.get();
	cin.get();
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
char p[6][6];//棋盘
int f,s,pf,ps;//f是'X'的个数，s是'0'的个数，pf是判断第一个人有没有赢，ps则是判断第二个人；
void dfs(int a,int b,int c,int d,int e,char t)//a,b是方向,c,d是当前的坐标,e是个数,t是棋子的类型('X'或'0')
{
	if(e==3){//如果3个连成了一条线
		if(t=='X')pf=1;//第一个人赢
		else ps=1;//第二个人赢
		return;//返回
	}
	else if(a+c<4&&a+c>0&&b+d<4&&b+d>0&&p[a+c][b+d]==t)//如果没出界，并且是同样的棋子
		dfs(a,b,a+c,b+d,e+1,t);//访问下一个坐标
}
int main()
{
	for(int x=1;x<4;x++)
		for(int y=1;y<4;y++){
			cin>>p[x][y];
			if(p[x][y]=='X')f++;
			if(p[x][y]=='0')s++;
		}
	if(s>f||f-s>1){
		cout<<"illegal";
		return 0;
	}
	for(int x=1;x<4;x++)
		for(int y=1;y<4;y++)
			if(p[x][y]!='.')//如果这个地方不是空的
				for(int a=-1;a<2;a++)
					for(int b=-1;b<2;b++)
						if(a!=0||b!=0)//并且方向正确
							dfs(a,b,x,y,1,p[x][y]);
	if((pf&&ps)||(pf&&f==s)||(ps&&f>s))cout<<"illegal";
	else if(pf)cout<<"the first player won";
	else if(ps)cout<<"the second player won";
	else if(f+s==9)cout<<"draw";
	else if(f>s)cout<<"second";
	else cout<<"first";
	return 0;
}

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<stack>
#include<set>
#define fd(x) for(int i=0;i<x;i++)
#define fdd(a,x) for(int i=a;i<x;i++)
#define mst(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<pii,int> ppi;
char a[3][3];
bool win(char ch){//这个函数不能用....
    for(int i=0;i<3;++i){
        //cout<<a[i][0]<<a[i][1]<<a[i][2]<<endl;
        if(a[i][0]==ch&&a[i][1]==ch&&a[i][2]==ch){
            return true;
        }
    }
    for(int i=0;i<3;++i){
        if(a[0][i]==ch&&a[1][i]==ch&&a[2][i]==ch){
            return true;
        }
    }
    if(a[0][0]==ch&&a[1][1]==ch&&a[2][2]==ch){
        return true;
    }
    if(a[2][0]==ch&&a[1][1]==ch&&a[0][2]==ch){
        return true;
    }
    // cout<<'e'<<endl;
    return false;
}
int main()
{
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    int cntX=0,cnt0=0;
    bool Xwin,Win0;
    for(int i=0;i<3;++i){
        for(int j=0;j<3;++j){
            cin>>a[i][j];
            if(a[i][j]=='X'){
                ++cntX;
            }
            if(a[i][j]=='0'){
                ++cnt0;
            }
        }
    }
    Xwin=win('X');
    Win0=win('0');
    // cout<<Win0<<' '<<Xwin<<endl;
    // for(int i=0;i<3;++i){
    //     for(int j=0;j<3;++j){
    //         cout<<a[i][j];
    //     }
    //     cout<<endl;
    // }
        if(abs(cntX-cnt0)>=2||(Xwin&&Win0)||cntX<cnt0||(Win0&&(cntX>cnt0))||(Xwin&&(cntX<=cnt0))){
            cout<<"illegal"<<endl;
            return 0;
        }
    if(!(Xwin||Win0)){
        if(cntX+cnt0==9){
            cout<<"draw"<<endl;
            return 0;
        }
        if(abs(cntX-cnt0)==1){
            cout<<"second"<<endl;
        }
        if(abs(cntX-cnt0)==0){
            cout<<"first"<<endl;
        }
    }else{
        if(Xwin){
                cout<<"the first player won"<<endl;
        }
        if(Win0){
                cout<<"the second player won"<<endl;
        }
    }
    return 0;
}

// clang-format off
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#ifndef godfather
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#endif

// #define int long long
typedef long long ll;
typedef long double ld;
typedef long double f80;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef gp_hash_table<int, int> umap;
typedef tree<int, null_type, less<int>, rb_tree_tag,
        tree_order_statistics_node_update> oset;
typedef pair<pii, int> piii;
typedef vector<int> vi;
typedef vector<pii> vii;

#define INF 100000000000000000
#define inf 1000000000
#define MOD  1000000007
#define PI 3.1415926535897932385

#define pb push_back
#define bitc __builtin_popcountll
#define mp make_pair
#define all(ar) ar.begin(), ar.end()

#define fr(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define rep(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define foreach(it, ar) for (auto it = ar.begin(); it != ar.end(); it++)
#define fil(ar, val) memset(ar, val, sizeof(ar))  // 0x3f for inf, 0x80 for -INF can also use with pairs

#ifdef godfather
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.first); __p(a.second); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
    cout<<name<<" : ";__p(arg1);cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
    int bracket=0,i=0;
    for(; ;i++)
        if(names[i]==','&&bracket==0)
            break;
        else if(names[i]=='(')
            bracket++;
        else if(names[i]==')')
            bracket--;
    const char *comma=names+i;
    cout.write(names,comma-names)<<" : ";
    __p(arg1);
    cout<<"| ";
    __f(comma+1,args...);
}
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define trace(...)
#define end_routine()
#endif

mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<int>(l, r)(rng32)
inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
ll modpow(ll b, ll e, ll mod=MOD) {
    ll ans=1; for(;e;b=b*b%mod,e/=2) if(e&1) ans=ans*b%mod; return ans; }

ld modp(ld b, ll e) {
    ld ans=1; for(;e;b=b*b,e/=2) if(e&1) ans=ans*b; return ans; }

void solve()
{
    // START
    vector<string>vec(3);
    for(int i=0;i<3;i++)
        cin>>vec[i];
    ll x=0,o=0;
    ll xx[3]={0}, xy[3]={0};
    ll ox[3]={0}, oy[3]={0};
    bool okax=false,okay=false;
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            if(vec[i][j]=='X'){
                xx[i]++;
                xy[j]++;
                if(xx[i]==3 || xy[j]==3) okax=true;
                x++;
            }else if(vec[i][j]=='0'){
                // cout<<"ew"<<endl;
                ox[i]++;
                oy[j]++;
                if(ox[i]==3 || oy[j]==3) okay=true;
                o++;
            }
        }
    }
    if(((vec[0][0]==vec[1][1]&& vec[1][1]==vec[2][2]) || (vec[2][0]==vec[1][1]&&vec[1][1]==vec[0][2]))&& vec[1][1]!='.')
    {
        if(vec[1][1]=='X') okax=true;
        else okay=true;
    }
    if(abs(x-o)>=2 || o>x||(okax && okay||(x>o && okay )) ||(x==o && okax)){
        cout<<"illegal"<<endl;
    }
    else if(okax){
        cout<<"the first player won"<<endl;
    }else if(okay){
        cout<<"the second player won"<<endl;
    }else if(x+o==9&& !okax && !okay ){
        cout<<"draw"<<endl;
    }else if(x==o){
        cout<<"first"<<endl;
    }else{
        cout<<"second"<<endl;
    }
    // END
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0), cout.precision(15);   cout<<fixed;
    #ifdef godfather
        cin.exceptions(cin.failbit);
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    // cin>>t;
    fr(i,1,t)
    {
        solve();
    }
    end_routine();
}

#include<bits/stdc++.h>
using namespace std;
string a[3];
bool win (char c) {
	for (int i=0; i<3; ++i) {
		if (a[i][0] == c && a[i][1] == c && a[i][2] == c)
			return true;
		if (a[0][i] == c && a[1][i] == c && a[2][i] == c)
			return true;
	}
	if (a[0][0] == c && a[1][1] == c && a[2][2] == c)
		return true;
	if (a[0][2] == c && a[1][1] == c && a[2][0] == c)
		return true;
	return false;
}

void no() {
	puts ("illegal");
	exit(0);
}

int main() {

	for (int i=0; i<3; ++i)
		cin >> a[i];

	int c1 = 0,  c2 = 0;
	for (int i=0; i<3; ++i)
		for (int j=0; j<3; ++j)
			if (a[i][j] == 'X')
				++c1;
			else if (a[i][j] == '0')
				++c2;
	if (! (c1 == c2 || c1 == c2 + 1))
		no();

	if (win ('X') && c1 == c2)
		no();
	if (win ('0') && c1 > c2)
		no();
	if (win ('X') && win ('0'))
		no();

	if (win ('X'))
		puts ("the first player won");
	else if (win ('0'))
		puts ("the second player won");
	else if (c1 + c2 == 9)
		puts ("draw");
	else if (c1 == c2)
		puts ("first");
	else
		puts ("second");

}

#include <stdio.h>

char str[3][3];

int main()
{
  int i,j,countx,count0;
  int count,k;
  char ch;
  for(i = 0;i<3;i++)
  {
      if(i>0)
          getchar();
   for(j = 0;j<3;j++)
       scanf("%c",&str[i][j]);
  }
  countx = 0;count0 = 0;
  for(i = 0;i<3;i++)
      for(j = 0;j<3;j++)
      {
          if(str[i][j]=='X')
              countx++;
          else if(str[i][j]=='0')
              count0++;
      }
  if(countx-count0==0||countx-count0==1)
  {
    count = 0;
    for(i = 0;i<3;i++)
    {
        if(str[i][0]==str[i][1]&&str[i][1]==str[i][2]&&str[i][2]!='.')
        {
            count++;
            ch = str[i][0];
        }
    }
    for(j = 0;j<3;j++)
    {
        if(str[0][j]==str[1][j]&&str[1][j]==str[2][j]&&str[2][j]!='.')
        {
            count++;
            ch = str[0][j];
        }
    }
    if(str[0][0]==str[1][1]&&str[1][1]==str[2][2]&&str[2][2]!='.')
    {
        count++;
        ch = str[0][0];
    }
    if(str[0][2]==str[1][1]&&str[1][1]==str[2][0]&&str[2][0]!='.')
    {
        count++;
        ch = str[0][2];
    }
    if(count==0||count==1||count==2)
    {
        if(count==1)
        {
            if(ch=='X')
            {
                if(countx-count0==1)
                printf("the first player won\n");
                else
                 printf("illegal\n");
            }
            else
            {
                if(countx==count0)
                printf("the second player won\n");
                else
                 printf("illegal\n");
            }
        }
        else if(count==2)
        {
            if(countx==5)
            printf("the first player won\n");
            else
              printf("illegal\n");
        }
        else
        {
            k = 0;
            for(i = 0;i<3;i++)
                for(j = 0;j<3;j++)
                    if(str[i][j]=='.')
                        k++;
            if(k==0)
                printf("draw\n");
            else
            {
                if(countx==count0)
                    printf("first\n");
                else
                    printf("second\n");
            }
        }
    }
    else
    {
      printf("illegal\n");
    }
  }
  else
      printf("illegal\n");
  return 0;
}

//Codeforces - 3C - Tic-tac-toe

#include<bits/stdc++.h>

#define FOR(i,a,n) for(int i=(a);i<(n);i++)
#define REP(i,n) FOR(i, 0, n)
using namespace std;

char s[5][5];
bool win(char c){
	REP(i,3){
		bool w=true;
		REP(j,3) if (s[j][i]!=c) w=false;
		if (w){
			return true;
		}
	}
	REP(i,3){
		bool w=true;
		REP(j,3) if (s[i][j]!=c) w=false;
		if (w){
			return true;
		}
	}
	bool w=true;
	REP(i,3) if (s[i][i]!=c) w=false;
	if (w){
		return true;
	}
	w=true;
	REP(i,3) if (s[i][2-i]!=c) w=false;
	if (w){
		return true;
	}
	return false;
}
int main(){
	REP(i,3) scanf("%s",s[i]);
	int c1=0,c2=0;
	REP(i,3){
		REP(j,3) {
			if (s[i][j]=='X') c1++;
			else if (s[i][j]=='0') c2++;
		}
	}
	if (c1-c2!=1 && c1-c2!=0 || win('X') && win('0') || c1>c2 && win('0') || c1==c2 && win('X')){
		printf("illegal\n");
		return 0;
	}
	if (win('X')){
		printf("the first player won\n");
	} else if (win('0')){
		printf("the second player won\n");
	} else if (c1+c2==9) {
		printf("draw\n");
	} else if (c1==c2) printf("first\n");
	else printf("second\n");
	return 0;
}

#include <iostream>
using namespace std;
#include <string>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <string>
int main(){
	char arr[3][3]; int x=0, y=0,n=0,l=0;
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
		{
			cin >> arr[i][j];
			if (arr[i][j] == 'X')x++;
			else if (arr[i][j] == '0')y++;
		}

		for (int i = 0; i < 3; i++)
		{
			if (arr[i][0] == arr[i][1]&& arr[i][1] == arr[i][2] && arr[i][1] != '.')
				if (arr[i][0] == 'X')
					n++;
				else
					l++;

			if (arr[0][i] == arr[1][i]&& arr[1][i] == arr[2][i]&& arr[1][i] != '.')
				if (arr[0][i] == 'X')
					n++;
				else
					l++;
		}
		if (arr[0][0] == arr[1][1]&& arr[1][1] == arr[2][2]&& arr[1][1]!='.')
			if (arr[0][0] == 'X')
				n++;
			else
				l++;

		if (arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0]&&arr[1][1] !='.')
			if (arr[0][2] == 'X')
				n++;
			else
				l++;

		if (x + y < 9 && !n&&!l)
			if (x < y || x - y>1)
				cout << "illegal";
			else if (x > y)
				cout << "second";
			else
				cout << "first";
		else
			if (n && l || x < y || x - y>1 || x > y&&l || x == y&&n || x < y || x - y>1)
				cout << "illegal";
			else if (!n && !l)
				cout << "draw";
			else if (n)
				cout << "the first player won";
			else
				cout << "the second player won";

	return 0;
}

#include<iostream>
    #include<vector>
    using namespace std;
    int main()
    {
        int x=0,y=0;
        string s;
        string t;
        int r=0;
        vector<string>v;
        //v[0]="X.X"; v[1]="X.0"; v[2]="0.0";
        while(getline(cin,s))
        {
             v.push_back(s);
        }

             for(int i=0;i<3;i++)
             {
                     for(int j=0;j<3;j++)
                     {
                     if(v[i][j]=='X')x++;
                             else if(v[i][j]=='0')y++;
                     }
             }
       for(int i=0;i<3;i++)
        {
               if(y>x){r=1;t="illegal";cout<<"illegal"<<endl; break;}
                if(v[i][0]==v[i][1] && v[i][0]==v[i][2] && v[i][0]!='.')
                {
                     if(v[i][0]=='X'){if(t!="the second player won"   && (x-y)==1)t="the first player won";
                     else {s="illegal";cout<<"illegal"<<endl; r=1;break;}}
                     else if(t!="the first player won"  && x==y)t="the second player won";
                     else {s="illegal";cout<<"illegal"<<endl;r=1; break;}
                }
                 if(v[0][i]==v[1][i] && v[0][i]==v[2][i] && v[0][i]!='.')
                {
                     if(v[0][i]=='X'){if(t!="the second player won"   && (x-y)==1)t="the first player won";
                     else {s="illegal";cout<<"illegal"<<endl; r=1;break;}}
                     else if(t!="the first player won" && x==y)t="the second player won";
                     else {s="illegal";cout<<"illegal"<<endl;r=1; break;}
                }
        }
        if(t!=""){if(r==0)cout<<t<<endl;}
         else if(v[0][0]==v[1][1] && v[0][0]==v[2][2] && v[0][0]!='.')
         {
                     if(v[0][0]=='X' && (x-y)==1)cout<<"the first player won"<<endl;
                     else if(v[0][0]=='0' && x==y)cout<<"the second player won"<<endl;
                     else if(s!="illegal")cout<<"illegal"<<endl;
         }
         else if(v[0][2]==v[1][1] && v[0][2]==v[2][0] && v[0][2]!='.')
         {
                     if(v[0][2]=='X' && (x-y)==1)cout<<"the first player won"<<endl;
                     else if(v[0][2]=='0' && x==y)cout<<"the second player won"<<endl;
                     else if(s!="illegal")cout<<"illegal"<<endl;
         }
         else
         {
             if((x-y)>1){if(s!="illegal")cout<<"illegal"<<endl;}
             else if((x+y)==9 && (x-y)==1){if(s!="illegal")cout<<"draw"<<endl;}
             else if(x>y){if(s!="illegal")cout<<"second"<<endl;}
             else {if(s!="illegal")cout<<"first"<<endl;}
        }
         return 0;
    }

#include<iostream>
using namespace std;
int cn;
int on;
char map[4][4];
int check(){
	int x3 = 0;
	int o3 = 0;
	for (int i = 1; i <= 3; ++i){
		int xw = 0;
		int ow = 0;
		for (int j = 1; j <= 3; ++j){
			if (map[i][j] == 'X'){
				++xw;
			}
			else if (map[i][j] == '0'){
				++ow;
			}
		}
		if (xw == 3)++x3;
		if (ow == 3)++o3;
	}
	for (int i = 1; i <= 3; ++i){
		int xw = 0;
		int ow = 0;
		for (int j = 1; j <= 3; ++j){
			if (map[j][i] == 'X'){
				++xw;
			}
			else if (map[j][i] == '0'){
				++ow;
			}
		}
		if (xw == 3)++x3;
		if (ow == 3)++o3;
	}
	int xw = 0;
	int ow = 0;
	for (int i = 1; i <= 3; ++i){
		if (map[i][i] == 'X'){
			++xw;
		}
		else if (map[i][i] == '0'){
			++ow;
		}
	}
	if (xw == 3)++x3;
	if (ow == 3)++o3;
	xw = 0;
	ow = 0;
	for (int i = 1; i <= 3; ++i){
		if (map[3 - i + 1][i] == 'X'){
			++xw;
		}
		else if (map[3 - i + 1][i] == '0'){
			++ow;
		}
	}
	if (xw == 3)++x3;
	if (ow == 3)++o3;
	if (x3&&o3)return -1;//illegal
	if (x3)return 1;
	if (o3)return 2;
	else return 0;
}
int main()
{
	while (cin >> map[1]+1){
		bool emp = true;
		cn = 0;
		on = 0;
		cin >> map[2]+1;
		cin >> map[3]+1;
		int ttt = check();
		for (int i = 1; i <= 3; ++i){
			for (int j = 1; j <= 3; ++j){
				if (map[i][j] == 'X'){
					++cn;
				}
				else if (map[i][j] == '0'){
					++on;
				}
				else{
					emp = false;
				}
			}
		}
		if (cn<on || cn>on + 1 || ttt == -1){
			cout << "illegal" << endl;
		}
		else if (ttt == 1){
			if (cn == on){
				cout << "illegal" << endl;
			}
			else
				cout << "the first player won" << endl;
		}
		else if (ttt == 2){
			if (cn != on)
				cout << "illegal" << endl;
			else
				cout << "the second player won" << endl;
		}
		else if (emp){
			cout << "draw" << endl;
		}
		else if (cn == on){
			cout << "first" << endl;
		}
		else if (cn == on + 1){
			cout << "second" << endl;
		}
	}
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int xcnt,ocnt,dots,countx(0),county(0);
char brd[3][3];

void check(char input){
for(int i=0;i<3;i++){

    //line of Columns
    if(brd[0][i]==input&& brd[1][i]== input&& (brd[2][i]==input) && input=='X') {countx++;}
    else if (brd[0][i]==input&& brd[1][i]== input&& (brd[2][i]==input) && input=='0') {county++;}
    //line of Raws
    if(brd[i][0]==input&& brd[i][1]==input && (brd[i][2]==input) && input=='X'){countx++;}
    else if(brd[i][0]==input&& brd[i][1]==input && (brd[i][2]==input) && input=='0'){county++;}
}
    //line of Right Diagonal
    if(brd[0][0]==input && brd[1][1]==input && brd[2][2]==input && input=='X') {countx++;}
    else if(brd[0][0]==input && brd[1][1]==input && brd[2][2]==input && input=='0') {county++;}

     //line of left Diagonal
    if(brd[0][2]==input && brd[1][1]==input && brd[2][0]== input && input=='X') {countx++;}
    else if(brd[0][2]==input && brd[1][1]==input && brd[2][0]== input && input=='0') {county++;}

}

bool legal(){
    xcnt,ocnt=0;
   for(int i=0;i<3;i++){
       for(int j=0;j<3;j++)
        if(brd[i][j]=='X')
            xcnt++;
        else if (brd[i][j]=='0')
            ocnt++;

       else if (brd[i][j]=='.')
            dots++;
       }
       check('X');
       check('0');

       if(xcnt-ocnt>=2 || ocnt>xcnt) return false;
       if(county==1 &&xcnt>ocnt) return false;
       if(countx >2 || county >2) return false;
       if(countx==1 &&xcnt==ocnt)return false;

       return true;
}

int main(){
    for(int i=0;i<3;i++)
        cin>> brd[i];

    while(true){

        if(!legal()) {cout<<"illegal"; break;}

        else if((countx==1 ||countx==2) && xcnt>ocnt) {cout<<"the first player won"; break;}

        else if((county==1 || county==2) && countx==0) {cout<<"the second player won"; break;}

        else

        if(dots==0) {cout<<"draw"; break;}

        else

         if (xcnt==ocnt) {cout<<"first"; break;}

        else {cout<<"second"; break;}

    }

}

#include<bits/stdc++.h>
#define two(x) (1<<(x))
using namespace std;
string b[3];
int win(char ch){
	vector<int>v;
	int mask=(1<<9)-1,mask2=0,cnt=0;
	for(int i=0;i<3;i++)v.push_back(two(i*3)+two(i*3+1)+two(i*3+2));
	for(int i=0;i<3;i++)v.push_back(two(i)+two(i+3)+two(i+6));
	v.push_back(two(0)+two(4)+two(8));
	v.push_back(two(2)+two(4)+two(6));
	for(int i=0;i<3;i++)for(int j=0;j<3;j++)if(b[i][j]==ch)mask2|=two(i*3+j);
	for(int i=0;i<v.size();i++)if((mask2&v[i])==v[i]){
		cnt++;
		mask&=v[i];
	}
	if(cnt==0)return 0;
	if(mask==0)return 2;
	return 1;
}
int count(char ch){
	int ans=0;
	for(int i=0;i<3;i++)for(int j=0;j<3;j++)if(b[i][j]==ch)ans++;
	return ans;
}
string f()
{
	int a=count('X'),b=count('0');
	if(a<b||a>b+1)return"illegal";
	int player=((a==b)?0:1);
	int wa=win('X'),wb=win('0');
	if(wa==2||wb==2||(player==0&&wa==1)||(player==1&&wb==1))return"illegal";
	if(wa==1)return"the first player won";
	if(wb==1)return"the second player won";
	if(a+b==9)return"draw";
	if(player==0)return"first";
	return"second";
}
int main()
{
	for(int i=0;i<3;i++)cin>>b[i];
	string a=f();
	cout<<a<<endl;
	return 0;
}

#include<iostream>
#include<vector>
using namespace std;
vector<char> v1,v2;
char m[3][3];
int x,o;
bool judge_illegal(){
	if(x-o>1)return true;
	else if(o>x)return true;
	else return false;
}
char judge_horizontal(){
	for(int i=0;i<3;i++){
		bool f=true;
		for(int j=0;j<2;j++){
			if(m[i][j]!=m[i][j+1])f=false;
		}
		if(f)return m[i][0];
	}
	return ' ';
}
char judge_vertical(){
	for(int j=0;j<3;j++){
		bool f=true;
		for(int i=0;i<2;i++){
			if(m[i][j]!=m[i+1][j])f=false;
		}
		if(f)return m[0][j];
	}
	return ' ';
}
char judge_diagonal(){
	if(m[0][0]==m[1][1] && m[1][1]==m[2][2])return m[0][0];
	else if(m[0][2]==m[1][1] && m[1][1]==m[2][0])return m[0][2];
	else return ' ';
}
int main(){
	for(int i=0;i<3;i++){
		cin>>m[i];
		for(int j=0;j<3;j++)if(m[i][j]=='X')x++;else if(m[i][j]=='0')o++;
	}
	for(int i=0;i<3;i++){
		bool f=true;
		for(int j=0;j<2;j++){
			if(m[i][j]!=m[i][j+1])f=false;
		}
		if(f && m[i][0]!='.')v1.push_back(m[i][0]);
	}
	for(int j=0;j<3;j++){
		bool f=true;
		for(int i=0;i<2;i++){
			if(m[i][j]!=m[i+1][j])f=false;
		}
		if(f && m[0][j]!='.')v2.push_back(m[0][j]);
	}
	if(v1.size()>1 || v2.size()>1){
		cout<<"illegal";
		return 0;
	}
	bool win1,win2;
	if(judge_horizontal()=='X' || judge_vertical()=='X' || judge_diagonal()=='X')win1=true;else win1=false;
	if(judge_horizontal()=='0' || judge_vertical()=='0' || judge_diagonal()=='0')win2=true;else win2=false;
	if(judge_illegal() || win2 && x>o || win1 && x==o)cout<<"illegal";
	else if(x+o==9 && !win1 && !win2)cout<<"draw";
	else if(win1)cout<<"the first player won";
	else if(win2)cout<<"the second player won";
	else if(o==x)cout<<"first";
	else cout<<"second";
	return 0;
}

#include<iostream>
#include<cmath>
using namespace std;

int cntx=0,cnt0=0,cntdot=0;
int aryx[10][2],ary0[10][2];
char chess[3][3];
int Win(char);

int main()
{

    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
        {
            cin>>chess[i][j];
            if(chess[i][j]=='X')
            {
                aryx[cntx][0]=i;
                aryx[cntx][1]=j;
                cntx++;
            }
            if(chess[i][j]=='0')
            {
                ary0[cnt0][0]=i;
                ary0[cnt0][1]=j;
                cnt0++;
            }

            if(chess[i][j]=='.')
            {
                cntdot++;
            }
        }
        if(cntx-cnt0<0||cntx-cnt0>1)
        {
            cout<<"illegal"<<endl;
            return 0;
        }
        if(cntx<3)
        {
            if(cntx>cnt0)
            {
                cout<<"second"<<endl;
                return 0;
            }
            else
            {
                cout<<"first"<<endl;
                return 0;
            }
        }
        if(Win('X')>1||Win('0')>1)
        {
            if(cntdot!=0)
                cout<<"illegal"<<endl;
            else
                cout<<"the first player won"<<endl;
            return 0;
        }
        if(Win('X')==1&&Win('0')==1)
        {
            cout<<"illegal"<<endl;
            return 0;
        }
        if(Win('X')==1&&Win('0')==0)
        {
            if(cntx>cnt0)
            cout<<"the first player won"<<endl;
            else
                cout<<"illegal"<<endl;
            return 0;
        }
        if(Win('X')==0&&Win('0')==1)
        {
            if(cntx==cnt0)
            cout<<"the second player won"<<endl;
            else
            cout<<"illegal"<<endl;
            return 0;
        }
        if(Win('X')==0&&Win('0')==0)
        {
            if(cntdot==0)
            {
                cout<<"draw"<<endl;
                return 0;
            }
            else
            {
                if(cntdot>=2&&cntdot<=4)
                {
                    if(cntx>cnt0)
                    {
                        cout<<"second"<<endl;
                        return 0;
                    }
                    if(cntx==cnt0)
                    {
                        cout<<"first"<<endl;
                        return 0;
                    }
                }

                if(cntdot==1)
                {
                    cout<<"first"<<endl;
                    /*int dotx,doty;
                    for(int i=0;i<3;i++)
                        for(int j=0;j<3;j++)
                        {
                            if(chess[i][j]=='.')
                            {
                                dotx=i;
                                doty=j;
                            }
                        }
                    aryx[cntx][0]=dotx;
                    aryx[cntx][1]=doty;
                    cntx++;
                    chess[dotx][doty]='X';
                    if(Win('X')==1)
                    {
                        cout<<"he first player won"<<endl;
                        return 0;
                    }
                    else
                    {
                        cout<<"draw"<<endl;
                        return 0;
                    }*/
                }
            }
        }

    return 0;
}
int Win(char ch)
{
    int xary[3]={0},yary[3]={0};
    for(int i=0;i<(ch=='X'?cntx:cnt0);i++)
    {
        if(ch=='X')
        {
        xary[aryx[i][0]]++;
        yary[aryx[i][1]]++;
        }
        else
        {
            xary[ary0[i][0]]++;
            yary[ary0[i][1]]++;
        }
    }
    int t=0;
    for(int i=0;i<3;i++)
    {
        if(xary[i]==3)
            t++;
        if(yary[i]==3)
            t++;
    }
    if(chess[0][0]==ch&&chess[1][1]==ch&&chess[2][2]==ch)
        t++;
    if(chess[0][2]==ch&&chess[1][1]==ch&&chess[2][0]==ch)
        t++;
    return t;

}

#include <cstdio>
#include <cstring>

//#define INPUT_REDIRECTION
#define MAXN 5

char chess[MAXN][MAXN];
int countX;
int count0;

void input()
{
    countX = 0;
    count0 = 0;
    memset(chess,0,sizeof(chess[0][0])*MAXN*MAXN);

    char temp[MAXN];
    for(int i = 1;i<=3;i++)
    {
        fscanf(stdin,"%s",temp);
        chess[i][1] = temp[0];
        chess[i][2] = temp[1];
        chess[i][3] = temp[2];
    }

    for(int i = 1;i<=3;i++)
    {
        for(int j = 1; j<=3;j++)
        {
            if(chess[i][j]=='X')
            {
                countX ++;
            }
            else if(chess[i][j]=='0')
            {
                count0 ++;
            }
        }
    }
}

bool someone_won(char someone)
{
    bool result = false;

    for(int i = 1; i<=3;i++)
        if(chess[i][1]==chess[i][2]&&chess[i][2]==chess[i][3]&&chess[i][3]==someone)
            result = true;

    for(int i = 1; i<=3;i++)
        if(chess[1][i]==chess[2][i]&&chess[2][i]==chess[3][i]&&chess[3][i]==someone)
            result = true;

    if(chess[1][1]==chess[2][2]&&chess[2][2]==chess[3][3]&&chess[3][3]==someone)
        result = true;
    if(chess[3][1]==chess[2][2]&&chess[2][2]==chess[1][3]&&chess[1][3]==someone)
        result = true;

    return result;
}

void process()
{
    if(count0>countX||(countX-count0)>1)
    {
        fprintf(stdout,"illegal\n");
        return;
    }

    bool first_won = false;
    bool second_won = false;

    if(someone_won('X'))
    {
        first_won = true;
    }

    if(someone_won('0'))
    {
        second_won = true;
    }

    if(second_won&&first_won)
    {
        fprintf(stdout,"illegal\n");
        return;
    }

    if(first_won)
    {
        if(countX==count0||(countX-count0)>1)
        {
            fprintf(stdout,"illegal\n");
            return;
        }
        fprintf(stdout,"the first player won\n");
        return;
    }

    if(second_won)
    {
        if(countX>count0)
        {
            fprintf(stdout,"illegal\n");
            return;
        }
        fprintf(stdout,"the second player won\n");
        return;
    }

    if((countX+count0)==9)
    {
        fprintf(stdout,"draw\n");
        return;
    }

    if(countX>count0)
    {
        fprintf(stdout,"second\n");
        return;
    }
    else
    {
        fprintf(stdout,"first\n");
        return;
    }
}

int main()
{
#ifdef INPUT_REDIRECTION
	freopen("in.in","r",stdin);
#endif
    input();
    process();

    return 0;
}

#include<cstdio>

char grid[3][3];

int main(void)
{
	int fcount = 0, scount = 0;
	bool fwin = false, swin = false; ///局部变量一定要赋值后才能用
	for (int i = 0; i < 3; ++i)
	{
		for (int j = 0; j < 3; j++)
		{
			grid[i][j] = getchar();
			if (grid[i][j] == 'X')
				++fcount;
			else if (grid[i][j] == '0')
				++scount;
		}
		getchar();
	}
	if (fcount - scount > 1 || fcount - scount < 0) ///永远是X(first)先走
		printf("illegal");
	else
	{
		///行
		for (int i = 0; i < 3; ++i)
		{
			if (grid[i][0] == grid[i][1] && grid[i][0] == grid[i][2] && grid[i][0] != '.')
			{
				if (grid[i][0] == 'X')
					fwin = true;
				else
					swin = true;
			}
		}
		if (fwin && swin)
		{
			printf("illegal");
			return 0;
		}
		else if (fwin || swin)
		{
			if (fwin && fcount == scount || swin && fcount > scount)
			{
				printf("illegal");
				return 0;
			}
			printf(fwin ? "the first player won" : "the second player won");
			return 0;
		}
		///列
		for (int i = 0; i < 3; ++i)
		{
			if (grid[0][i] == grid[1][i] && grid[0][i] == grid[2][i] && grid[0][i] != '.')
			{
				if (grid[i][0] == 'X')
					fwin = true;
				else
					swin = true;
			}
		}
		if (fwin && swin)
		{
			printf("illegal");
			return 0;
		}
		else if (fwin || swin)
		{
			if (fwin && fcount == scount || swin && fcount > scount)
			{
				printf("illegal");
				return 0;
			}
			printf(fwin ? "the first player won" : "the second player won");
			return 0;
		}
		///斜
		if (grid[1][1] != '.' && (grid[0][0] == grid[1][1] && grid[2][2] == grid[1][1] || grid[0][2] == grid[1][1] && grid[2][0] == grid[1][1]))
		{
			if (grid[1][1] == 'X')
				fwin = true;
			else
				swin = true;
		}
		if (fwin || swin)
		{
			if (fwin && fcount == scount || swin && fcount > scount)
			{
				printf("illegal");
				return 0;
			}
			printf(fwin ? "the first player won" : "the second player won");
			return 0;
		}
		printf(fcount > scount ? "second" : "first");
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e5+10;
const int INF=99999999;
const int mod=1e9;
const int N=1e6+10;
ll gcd(ll a,ll b){return (b==0)?a:gcd(b,a%b);}
ll lcm(ll a,ll b){return a*b /gcd(a,b);}
//struct node{int h,c;}a[maxn];
//bool cmp(node a,node b){if(a.h==b.h) return a.c<b.c; return a.h<b.h;}
//int vis[maxn], cost[maxn],num[201];
char G[4][4];
int judeg(char x)
{
    if(G[1][1]==G[2][2]&&G[2][2]==G[3][3]&&G[1][1]==x)
             return 0;
         for(int i=1;i<=3;i++)
             if(G[i][1]==G[i][2]&&G[i][2]==G[i][3]&&G[i][1]==x)
                return 0;
          for(int i=1;i<=3;i++)
             if(G[1][i]==G[2][i]&&G[2][i]==G[3][i]&&G[1][i]==x)
                  return 0;
         if(G[3][1]==G[2][2]&&G[2][2]==G[1][3]&&G[3][3]==x)
          return 0;
    return 1;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int x,o;
    x=o=0;
     for(int i=1;i<=3;i++)
     {
         for(int j=1;j<=3;j++)
         {
             cin>>G[i][j];
             if(G[i][j]=='X')
                x++;
             else if(G[i][j]=='0')
                o++;
                else
                    continue;
         }
     }
     if(abs(x-o)>1&&(x!=3||o!=3))
     {
         //cout<<x<<" "<<o<<endl;
         cout<<"illegal"<<endl;
     }
     else if(o==0&&x==0)
     {
         cout<<"draw"<<endl;
     }
     else
     {
         int flag=judeg('X');
         int glag=judeg('0');
         if(flag==glag&&flag==0)
            cout<<"illegal"<<endl;
         else if(flag!=glag)
         {
             if(flag==0&&x>o||flag==1&&o>x)
             cout<<"illegal"<<endl;
             else if(x!=o)
                cout<<"the first player won"<<endl;
             else
                cout<<"the second player won"<<endl;
         }
         else
         {
             if(o!=x)
                cout<<"second"<<endl;
             else
                cout<<"first"<<endl;
         }
     }

    return 0;
}

//============================================================================
// Name        : medo.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <bits/stdc++.h>
using namespace std;

string grid[3] ;

int main() {

	cin >> grid[0] >> grid[1] >> grid[2] ;
	int cnt_o = 0 , cnt_x = 0 ;
	for(int i = 0 ; i < 3 ; i++)
		for(int j = 0 ; j < 3 ; j++)
			if(grid[i][j] == '0')
				cnt_o++ ;
			else if(grid[i][j] == 'X')
				cnt_x++ ;
	if(cnt_o + cnt_x < 9 && (cnt_x == cnt_o || cnt_x - cnt_o == 1))
	{
		if(cnt_o < cnt_x)
			cout << "second" << endl;
		else
			cout << "first" << endl;
		return 0 ;
	}
	if(cnt_o != cnt_x && cnt_x - cnt_o != 1)
	{
		cout << "illegal" << endl;
		return 0 ;
	}
	bool x = false ;
	for(int i = 0 ; i < 3 ; i++)
	{
		int cx = 0 ;
		for(int j = 0 ; j < 3 ; j++)
				if(grid[i][j] == 'X')
					cx++ ;
		if(cx == 3)
		{
			x = true ;
			break ;
		}
	}
	if(x)
	{
		cout << "the first player won" << endl;
		return 0 ;
	}
	for(int i = 0 ; i < 3 ; i++)
	{
		int cx = 0 ;
		for(int j = 0 ; j < 3 ; j++)
				if(grid[j][i] == 'X')
					cx++ ;
		if(cx == 3)
		{
			x = true ;
			break ;
		}
	}
	if(x)
	{
		cout << "the first player won" << endl;
		return 0 ;
	}
	if(grid[0][0] == 'X' && grid[1][1] == 'X' && grid[2][2] == 'X')
	{
		cout << "the first player won" << endl;
		return 0 ;
	}
	if(grid[0][2] == 'X' && grid[1][1] == 'X' && grid[2][0] == 'X')
	{
		cout << "the first player won" << endl;
		return 0 ;
	}
	//oooooooooooooooooooooooooooooo
	bool o = false ;
	for(int i = 0 ; i < 3 ; i++)
	{
		int co = 0 ;
		for(int j = 0 ; j < 3 ; j++)
				if(grid[i][j] == '0')
					co++ ;
		if(co == 3)
		{
			o = true ;
			break ;
		}
	}
	if(o)
	{
		cout << "the second player won" << endl;
		return 0 ;
	}
	for(int i = 0 ; i < 3 ; i++)
	{
		int co = 0 ;
		for(int j = 0 ; j < 3 ; j++)
				if(grid[j][i] == '0')
					co++ ;
		if(co == 3)
		{
			o = true ;
			break ;
		}
	}
	if(o)
	{
		cout << "the second player won" << endl;
		return 0 ;
	}
	if(grid[0][0] == '0' && grid[1][1] == '0' && grid[2][2] == '0')
	{
		cout << "the second player won" << endl;
		return 0 ;
	}
	if(grid[0][2] == '0' && grid[1][1] == '0' && grid[2][0] == '0')
	{
		cout << "the second player won" << endl;
		return 0 ;
	}
	cout << "draw" << endl ;
	return 0;
}

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int mat[3][3],i,j;
	string s;
	for(i=0;i<3;i++)
	{
		cin>>s;
		for(j=0;j<3;j++)
		{
			if(s[j]=='X')
			mat[i][j]=1;
			if(s[j]=='0')
			mat[i][j]=2;
			if(s[j]=='.')
			mat[i][j]=0;
		}
	}
	int count1=0,count2=0,ans=0;
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			if(mat[i][j]==1)
			count1++;
			else if(mat[i][j]==2)
			count2++;
			if(mat[i][j]==1)
			{
				if(mat[i+1][j]==1 && mat[i+2][j]==1)
				ans=1;
				else if(mat[i][j+1]==1 && mat[i][j+2]==1)
				ans=1;
				else if(mat[i+1][j+1]==1 && mat[i+2][j+2]==1)
				ans=1;
				else if(mat[i+1][j-1]==1 && mat[i+2][j-2]==1)
				ans=1;
			}
			if(mat[i][j]==2)
			{
				if(mat[i+1][j]==2 && mat[i+2][j]==2)
				ans=2;
				else if(mat[i][j+1]==2 && mat[i][j+2]==2)
				ans=2;
				else if(mat[i+1][j+1]==2 && mat[i+2][j+2]==2)
				ans=2;
				else if(mat[i+1][j-1]==2 && mat[i+2][j-2]==2)
				ans=2;
		    }
		}
	}
	if(count1>3 || count2>3)
	cout<<"illegal"<<endl;
	else if(abs(count1-count2)>1)
	cout<<"illegal"<<endl;
    else if(count1==3 && ans==1)
	cout<<"the first player won"<<endl;
	else if(count2==3 && ans==2)
	cout<<"the second player won"<<endl;
	else if(count1==3 && count2==3 && ans==0)
	cout<<"draw"<<endl;
	else if(count1>count2)
	cout<<"second"<<endl;
	else if(count1==count2 && count1<3)
	cout<<"first"<<endl;
return 0;
}

#include<bits/stdc++.h>
using namespace std;
string s[3];

int check(char c)
{
    for(int i=0;i<3;i++)
    {
        int win = 1;
        for(int j=0;j<3;j++)
            if(s[i][j]!=c)
                win=0;
        if(win==1)return win;
    }

    if(s[0][0]==c&&s[1][1]==c&&s[2][2]==c)return 1;
    if(s[0][2]==c&&s[1][1]==c&&s[2][0]==c)return 1;
    return 0;
}
int main()
{
    for(int i=0;i<3;i++)
        cin>>s[i];
    int X=0,O=0;
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
            if(s[i][j]=='X')X++;
            else if(s[i][j]=='0')O++;
    int Win1 = check('X');
    int Win2 = check('0');
    if(Win1&&Win2)return puts("illegal"),0;
    if(Win1&&X-O!=1)return puts("illegal"),0;
    if(Win2&&X!=O)return puts("illegal"),0;
    if(X-O>1||O>X)return puts("illegal"),0;
    if(Win1)return puts("the first player won"),0;
    if(Win2)return puts("the second player won"),0;
    if(X+O==9)return puts("draw"),0;
    if(X==O)return puts("first"),0;
    if(O==X-1)return puts("second"),0;

}

//
// Created by Atrin Hojjat on 3/24/17.
//

#include <iostream>
#include <cstdio>

#define ILLEGAL {cout << "illegal" << endl;return 0;}
#define PLAYER_WINS(i) {printf("the %s player won",i==1 ? "first" : "second");return 0;}
#define DRAW {cout << "draw" << endl;return 0;}
#define PLAYER_MOVES(i) {cout << (i ? "first" : "second") << endl;return 0;}

using namespace std;

int main() {
  char t[3][3];
  int O = 0, X = 0;
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) {
      cin >> t[i][j];
      if (t[i][j] == '0')
        O++;
      else if (t[i][j] == 'X')
        X++;
    }

  if (abs(X - O) > 1 || X<O) ILLEGAL

  int xw = 0, ow = 0;
  for (int i = 0; i < 3; i++) {
    if (t[i][0] == t[i][1] && t[i][0] == t[i][2]) {
      if (t[i][0] == '0')
        ow++;
      else if (t[i][0] == 'X')
        xw++;
    }
    if (t[0][i] == t[1][i] && t[0][i] == t[2][i]) {
      if (t[0][i] == '0')
        ow++;
      else if (t[0][i] == 'X')
        xw++;
    }
  }
  if(ow +xw > 1)ILLEGAL
  if(xw)PLAYER_WINS(1)
  if(ow)PLAYER_WINS(2)

  PLAYER_MOVES(X == O)

  return 0;
}

#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <set>
#include <vector>
#include <queue>
using namespace std;
#define LL long long
string board[3];
int winner() {
    int ans=0, cnt=0;
    for (int i=0; i < 3; i++) {
        if (board[i][0]==board[i][1] && board[i][1] == board[i][2]) {
            if (board[i][0] == 'X') {
                ans = 1;
                cnt++;
            }
            else if (board[i][0] == '0') {
                ans = 2;
                cnt++;
            }
        }
        if (board[0][i]==board[1][i] && board[1][i] == board[2][i]) {
            if (board[0][i] == 'X') {
                ans = 1;
                cnt++;
            }
            else if (board[0][i] == '0') {
                ans = 2;
                cnt++;
            }
        }
    }
    if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
        if (board[1][1] == 'X') {
            ans = 1;
            cnt++;
        }
        else if (board[1][1] == '0') {
            ans = 2;
            cnt++;
        }
    }
    if (board[2][0] == board[1][1] && board[1][1] == board[0][2]) {
        if (board[1][1] == 'X') {
            ans = 1;
            cnt++;
        }
        else if (board[1][1] == '0') {
            ans = 2;
            cnt++;
        }
    }
    if (cnt > 1)
        return -1;
    else
        return ans;
}
int main() {
    ios_base::sync_with_stdio(false);
    for (int i=0; i < 3; i++)
        cin >> board[i];
    int cnt_x=0, cnt_0=0, cnt_dot=0;
    for (int i=0; i < 3; i++)
        for (int k=0; k < 3; k++) {
            if (board[i][k] == 'X')
                cnt_x++;
            else if (board[i][k] == '0')
                cnt_0++;
            else
                cnt_dot++;
        }
    int win = winner();
    if ((cnt_x == 0 && cnt_0 > 0) || abs(cnt_0-cnt_x) > 1 || (win == 1 && cnt_x > cnt_0)
            || (win == 2 && cnt_0 > cnt_x) || win == -1) {
        cout << "illegal\n";
        return 0;
    }
    if (win == 1)
        cout << "the first player won\n";
    else if (win == 2)
        cout << "the second player won\n";
    else {
        if (cnt_dot == 0)
            cout << "draw\n";
        else {
            int a = min(cnt_0, cnt_x);
            if (a == cnt_x)
                cout << "first\n";
            else
                cout << "second\n";
        }
    }
    return 0;
}

/*#include <string>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
#include <deque>
#include <string>
#include <map>
#include <utility>
#include <set>
using namespace std;
#define ll  long long
#define x first
#define y second
#define N 1000005
#define MAXN 999999
//DIV 2 203 C

int max(int  a, int b) { if (a > b) return a; else return b; }
//int min(int  a, int b) { if (a < b) return a; else return b; }
ll min(ll  a, ll b) { if (a < b) return a; else return b; }
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return a * (b / gcd(a, b)); }
vector <bool> isPrime(N + 1, true);
string tos(int n) { stringstream ss; ss << n; return ss.str(); }
void seive() {
	isPrime[0] = 0, isPrime[1] = 0; for (ll i = 2; i*i <= N; i++) if (isPrime[i]) for (ll j = i * i; j <= N; j += i) isPrime[j] = 0;
}
int dx[] = { 1, -1, 0, 0 };
int dy[] = { 0, 0, 1, -1 };
//bool valid(int r, int c) { if (r >= 0 && r < n && c >= 0 && c < m) return 1; return 0; }
const int  val = 111905; ll mod = 1000003;
ll n, a1, a2, a3;
int isDivby8(string s)
{
	string s, s1 = "", s2 = "", s3 = "";

	int sz = s.size();
	string t = s.substr(max(0, sz - 4));
	string lst3 = s.substr(max(0, sz - 3));

	int tr = atoi(lst3.c_str());
	if (tr % 8 == 0) { cout << "YES\n" << s << "\n"; return 0; }
	int tsz = t.size();
	if (tsz == 4) { s1 = t; s1.erase(1, 1); s2 = t; s2.erase(2, 1); s3 = t; s3.erase(3, 1); }
	else {
		if (tsz == 1) { s1 = t.substr(0, 1); s2 = "7"; s3 = "7"; }
		else if (tsz == 2) { s1 = t.substr(0, 1); s2 = t.substr(1, 1);; s3 = "7"; }
		else { s1 = t; s1.erase(0, 1); s2 = t; s2.erase(1, 1); s3 = t; s3.erase(2, 1); }
	}

	//cout <<"STRINGS  : " << s1 << "  " << s2 << "  " << s3;
	a1 = atoi(s1.c_str()); a2 = atoi(s2.c_str()); a3 = atoi(s3.c_str());

	if (a1 % 8 == 0) return a1;
	else if (a2 % 8 == 0) cout << "YES\n" << a2 << "\n"; else if (a3 % 8 == 0)   cout << "YES\n" << a3 << "\n"; else cout << "NO\n";

}
int main()
{
	ios::sync_with_stdio(false);
	cin >> s;

	//system("PAUSE");
	return 0;
}*/
#include <string>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
#include <deque>
#include <string>
#include <map>
#include <utility>
#include <set>
using namespace std;
#define ll  long long
#define x first
#define y second
#define N 1000005
#define MAXN 999999
//DIV 2 3 C

int max(int  a, int b) { if (a > b) return a; else return b; }
//int min(int  a, int b) { if (a < b) return a; else return b; }
ll min(ll  a, ll b) { if (a < b) return a; else return b; }
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return a * (b / gcd(a, b)); }
vector <bool> isPrime(N + 1, true);
string tos(int n) { stringstream ss; ss << n; return ss.str(); }
void seive() {
	isPrime[0] = 0, isPrime[1] = 0; for (ll i = 2; i*i <= N; i++) if (isPrime[i]) for (ll j = i * i; j <= N; j += i) isPrime[j] = 0;
}
int dx[] = { 1, -1, 0, 0 };
int dy[] = { 0, 0, 1, -1 };
//bool valid(int r, int c) { if (r >= 0 && r < n && c >= 0 && c < m) return 1; return 0; }
const int  val = 111905; ll mod = 1000003;
int n;  int xcnt, ycnt, xcols[10], xrows[10], yrows[10], ycols[10]; char a[6][6]; bool xwins, ywins;
int main()
{
	ios::sync_with_stdio(false);
	for (int i = 1; i <= 3;i++)
		for (int j = 1; j <= 3; j++)
		{
			cin >> a[i][j];

			if (a[i][j] == 'X') { xcnt++; xrows[i]++; xcols[j]++; if (xrows[i] == 3 || xcols[j] == 3) xwins++; }
			else if (a[i][j] == '0') { ycnt++; yrows[i]++; ycols[j]++; if (yrows[i] == 3 || ycols[j] == 3) ywins++; }
		}
	if (a[1][1] == 'X' && a[2][2] == 'X' && a[3][3] == 'X' )xwins++;
	if ( a[1][3] == 'X' && a[2][2] == 'X' && a[3][1] == 'X')xwins++;
	if (a[1][1] == '0' && a[2][2] == '0' && a[3][3] == '0' )ywins++;
	if (a[1][3] == '0' && a[2][2] == '0' && a[3][1] == '0') ywins++;

	int cnt = xcnt + ycnt;
	if ((xwins>=1 && ywins>=1) || xwins>1 || ywins>1) cout << "illegal";
	else if (xwins) cout << "the first player won";
	else if (ywins)	cout << "the second player won";
	else if (cnt == 9) cout << "draw";
	else { if ((cnt + 1) & 1) cout << "first"; else cout << "second"; }

	//system("PAUSE");
	return 0;
}
/*#include <string>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
#include <deque>
#include <string>
#include <map>
#include <utility>
#include <set>
using namespace std;
#define ll long long
#define x first
#define y second
#define N 1000005
#define MAXN 9999999999999
//DIV 2 570 C

int max(int  a, int b) { if (a > b) return a; else return b; }
//int min(int  a, int b) { if (a < b) return a; else return b; }
ll min(ll  a, ll b) { if (a < b) return a; else return b; }
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return a * (b / gcd(a, b)); }
vector <bool> isPrime(N + 1, true);
string tos(int n) { stringstream ss; ss << n; return ss.str(); }
void seive() {
	isPrime[0] = 0, isPrime[1] = 0; for (ll i = 2; i*i <= N; i++) if (isPrime[i]) for (ll j = i * i; j <= N; j += i) isPrime[j] = 0;
}
int dx[] = { 1, -1, 0, 0 };
int dy[] = { 0, 0, 1, -1 };
//bool valid(int r, int c) { if (r >= 0 && r < n && c >= 0 && c < m) return 1; return 0; }
const int  val =1<<19;
int n, m,s,e, ans = 1, a[val], b[val];
int main()
{
	string s1, s2,p1,p2;
	cin >> s1 >> s2;
	/*int sz = t.size();
	for (int i = 1; i < sz; i++)
	{
		p1 = t.substr(0, i); string p2 = t.substr(i, n-i);
		//cout << "part 1 : " << p1 << "  part2 : " << p2 << "\n";
		if (isSubSequence(s, p1) && isSubSequence(s,p2))ans++;
	}
	int i = 0, j = 0, beg = -1;
	while (j < s2.size()) {
		if (s1[i] == s2[j])if (beg = -1) {beg = j; a[s++] = beg;}
			i++;
			if (i == s1.size()) { b[e++] = j; i = 0; beg = -1; }
		j++;
	}
	for (int k = 0; k < e ; k++)
		cout << a[i] << " " << b[i] << "\n";
	for (int k = 0; k < e - 1; k++)
		ans *= b[k + 1] - a[k];

	cout << ans << "\n";

	system("PAUSE");
	return 0;
}*/
/*#include <string>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
#include <deque>
#include <string>
#include <map>
#include <utility>
#include <set>
using namespace std;
#define ll long long
#define x first
#define y second
#define N 1000005
#define MAXN 9999999999999
//DIV 2 570 C

int max(int  a, int b) { if (a > b) return a; else return b; }
//int min(int  a, int b) { if (a < b) return a; else return b; }
ll min(ll  a, ll b) { if (a < b) return a; else return b; }
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return a * (b / gcd(a, b)); }
vector <bool> isPrime(N + 1, true);
string tos(int n) { stringstream ss; ss << n; return ss.str(); }
void seive() {
	isPrime[0] = 0, isPrime[1] = 0; for (ll i = 2; i*i <= N; i++) if (isPrime[i]) for (ll j = i * i; j <= N; j += i) isPrime[j] = 0;
}
int dx[] = { 1, -1, 0, 0 };
int dy[] = { 0, 0, 1, -1 };
//bool valid(int r, int c) { if (r >= 0 && r < n && c >= 0 && c < m) return 1; return 0; }
const int  val = 1 << 19;
ll n,k, m; vector <ll> frst; vector <ll> sec; ll suma, sumb, mx; vector < pair<int, int>> v;
int main()
{
	cin >> n;
	//system("PAUSE");
	return 0;
}*/

#include <cstdlib>
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

char mp[4][4];

int check()
{
    int ans=0;
    for(int i=0;i<3;i++)
    {
        if(mp[i][0]==mp[i][1] && mp[i][1]==mp[i][2])
        {
            if(mp[i][0]=='X')
            {
                //cout<<i;
                if(ans==0)
                ans=1;
                else
                {
                    ans=3;
                    return ans;
                }
            }
            else if(mp[i][0]=='0')
            {
                if(ans==0)
                    ans=2;
                else
                {
                    ans=3;
                    return ans;
                }
            }
        }
        if(mp[0][i]==mp[1][i] && mp[1][i]==mp[2][i])
        {
            if(mp[0][i]=='X')
            {
                //cout<<i;
                if(ans==0)
                    ans=1;
                else
                {
                    ans=3;
                    return ans;
                }
            }
            else if(mp[0][i]=='0')
            {
                if(ans==0)
                    ans=2;
                else
                {
                    ans=3;
                    return ans;
                }
            }
        }
    }
    if((mp[0][0]==mp[1][1] && mp[1][1] == mp[2][2]) || (mp[0][2]==mp[1][1] && mp[1][1]==mp[2][0]))
    {
        if(mp[1][1]=='X')
        {
            if(ans==0)
                ans=1;
            else
            {
                ans=3;
                return ans;
            }
        }
        else if(mp[1][1]=='0')
        {
            if(ans==0)
                ans=2;
            else
            {
                ans=3;
                return ans;
            }
        }

    }
    return ans;
}

int main()
{
    bool f=false;
    int cX=0,c0=0;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            mp[i][j]=getchar();
            if(mp[i][j]=='X')cX++;
            if(mp[i][j]=='0')c0++;
            if(mp[i][j]=='.')f=true;
        }
        getchar();
    }
    int t=check();
    //cout<<t<<endl;
    if(t==3 || (t==1 && c0!=cX-1 && f==true) || (t==2 && cX!=c0 && f==true))cout<<"illegal"<<endl;
    else if(cX==c0 && cX!=5 && f==1)cout<<"first"<<endl;
    else if(cX==c0+1 && f==1)cout<<"second"<<endl;
    else if(abs(cX-c0)>1 || c0>cX)cout<<"illegal"<<endl;
    else
    {

        if(t==1)cout<<"the first player won"<<endl;
        else if(t==2)cout<<"the second player won"<<endl;
        else cout<<"draw"<<endl;
    }
    return 0;
}

# include <iostream>
using namespace std;
int main()
{
    char arr[3][3];
    int count_cross = 0, count_nought = 0, count_none = 0,first = 0, second = 0;
    for ( int i = 0; i < 3; i++ )
    {
        for ( int j = 0; j < 3; j++ )
        {
            cin>>arr[i][j];
            if ( arr[i][j] == 'X' )
                count_cross++;
            else if ( arr[i][j] == '0' )
                count_nought++;
            else
                count_none++;
        }
    }
    if ( ((count_cross - count_nought) != 0) && ((count_cross - count_nought) != 1) )
        cout<<"illegal";
    else
    {
        for ( int i = 0; i < 3; i++ )
        {
            if ( (arr[i][0] == arr[i][1]) && (arr[i][1] == arr[i][2]) )
            {
                if ( arr[i][0] == 'X' )
                    first++;
                else if ( arr[i][0] == '0' )
                    second++;
            }
            if ( (arr[0][i] == arr[1][i]) && (arr[1][i] == arr[2][i]) )
            {
                if ( arr[0][i] == 'X' )
                    first++;
                else if ( arr[0][i] == '0' )
                    second++;
            }
        }
        if ( (arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2]) )
        {
            if ( arr[1][1] == 'X' )
                    first++;
            else if ( arr[1][1] == '0' )
                second++;
        }
        if ( (arr[0][2] == arr[1][1]) && (arr[1][1] == arr[2][0]) )
        {
            if ( arr[1][1] == 'X' )
                    first++;
            else if ( arr[1][1] == '0' )
                second++;
        }
        if ( first > 0 )
        {
            if ( second == 0 )
                cout<<"the first player won";
            else
                cout<<"illegal";
        }
        else
        {
            if ( (second > 0) && (0 == (count_cross - count_nought)) )
                cout<<"the second player won";
            else if ( (second > 0) && (0 != (count_cross - count_nought)) )
                cout<<"illegal";
            else
            {
                if ( count_none == 0 )
                    cout<<"draw";
                else
                {
                    if ( (count_cross - count_nought) == 1 )
                        cout<<"second";
                    else
                        cout<<"first";
                }
            }
        }
    }
    return 0;
}

#include <iostream>

using namespace std;

string line[3];
int x, o;

bool win(char c)
{
    if (line[0][0]==line[1][1]==line[2][2]==c)
        return 1;
    if (line[0][2]==line[1][1]==line[2][0]==c)
        return 1;

    for (int i=0; i<3; i++)
    {
        int ok=1;
        for (int j=0; j<3; j++)
            if (line[i][j]!=c)
                ok=0;
        if (ok)
            return 1;
    }

    for (int j=0; j<3; j++)
    {
        int ok=1;
        for (int i=0; i<3; i++)
            if (line[i][j]!=c)
                ok=0;
        if (ok)
            return 1;
    }
    return 0;
}

int main()
{
    for (int i=0; i<3; i++)
        cin>>line[i];

    for (int i=0; i<3; i++)
        for (int j=0; j<3; j++)
            if (line[i][j]=='X')
                x++;
            else if (line[i][j]=='0')
                o++;

    if (win('X') && x==o+1)
    {
        cout<<"the first player won";
        return 0;
    }

    if (win('0') && x==o)
    {
        cout<<"the second player won";
        return 0;
    }

    if (x==5 && o==6 && !win('X') && !win('0'))
    {
        cout<<"draw";
        return 0;
    }

    if ((win('0') && x!=o)||(win('X') && x!=o+1)||(x>o+1))
    {
        cout<<"illegal";
        return 0;
    }

    if (x==o)
        cout<<"first";
    else
        cout<<"second";
    return 0;
}

#include <bits/stdc++.h>

#define LL long long int
#define ULL unsigned long long int

#define FOR(i,a,b) for(__int64 i=a;i<=b;i++)
#define REV_FOR(i,a,b) for(int i=a;i>=b;i--)
#define _cin  ios_base::sync_with_stdio(0); cin.tie(0);

#define clear(a) memset(a,0,sizeof(a))

#define MOD 1000000007
#define INF (1<<28)
#define mx = 10e9
#define mn = -1*mx

#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a<b?a:b)

#define pb push_back
#define pf pop_front()
#define p push
#define f first
#define s second

using namespace std;

main()
	{
	int x = 0,o = 0;
	char a[10][10];
	FOR(i,1,3)
		FOR(j,1,3)
			{
			cin>>a[i][j];
			if(a[i][j] == 'X')
				x++;
			if(a[i][j] == '0')
				o++;
			}
	if(x - o >= 2)
		printf("illegal");
	else if(o > x)
		printf("illegal");
	bool win_o = false, win_x = false;
	FOR(i,1,3)
		{
		FOR(j,1,3)
			{
			if(a[i][j] == 'X')
					if(a[i+1][j] == 'X' && a[i+2][j] == 'X' || a[i][j+1] == 'X' && a[i][j+1] == 'X' || a[i+1][j+1] == 'X' && a[i+2][j+2] == 'X')
							win_x = true;
			if(a[i][j] == '0')
				if(a[i+1][j] == '0' && a[i+2][j] == '0' || a[i][j+1] == '0' && a[i][j+1] == '0' || a[i+1][j+1] == '0' && a[i+2][j+2] == '0')
					win_o = true;
			}
		}
		if(win_x && win_o)
			printf("illegal");
		else if(x + o < 9)
			((x > o) ? printf("second") : printf("first"));
		else if (win_x)
			printf("the first player won");
		else if(win_o)
			printf("the second player won");
		else
			printf("draw");
	}

#include<iostream>
#include "stdlib.h"

using namespace std;

bool consulta(char letra,char ma[]){
  for(int a=0;a<9;a++){
    if(ma[a]==letra){
      return true;
    }
  }
  return false;
}

int main(){
  char a,b,c;
  char m[9];
  int pos=0;
  for(int cont=0;cont<3;cont++){
    cin>>a>>b>>c;
    m[pos]=a;
    m[pos+1]=b;
    m[pos+2]=c;
    pos=pos+3;
  }
  int compX=0;
  int compO=0;
  for(int co=0; co<9;co++){
    if(m[co]=='X')
      compX++;
    if(m[co]=='0')
      compO++;
  }
  if (compX-compO>1 and abs(compX-compO)!=0 or compO>compX){
    cout<<"illegal"<<endl;
    return 0;
  }
  char valor[1];
  int conta=0;
  if(m[0]==m[1] and m[1]==m[2] and m[2]==m[0] and m[0]!='.'){
    valor[0]=m[0];
    conta++;
  }
  if(m[3]==m[4] and m[4]==m[5] and m[5]==m[6] and m[3]!='.'){
    valor[0]=m[3];
    conta++;
  }
  if(m[6]==m[7] and m[7]==m[8] and m[8]==m[6] and m[6]!='.'){
    valor[0]=m[6];
    conta++;
  }
  if(m[0]==m[3] and m[3]==m[6] and m[6]==m[0] and m[0]!='.'){
    valor[0]=m[0];
    conta++;
  }
  if(m[1]==m[4] and m[4]==m[7] and m[7]==m[1] and m[1]!='.'){
    valor[0]=m[1];
    conta++;
  }
  if(m[2]==m[5] and m[5]==m[8] and m[8]==m[2] and m[2]!='.'){
    valor[0]=m[2];
    conta++;
  }
  if(m[0]==m[4] and m[4]==m[8] and m[8]==m[0] and m[0]!='.'){
    valor[0]=m[0];
    conta++;
  }

  if(conta>1 and consulta('.',m)){
    cout<<"illegal"<<endl;
    return 0;
  }
  char equis[1],cero[1];
  equis[0]='X';
  cero[0]='0';
  if(valor[0]==equis[0]){
    cout<<"the first player won"<<endl;
    return 0;
  }
  if(valor[0]==cero[0]){
    cout<<"the second player won"<<endl;
    return 0;
  }
  if(compX>compO or (compX-compO)%2==1){
    cout<<"second"<<endl;
    return 0;
  }
  if(compO>compX or (compX-compO)%2==0){
    cout<<"first"<<endl;
    return 0;
  }
  return 0;
}

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <climits>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>

#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pi acos(-1.0)
#define EPS 1e-9

using namespace std;

int main()
{
	int X=0,O=0;
	vector<string> v(3);for(int i=0;i<3;i++)cin>>v[i],X+=count(all(v[i]),'X'),O+=count(all(v[i]),'0');
	if(X-1!=O && X!=O){puts("illegal\n");return 0;}
	if(X+O==9){
		if(X!=5){puts("illegal\n");return 0;}
	}
	for(int i=0;i<3;i++){
		if(v[i]=="OOO"){
			if(O!=X){puts("illegal\n");return 0;}
			puts("the second player won\n");return 0;
		}
		else if(v[i]=="XXX"){
			if(X-1!=O){puts("illegal\n");return 0;}
			puts("the first player won\n");return 0;
		}
	}
	for(int i=0;i<3;i++){
		string s;
		for(int j=0;j<3;j++)s+=v[j][i];
		if(s=="OOO"){
			if(O!=X){puts("illegal\n");return 0;}
			puts("the second player won\n");return 0;
		}
		else if(s=="XXX"){
			if(X-1!=O){puts("illegal\n");return 0;}
			puts("the first player won\n");return 0;
		}
	}
	string s;int i=0,j=0;
	while(i<3)s+=v[i++][j++];
	if(s=="OOO"){
		if(O!=X){puts("illegal\n");return 0;}
		puts("the second player won\n");return 0;
	}
	else if(s=="XXX"){
		if(X-1!=O){puts("illegal\n");return 0;}
		puts("the first player won\n");return 0;
	}
	s.clear();i=0;j=2;
	while(i<3)s+=v[i++][j--];
	if(s=="OOO"){
		if(O!=X){puts("illegal\n");return 0;}
		puts("the second player won\n");return 0;
	}
	else if(s=="XXX"){
		if(X-1!=O){puts("illegal\n");return 0;}
		puts("the first player won\n");return 0;
	}
	if(X+O==9){puts("draw\n");return 0;}
	if(X-1==O)puts("second\n");
	else puts("first\n");
	return 0;
}

#include <iostream>
#include <cstdlib>
#include <cstdio>

/*
 *codeforce round3 C
 *date:07/09/2018
 */

using namespace std;

void x_o(int& a, int& b, char c)
{
    if(c == 'X')
    {
        ++a;
    }
    else if(c == '0')
    {
        ++b;
    }
}

int main()
{
    int xcount = 0, ocount = 0, sign = 0, temp = 0, won = 2;
    char* rectangle[3];
    for(int i = 0; i < 3; ++i)
    {
        rectangle[i] = new char[4];
        for(int j = 0; j < 3; ++j)
        {
            scanf("%c", rectangle[i] + j);
            while(rectangle[i][j] == ' ' || rectangle[i][j] == '\n')
            {
                scanf("%c", rectangle[i] + j);
            }
            x_o(xcount, ocount, rectangle[i][j]);
        }
    }
    if(xcount - ocount > 1 || ocount - xcount > 0)
    {
        printf("illegal\n");
        return 0;
    }
    for(int i = 0; i < 3; ++i)
    {
        if(rectangle[i][0] == rectangle[i][1] && rectangle[i][1] == rectangle[i][2] && (rectangle[i][0] == '0' || rectangle[i][0] == 'X'))
        {
            if(rectangle[i][0] == '0')
            {
                won = 0;
            }
            else
            {
                won = 1;
            }
            ++sign;
        }
    }
    if(sign > 1)
    {
        printf("illegal\n");
        return 0;
    }
    else if(sign == 1)
    {
        temp = 1;
    }
    for(int j = 0; j < 3; ++j)
    {
        if(rectangle[0][j] == rectangle[1][j] && rectangle[1][j] == rectangle[2][j] && (rectangle[0][j] == '0' || rectangle[0][j] == 'X'))
        {
            if(temp == 1)
            {
                --temp;
            }
            else
            {
                if(rectangle[0][j] == '0')
                {
                    won = 0;
                }
                else
                {
                    won = 1;
                }
                ++sign;
            }
        }
    }
    temp = 0;
    if(rectangle[0][0] == rectangle[1][1] && rectangle[2][2] == rectangle[1][1] && (rectangle[0][0] == '0' || rectangle[0][0] == 'X'))
    {
        ++sign;
        temp = 1;
    }
    if(rectangle[2][0] == rectangle[1][1] && rectangle[0][2] == rectangle[1][1] && (rectangle[1][1] == '0' || rectangle[1][1] == 'X'))
    {
        if(temp != 1)
        {
            ++sign;
        }
    }
    if(sign == 1)
    {
        if(xcount - ocount == 1)
        {
            if(won == 0)
            {
                printf("illegal\n");
                return 0;
            }
            else
            {
                printf("the first player won\n");
            }
        }
        else
        {
            if(won == 1)
            {
                printf("illegal\n");
                return 0;
            }
            printf("the second player won\n");
        }
    }
    else if(sign == 0)
    {
        if(xcount + ocount == 9)
        {
            printf("draw\n");
            return 0;
        }
        if(xcount > ocount)
        {
            printf("second\n");
        }
        else
        {
            printf("first\n");
        }
    }
    else
    {
        printf("illegal\n");
    }
    return 0;
}

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <iostream>

#define SIZE 3
using namespace std;

char game_board[SIZE][SIZE];
int X_count = 0;
int O_count = 0;
int dot_count = 0;

int main()
{
    bool x_won = false, y_won = false;
    for(int i=0; i<SIZE; i++)
    {
        for(int j=0; j<SIZE; j++)
        {
            scanf("%c",&game_board[i][j]);
            if(game_board[i][j] == 'X')
                X_count++;
            else if(game_board[i][j] == '0')
                O_count++;
            else
                dot_count++;
            //cout<<X_count<<" "<<O_count<<endl;
        }
        getchar();
    }

    if (X_count < O_count || X_count > (O_count + 1))
    {
        printf("illegal\n");
        return 0;
    }
    else if(dot_count == 0)
    {
        if (X_count <= O_count || X_count > (O_count + 1))
        {
            printf("illegal\n");
            return 0;
        }
    }

    if ((game_board[0][0] == game_board[1][1] && game_board[1][1] == game_board[2][2]))
    {
        if(game_board[1][1] == 'X')
            x_won = true;
        else if(game_board[1][1] == '0')
            y_won = true;
    }

    if((game_board[0][2] == game_board[1][1] && game_board[1][1] == game_board[2][0]))
    {
        if(game_board[1][1] == 'X')
            x_won = true;
        else if(game_board[1][1] == '0')
            y_won = true;
    }

    for(int i=0; i<SIZE; i++)
    {
        if((game_board[0][i] == game_board[1][i] && game_board[0][i] == game_board [2][i]))
        {
            if(game_board[0][i] == 'X')
                x_won = true;
            else if(game_board[0][i] == '0')
                y_won = true;
        }
        if((game_board[i][0] == game_board[i][1] && game_board[i][0] == game_board[i][2]))
        {
            if(game_board[i][0] == 'X')
                x_won = true;
            else if(game_board[1][0] == '0')
                y_won = true;
        }
    }

    if (x_won == y_won && x_won ==true)
        printf("illegal\n");
    else if(x_won)
        printf("the first player won\n");
    else if(y_won)
        printf("the second player won\n");
    else if (X_count > O_count)
        printf("second\n");
    else
        printf("first\n");
    return 0;
}

#include <iostream>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <map>
#include <unordered_map>
#include <stack>
#include <algorithm>
#include <stdio.h>
#include <cstring>					//  For memset
#include <sstream>

using namespace std;

#define lop(i,n) for(int i = 0 ; i < n ;i++)
#define VI	vector<int>
#define VLL	vector<ll>
#define VPI	vector<pair<int , int>>
#define VII	vector<VI>
#define all(v)	v.begin() , v.end()
#define SORT(v)	sort(all(v))
#define ll	long long
#define ull unsigned long long
#define scni(x)	scanf("%d",&x)				//  int
#define scnll(x)	scanf("%lld",&x)			//  ll
#define scnd(x)	scanf("%lf",&x)			//  double
#define prnti(x)	printf("%d\n",x)				//  int
#define prntll(x)	printf("%lld",x)				//  int
#define prntd(x)	printf("%.9f",x)				//  int
#define sz(x)  x.size()
#define SPC  <<" "<<
#define INF int(1e9)
#define pp push_back
#define P pair<int , int>
#define mp make_pair

const double EPS = 1e-9;
const double PI = 3.14159265359;

//  O( n * log(log(n)) )
vector<ll> primes;
void GeneratingPrimes(int top){
	vector<bool> p (top+1 , 1);
//	vector<ll> primes;
	p[0] = p[1] = 0;

	for(int i = 2; i <= sqrt(top) ; i++){
		if(!p[i])continue;
		if(p[i]){
			for(int j = i*i ; j <= top ; j+=i){
				p[j]=0;
			}
		}
	}
	lop(i , top+1)
		if(p[i])primes.push_back(i);
//	return primes;
}

ll gcd(ll a ,ll b){return (b==0)?a : gcd(b , a%b);}
ll lcm(ll a, ll b){return (a * b) / gcd(a , b);}

void EXTgcd(ll a , ll b , int & x0 , int & y0){
	if(b==0){ x0 =1 , y0 = 0; return; }
	EXTgcd( b , a%b , x0 , y0 );
	ll x1 = y0;
	ll y1 = x0 - (a/b) * y0;
	x0 = x1;
	y0 = y1;
}

ull power(ull x, ull y)
{
    ll res = 1;
//    x = x % p;
    while (y > 0)
    {
        if (y & 1) res = (res*x);
        y = y>>1;
		x = (x*x);
    }
    return res;
}

double dist(double a , double b , double x, double y){ return sqrt( pow( a-x ,2) + pow( b-y ,2) ); }

double dist3(double a , double b , double c , double x , double y ,double z ){ return sqrt( pow( a-x ,2) + pow( b-y ,2) + pow( c-z ,2)); }

int xadd[9]= { 1 , -1 , 0 , 1 , -1 , 0 , 1 , -1 , 0 };
int yadd[9]= { 1 , -1 , 0 ,-1 ,  0 , 1 , 0 ,  1, -1 };

#define no  { cout<<"NO\n"; }
#define yes { cout<<"YES\n";}
#define _1 { cout<<"-1"; return 0;}

ll const N = 10007;
ll mod = 1e9 + 7;
///////////////////////////////////////////////////////////////////

#define d "draw"
#define f  { cout << "first"			; return 0;}
#define s  { cout << "second"			; return 0;}
#define fw { cout << "the first player won"	; return 0;}
#define sw { cout << "the seconf player won"; return 0;}
#define il { cout << "illegal"			; return 0;}

int rs[3];
int cs[3];
int di[2];
bool won[2];

int main(){

	vector<string> v(3);
	lop(i , 3)cin >> v[i];

	int x =0,  o=0;
	lop(i , 3)lop(j , 3){
		if(v[i][j] == 'X')x++;
		else if(v[i][j] == '0')o++;
	}

	if(o > x || x - o > 1)il;
	lop(i , 3)lop(j , 3){ if(v[i][j]=='X')rs[j]++; else if(v[i][j]=='0')rs[j]--; }
	lop(j , 3)lop(i , 3){ if(v[i][j]=='X')cs[i]++; else if(v[i][j]=='0')cs[i]--; }
	lop(i , 3)if(v[i][i] == 'X')di[0]++; else if(v[i][i]=='0')di[0]--;
	lop(i , 3)if(v[i][2-i] == 'X')di[1]++; else if(v[i][2-i]=='0')di[1]--;

	lop(i , 3)if(rs[i]==3 || cs[i]==3)won[0]=1;
	lop(i , 3)if(rs[i]==-3 || cs[i]==-3)won[1]=1;
	if(di[0]==3 || di[1]==3)won[0]=1;
	if(di[0]==-3 || di[1]==-3)won[1]=1;

	if(won[0] && won[1])il;
	if(won[1] && x > o)il;
	if(won[0] && x == o)il;
	if(won[0])fw;
	if(won[1])sw;
	if(x+o == 9)d;
	if(x == o){f;}
	else {s;}

return 0;}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef  long long int ll;
typedef unsigned long long int ull;
typedef  double ld;
typedef tree <int,null_type,less <int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#define rep(g, i, n) for (ll g = i; g < n; g++)
#define rev(g, n, i) for (ll g = n - 1; g >= i; g--)
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(v) v.begin(), v.end()
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define um unordered_multiset
#define us unordered_set
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define ve vector
#define cc continue
#define bb break
#define rem 1000000007
//str.erase(index,length of chars to be erased);
void sieve(bool prime[])
{
    ll n=1000000;
    prime[0]=false;
    prime[1]=false;
    for (ll p=2;p*p<=n;p++)
        if (prime[p]==true)
        {
            for (ll i=p*p;i<=n;i+=p)
            prime[i]=false;
        }
}
ll acc (vector <ll> v)
{
    ll ans=0;
    rep (i,0,v.size())
        ans+=v[i];
    return ans;
}
void dfs(int node, ve<ll> adj[], ve<ll> visited)
{
    visited[node] = true;
    // do stuff
    for (int i = 0; i < (int)adj[node].size(); i += 1)
    {
        if (!visited[adj[node][i]])
            dfs(adj[node][i], adj, visited);
    }
}
ll logn(ll n)
{ ll temp=0;
    while(n>=2)
    {
        temp++;
        n/=2;
    }
    return temp;
}
ll power(ll x)
{ll temp=1;
    while(x>0)
    {
        temp*=2;
        x--;
    }
    return temp;
}
ll sod(ll n)
{
    ll temp=0;
    while(n>0)
    {
        ll z=n%10;
        temp+=z;
        n/=10;
    }
    return temp;
}
ll get(ll x)
{
 ll ans=0;
 for(ll i=2;i*i*i<=x;++i)
 {
     ans+=(x/(i*i*i));}
 return ans;
}
ll mod(ll x)
{
    if(x>=0)return x;
    else return -x;

}
void solve()
{
char c[3][3];
ll x=0,o=0,xc=0,oc=0,d=0;
rep(i,0,3)
{
    rep(j,0,3)
    {
    cin>>c[i][j];
    }
}
rep(i,0,3)
{
    rep(j,0,3)
    {
    if(c[i][j]=='0')o++;
    if(c[i][j]=='X')x++;
    if(c[i][j]=='.')d++;
   if(j==0){if(c[i][j]==c[i][j+1]&&c[i][j+1]==c[i][j+2]&&c[i][j]=='X')xc++;
        if(c[i][j]==c[i][j+1]&&c[i][j+1]==c[i][j+2]&&c[i][j]=='0')oc++;
    }
     if(i==0){if(c[i][j]==c[i+1][j]&&c[i+1][j]==c[i+2][j]&&c[i][j]=='X')xc++;
        if(c[i][j]==c[i+1][j]&&c[i+1][j]==c[i+2][j]&&c[i][j]=='0')oc++;
    }
    }

}
if(c[0][0]==c[1][1]&&c[1][1]==c[2][2]&&c[2][2]=='X')xc++;
if(c[0][0]==c[1][1]&&c[1][1]==c[2][2]&&c[2][2]=='0')oc++;
if(c[0][2]==c[1][1]&&c[1][1]==c[2][0]&&c[2][0]=='X')xc++;
if(c[0][2]==c[1][1]&&c[1][1]==c[2][0]&&c[2][0]=='0')oc++;
//cout<<x<<" "<<o<<" "<<xc<<" "<<oc<<" "<<d<<endl;
 if((x-o)!=1&&(x-o)!=0){cout<<"illegal";return;}
 if(xc>=1&&oc==0){cout<<"the first player won";return;}
 if(xc==0&&oc>=1){cout<<"the second player won";return;}
 if(d!=0&&x>o&&xc==0&&oc==0){cout<<"second";return;}
 if(d!=0&&x==o&&xc==0&&oc==0){cout<<"first";return;}
 if(d==0&&x==o&&xc==0&&oc==0){cout<<"draw";return;}

    cout<<"illegal";
}
int main()
{

  solve();

   return 0;
}

/*	THE NAME OF ALLAH	*/
/*
                                       __
   |   |        --          |        /    \
  | | | |      /  |      __ |       |      |
  |  |  |     /---      |   |      |        |
  |     |    |         |    |       |      |
  |     |    |____      |__ |        \ __ /

	   	      _       _
        	/   \   /   \
           /     \ /     \
          |       |       |
           |             |             \
 |\ _ _ _ _ |           |  _ _ _ _ _ _ _\
 |/          |         |                /
              |       |                /
               |     |
      _ _ _     |   |             _ _
    /            | |            /     \
   /              |            |       |
  /                             \     /
 |                               \
 |                                \
 |                                 \
  \                           /     \
   \                         |       |
    \ _ _ _                   \ _ _ /

*/

#include<iostream>
#include<fstream>
#include<algorithm>
#include<set>
#include<string.h>
#include<map>
#include<list>
#include<iomanip>
#include <cstdio>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>
using namespace std;

#define all(v)			((v).begin(),(v).end())
#define sz(v)			((ll)(v).size())
#define clr(v,d)		memset((v),(d),sizeof(v))
#define rep(i,v)		for(ll i = 0 ; i < (ll) sz(v) ; i++ )
#define lp(i,n)			for(ll i = 0 ; i < (ll) n; i++ )
#define lpu(i,j,n)		for(ll i = (j) ; i < (ll) n ; i++ )
#define lpd(i,j,n)		for(ll i = (j) ; i >= (ll) n ; i-- )
#define MP(a,b)  make_pair(a,b)

typedef long long ll;
typedef unsigned long long ull;
typedef double dd;
typedef pair < ll , ll > p2 ;
typedef vector < vector < ll > > vv ;
typedef vector < ll > vll ;

const ll oo = 1e18 ;
ll arrx[10] ;
ll arro[10] ;
//pair < p2 , ll > arr[100010] ;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//while(1)
	//{
		//freopen("input.txt" , "r" , stdin) ;
		//freopen("output.txt" , "w" , stdout) ;
		char s[4][4] ;
		ll cx = 0 , co = 0 ;
		lp(i , 3)
		{
			lp( j , 3)
			{
				cin >> s[i][j] ;
				if(s[i][j] == 'X')
					cx++;
				else if(s[i][j] == '0')
					co++;
			}
		}
		if(co + cx == 9)
		{
			cout << "draw\n" ;
			return 0 ;
		}
		if((s[0][0] == s[0][1] && s[0][1] == s[0][2] && s[0][0] != '.')
		||(s[1][0] == s[1][1] && s[1][1] == s[1][2] && s[1][0] != '.')
		||(s[2][0] == s[2][1] && s[2][1] == s[2][2] && s[2][0] != '.')
		||(s[0][0] == s[1][0] && s[1][0] == s[2][0] && s[0][0] != '.')
		||(s[0][1] == s[1][1] && s[1][1] == s[2][1] && s[0][1] != '.')
		||(s[0][2] == s[1][2] && s[1][2] == s[2][2] && s[0][2] != '.')
		||(s[0][0] == s[1][1] && s[1][1] == s[2][2] && s[0][0] != '.')
		||(s[0][2] == s[1][1] && s[1][1] == s[2][0] && s[0][2] != '.'))
		{
				cout << "illegal\n" ;
			return 0 ;
		}
		if(cx > co)
		{
			cout << "second\n" ;
			return 0 ;
		}
		else if(cx <= co)
		{
			cout << "first\n" ;
			return 0 ;
		}
		lp(i , 3)
		{
			lp( j , 3)
			{
				if(s[i][j] == 'X')
					arrx[i]++;
				else if(s[i][j] == '0')
					arro[i]++;
			}
		}
		ll p = 3 ;
		lp(i , 3)
		{
			lp( j , 3)
			{
				if(s[j][i] == 'X')
					arrx[p]++;
				else if(s[j][i] == '0')
					arro[p]++;
			}
			p++;
		}
		lp(i , 3)
		{
			lp( j , 3)
			{
				if(i == j)
				{
					if(s[i][j] == 'X')
						arrx[p]++;
					else if(s[i][j] == '0')
						arro[p]++;
				}
			}
		}
		p++;
		lp(i , 3)
		{
			lp( j , 3)
			{
				if(i == 2 - j)
				{
					if(s[i][j] == 'X')
						arrx[p]++;
					else if(s[i][j] == '0')
						arro[p]++;
				}
			}
		}
		lp(i,8)
		{
			if(arrx[i] == 2 && arro[i] == 0 && cx < co)
			{
				cout << "the first player won\n";
				return 0 ;
			}
			else if(arrx[i] == 0 && arro[i] == 2 && cx > co)
			{
				cout << "the second player won\n";
				return 0 ;
			}
		}
	//}
	return 0 ;
}

#include <bits/stdc++.h>
using namespace std;
#define HAZEM ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0);
#define ull unsigned long long
#define ll long long
#define u unsigned
#define modulo 1000000007
#define MAXLL 1e18
#define MINLL -1e18
#define MAX 1e9
#define MIN -1e9
#define setDP(arr) memset(arr,-1,sizeof(arr))
#define setZero(arr) memset(arr,0,sizeof(arr))
#define endl '\n'
#define Rep(i,n,inc) for(int i=0;i<n;i+=inc)

typedef long long LL;

bool visited[3][3];
char arr[3][3];
int dfsHorizontal(int i,int j,char c)
{
    if(i>=3 || i<0 || j>=3 || j<0 || arr[i][j]!=c || arr[i][j]=='.' || visited[i][j])
    {
        return 0;
    }
    visited[i][j]=true;
    return max(dfsHorizontal(i+1,j,c)+1,dfsHorizontal(i-1,j,c)+1);
}
int dfsDiagonalRight(int i,int j,char c)
{
   if(i>=3 || i<0 || j>=3 || j<0 || arr[i][j]!=c || arr[i][j]=='.' || visited[i][j])
    {
        return 0;
    }
    visited[i][j]=true;
    return max(dfsDiagonalRight(i+1,j+1,c)+1,dfsDiagonalRight(i-1,j-1,c)+1);

}
int dfsDiagonalLeft(int i,int j,char c)
{
   if(i>=3 || i<0 || j>=3 || j<0 || arr[i][j]!=c || arr[i][j]=='.' || visited[i][j])
    {
        return 0;
    }
    visited[i][j]=true;
    return max(dfsDiagonalLeft(i-1,j+1,c)+1,dfsDiagonalLeft(i+1,j-1,c)+1);

}
int dfsVertical(int i,int j,char c)
{
   if(i>=3 || i<0 || j>=3 || j<0 || arr[i][j]!=c || arr[i][j]=='.' || visited[i][j])
    {
        return 0;
    }
    visited[i][j]=true;
    return max(dfsVertical(i,j+1,c)+1,dfsVertical(i,j-1,c)+1);
}

int main()
{
    HAZEM;
    int X=0,O=0;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='X')
            {
                X++;
            }
            else if(arr[i][j]=='0')
            {
                O++;
            }
        }
    }

    bool P1=false,P2=false;
    for(int i=0;i<3;i++)
    {

        for(int j=0;j<3;j++)
        {
            if(!visited[i][j] && arr[i][j]!='.')
            {
                int ret=dfsVertical(i,j,arr[i][j]);
                visited[i][j]=false;
                ret=max(ret,dfsHorizontal(i,j,arr[i][j]));
                visited[i][j]=false;
                ret=max(ret,dfsDiagonalLeft(i,j,arr[i][j]));
                visited[i][j]=false;
                ret=max(ret,dfsDiagonalRight(i,j,arr[i][j]));
                //cout<<ret<<" "<<i<<" "<<j<<" "<<arr[i][j]<<endl;
                if(ret>=3 && arr[i][j]=='X')
                {
                    P1=true;
                }
                else if(ret>=3 && arr[i][j]=='0')
                {
                    P2=true;
                }
            }
        }
    }
    //cout<<P1<<" "<<P2<<endl;
    if(abs(X-O)>1 || O>X || (P1==true && P2==true) || (P2 && X>O) || (P1 && X==O))
    {
        cout<<"illegal"<<endl;
        return 0;
    }
    if(P1)
    {
                    cout<<"the first player won"<<endl;
                    return 0;
    }
    else if(P2)
    {
                    cout<<"the second player won"<<endl;
                    return 0;

    }
    if(X==5 && O==4)
    {
        cout<<"draw"<<endl;
        return 0;
    }
    if(X==O)
    {
        cout<<"first"<<endl;
    }
    else
    {
        cout<<"second"<<endl;
    }
    return 0;

}

#include<iostream>
#include<set>
#include<algorithm>
#define ll long long
#include<bits/stdc++.h>
#define inf 1000000
using namespace std;
string tic[3];
int main()
{
	int x=0;
	int o=0;
	int d=0;
	int win=0;
	int win1=0;
	int win2=0;
	for(int i=0;i<3;i++)
	{
			cin>>tic[i];
			for(int j=0;j<3;j++)
			if(tic[i][j]=='X')
			x++;
			else if(tic[i][j]=='0')
			o++;
			else
			d++;
	}

		for(int i=0;i<3;i++)
		{
			if(tic[i]=="XXX")
			{
				win1=1;
				break;
			}
		}

		for(int i=0;i<3;i++)
		{
			if(tic[i]=="000")
			{
				win2=1;
				break;
			}
		}
		for(int i=0;i<3;i++)
		{
			int c=0;
			for(int j=0;j<3;j++)
			{
				if(tic[j][i]=='X')
				c++;
			}
			if(c==3)
			{
				win1=1;
				break;
			}
		}
			for(int i=0;i<3;i++)
		{
			int c=0;
			for(int j=0;j<3;j++)
			{
				if(tic[j][i]=='0')
				c++;
			}
			if(c==3)
			{
				win2=1;
				break;
			}
		}
		int y=0;
		for(int i=0;i<3;i++)
		{
			if(tic[i][i]=='X')
			{
				y++;
			}
		}
		if(y==3)
		win1=1;
		y=0;
		if(tic[0][2]=='X'&&tic[1][1]=='X'&&tic[0][2]=='X')
		win1=1;
			if(tic[0][2]=='0'&&tic[1][1]=='0'&&tic[0][2]=='0')
		win2=1;
		for(int i=0;i<3;i++)
		{
			if(tic[i][i]=='0')
			{
				y++;
			}
		}
		if(y==3)
		win2=1;

		if((win1==0&&win2==0)&&d==0)
{
cout<<"draw";
return 0;
}
if(o>x||(win1==1&&win2==1)||(win2==1&&x>o)||(win1==1&&o>=x)||(x-o>1))
{
cout<<"illegal";
return 0;
}
		if(win1==1)
{
	cout<<"the first player won";
	return 0;
}
if(win2==1)
{
cout<<"the second player won";
return 0;
}
if(d>0)
{
	if(x>o)
	{
		cout<<"second";
		return 0;
	}
	else if(x==o)
	{
		cout<<"first";
		return 0;
	}

}

	return 0;
}

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define LTOR(i,a,n) for (int i=a;i<n;i++)
#define RTOL(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define Spause system("pause");
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
const ll mod = 1000000007;
ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1){ if (b & 1)res = res*a%mod; a = a*a%mod; }return res; }
// head
char p[3][3];
int won(){
	int xp(0), op(0);
	int X(0), O(0);
	int win(0);
	LTOR(j, 0, 3){
		LTOR(i, 0, 3){
			if (p[i][j] == 'X')xp++;
			if (p[i][j] == 'O')op++;
		}
		if (xp == 3){ X++; win++; }
		if (op == 3){ O++; win++; }
		xp = 0; op = 0;
	}
	LTOR(i, 0, 3){
		LTOR(j, 0, 3){
			if (p[i][j] == 'X')xp++;
			if (p[i][j] == 'O')op++;
		}
		if (xp == 3){ X++; win++; }
		if (op == 3){ O++; win++; }
		xp = 0; op = 0;
	}
	LTOR(i, 0, 3){
		if (p[i][i] == 'X')xp++;
		if (p[i][i] == 'O')op++;
	}
	if (xp == 3){ X++; win++; }
	if (op == 3){ O++; win++; }
	RTOL(i, 0, 3){
		if (p[i][i] == 'X')xp++;
		if (p[i][i] == 'O')op++;
	}
	if (xp == 3){ X++; win++; }
	if (op == 3){ O++; win++; }

	xp = 0; op = 0;
	if (win > 1)return 3;
	if (X == 1)return 1;
	if (O == 1)return 2;
}
int main()
{
	int x(0), o(0);
	bool f = 0;
	LTOR(i, 0, 3){
		LTOR(j, 0, 3){
			cin >> p[i][j];
			if (p[i][j] == 'X')x++;
			if (p[i][j] == '0')o++;
			if (p[i][j] == '.')f = 1;
		}
	}
	if (abs(x - o) > 1 || o>x){
		cout << "illegal"; return 0;
	}
	if (f != 1){
		int win = won();
		if (win == 1)cout << "the first player won";
		if (win == 2)cout << "the second player won";
		if (win == 3)cout << "illegal";
		if (win == 0)cout << "draw";
	}
	else {
		if (x > o)cout << "second";
		else if(o==x)cout << "first";
	}
	return 0;
}

#include <iostream>
using namespace std;

int ary[3][3];
bool has_won(int val) {
    for (int i = 0; i != 3; i++) {
        if ((ary[i][0] == ary[i][1]) && (ary[i][1]== ary[i][2]) && (ary[i][2] == val))
            return true;
        if ((ary[0][i] == ary[1][i]) && (ary[1][i]== ary[2][i]) && (ary[2][i] == val))
            return true;
    }
    if ((ary[0][0] == ary[1][1]) && (ary[1][1]== ary[2][2]) && (ary[2][2] == val))
        return true;
    if ((ary[2][0] == ary[1][1]) && (ary[1][1]== ary[0][2]) && (ary[0][2] == val))
        return true;
    return false;
}

main () {
    char a,b,c;
    int count_p1 = 0;
    int count_p2 = 0;
    for (int i = 0; i != 3; i++) {
        cin >> a >> b >> c;
        a=='.'?ary[i][0]=0:a=='X'?ary[i][0]=1,count_p1++:(ary[i][0]=2,count_p2++);
        b=='.'?ary[i][1]=0:b=='X'?ary[i][1]=1,count_p1++:(ary[i][1]=2,count_p2++);
        c=='.'?ary[i][2]=0:c=='X'?ary[i][2]=1,count_p1++:(ary[i][2]=2,count_p2++);
    }
    bool p1 = has_won(1);
    bool p2 = has_won(2);
    //cout << count_p1 << " " << count_p2 << '\n';
    //if ((p1 && p2) || (count_p2 > count_p1) || (count_p2 - count_p1 > 1) || (p2 && (count_p1 > count_p2))) {
        //printf("illegal\n");
        //return 0;
    //}
    //if (p1 || p2) {
        //if (count_p1 == count_p2) {
            //printf("the second player won\n");
            //return 0;
        //}
        //else {
            //printf("the first player won\n");
            //return 0;
        //}
    //}
    //if (count_p1 + count_p2 == 9) {
        //printf("draw\n");
        //return 0;
    //}
    //if (count_p1 == count_p2) {
        //printf("first\n");
        //return 0;
    //}
    //printf("second\n");
    if (count_p2 > count_p1) {
        printf("illegal\n");
        return 0;
    }
    if (count_p2 == count_p1) {
        if (p1) {
            printf("illegal\n");
            return 0;
        }
        if (p2) {
            printf("the second player won\n");
            return 0;
        }
        if (count_p2 + count_p1 == 9) {
            printf("draw\n");
            return 0;
        }
        printf("first\n");
        return 0;

    }
    if (count_p1 > count_p2) {
        if (p2) {
            printf("illegal\n");
            return 0;
        }
        if (p1) {
            printf("the first player won\n");
            return 0;
        }
        if (count_p2 + count_p1 == 9) {
            printf("draw\n");
            return 0;
        }
        printf("second\n");
        return 0;

    }

    return 0;
}

#include <iostream>
using namespace std;
int main () {
char lenta[1000][1000];
int n=3;
int pirmaeilutex=0;
int antraeilutex=0;
int treciaeilutex=0;
int pirmasstulpelisx=0;
int antrasstulpelisx=0;
int treciasstulpelisx=0;
int istrizainekairedesinex=0;
int istrizainedesinekairex=0;

int pirmaeiluteo=0;
int antraeiluteo=0;
int treciaeiluteo=0;
int pirmasstulpeliso=0;
int antrasstulpeliso=0;
int treciasstulpeliso=0;
int istrizainekairedesineo=0;
int istrizainedesinekaireo=0;
for (int i=0;i<n;i++){
    for (int j=0;j<n;j++){
        cin>>lenta[i][j];
    }
}

for (int i=0;i<n;i++){
    if (lenta[0][i]=='X'){
        pirmaeilutex++;
    }
     if (lenta[1][i]=='X'){
        antraeilutex++;
    }
    if (lenta[2][i]=='X'){
        treciaeilutex++;
    }
    if (lenta[i][0]=='X'){
        pirmasstulpelisx++;
    }
    if (lenta[i][1]=='X'){
        antrasstulpelisx++;
    }
    if (lenta[i][2]=='X'){
        treciasstulpelisx++;
    }

}
if (lenta[0][0]=='X'&&lenta[1][1]=='X'&&lenta[2][2]=='X'){
    istrizainekairedesinex++;
}
if (lenta[2][2]=='X'&&lenta[1][1]=='X'&&lenta[0][0]=='X'){
    istrizainedesinekairex++;
}
/////////////////////////////////////
for (int i=0;i<n;i++){
    if (lenta[0][i]=='0'){
        pirmaeiluteo++;
    }
     if (lenta[1][i]=='0'){
        antraeiluteo++;
    }
    if (lenta[2][i]=='0'){
        treciaeiluteo++;
    }
    if (lenta[i][0]=='0'){
        pirmasstulpeliso++;
    }
    if (lenta[i][1]=='0'){
        antrasstulpeliso++;
    }
    if (lenta[i][2]=='0'){
        treciasstulpeliso++;
    }

}
if (lenta[0][0]=='0'&&lenta[1][1]=='0'&&lenta[2][2]=='0'){
    istrizainekairedesineo++;
}
if (lenta[2][2]=='0'&&lenta[1][1]=='0'&&lenta[0][0]=='0'){
    istrizainedesinekaireo++;
}
//////////////////////////////
int tusciukiekis=0;
int xkiekis=0;
int okiekis=0;
for (int i=0;i<n;i++){
    for (int j=0;j<n;j++){
        if (lenta[i][j]=='.'){
            tusciukiekis++;
        }

    }
}
for (int i=0;i<n;i++){
    for (int j=0;j<n;j++){
      if (lenta[i][j]=='X'){
            xkiekis++;
        }
    }}
  for (int i=0;i<n;i++){
    for (int j=0;j<n;j++){
      if (lenta[i][j]=='0'){
            okiekis++;
        }
    }}
    int skirtumasxo;
int skirtumasox;
if (xkiekis>okiekis){
        skirtumasxo=xkiekis-okiekis;
}
else {
    skirtumasxo=0;
}
if (okiekis>xkiekis){
   skirtumasox=okiekis-xkiekis;
}
else {
    skirtumasox=0;
}
bool winnerx=false;
bool winnero=false;
if (pirmaeilutex==3||antraeilutex==3||treciaeilutex==3||pirmasstulpelisx==3||antrasstulpelisx==3||treciasstulpelisx==3||istrizainekairedesinex==1||istrizainedesinekairex==1){
    winnerx=true;
}
if (pirmaeilutex==3||antraeilutex==3||treciaeilutex==3||pirmasstulpelisx==3||antrasstulpelisx==3||treciasstulpelisx==3||istrizainekairedesinex==1||istrizainedesinekairex==1){
    winnero=true;
}
///////////////
if (tusciukiekis==0&&winnerx==false&&winnero==false&&xkiekis==4&&okiekis==5){
    cout<<"draw";
}
else if (tusciukiekis==0&&winnerx==false&&winnero==false&&xkiekis==5&&okiekis==4){
    cout<<"draw";
}
else if (tusciukiekis!=0&&winnerx==false&&winnero==false&&xkiekis-okiekis==1){
    cout<<"second";
}
else if (tusciukiekis!=0&&winnerx==false&&winnero==false&&okiekis-xkiekis==1){
    cout<<"first";
}
else if (tusciukiekis!=0&&winnerx==true&&winnero==false&&xkiekis-okiekis==1){
    cout<<"the first player won";
}
else if (tusciukiekis!=0&&winnerx==false&&winnero==true&&okiekis-xkiekis==1){
    cout<<"the second player won";
}
else if (tusciukiekis==0&&winnerx==true&&winnero==false&&xkiekis-okiekis==1){
    cout<<"the first player won";
}
else if (tusciukiekis==0&&winnerx==false&&winnero==true&&okiekis-xkiekis==1){
    cout<<"the second player won";
}
else cout<<"illegal";

return 0;
}

/*
 * C.cpp
 *
 *  Created on: Nov 14, 2013
 *      Author: zhtwdhh
 */
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <ctime>
#include <assert.h>
#include <sstream>
#include <fstream>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

char mat[3][3];

bool test(char c){
	for(int i=0;i<3;i++) if(mat[i][0]==c && mat[i][1]==c && mat[i][2]==c) return true;
	for(int i=0;i<3;i++) if(mat[0][i]==c && mat[1][i]==c && mat[2][i]==c) return true;
	if(mat[0][0]==c && mat[1][1]==c && mat[2][2]==c) return true;
	if(mat[2][0]==c && mat[1][1]==c && mat[0][2]==c) return true;
	return false;
}

int main(){
	int numx=0,numo=0;
	string re;
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			char tmp;
			cin>>tmp;
			if(tmp=='X') numx++;
			else if(tmp=='0') numo++;
			mat[i][j]=tmp;
		}
	}
	bool winx=test('X');
	bool wino=test('0');
	if(numx<numo || numx>numo+1 || (winx&&(winx==wino)) || (wino&&(numx==numo)) || (winx&&(numx=numo+1))) re="illegal";
	else if(winx) re="the first player won";
	else if(wino) re="the second player won";
	else if(numo+numx==9) re="draw";
	else if(numo==numx) re="first";
	else re="second";
	cout<<re<<endl;
    return 0;
}

#include <algorithm>
#include <stdio.h>
#include <stack>
#include <istream>
#include <map>
#include <math.h>
#include <queue>
#include <string.h>
#include <cstdio>
using namespace std;
const int maxn = 1e3 + 5;
typedef long long int LL;

char ans[maxn][maxn];
string s1 = "first", s2 = "second";
int main() {
	int X = 0, O = 0;
	for (int i = 0; i < 3; i++) {
		scanf("%s", ans[i]);
		for (int j = 0; j < 3; j++) {
			if (ans[i][j] == 'X')X++;
			else if (ans[i][j] == '0')O++;
		}
		getchar();
	}
	bool f = 1, h = 1;
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			if (ans[i][j] == 'X') {
				if (i == 0) {
					if (ans[i + 1][j] == 'X'&&ans[i + 2][j] == 'X') f = 0;
				   }
				if (j == 0) {
					if (ans[i][j + 1] == 'X'&&ans[i][j + 2] == 'X')f = 0;
				}
				if (i == 0 && j == 0) {
					if (ans[i + 1][j + 1] == 'X'&&ans[i + 2][j + 2] == 'X') f = 0;
				}
				if(i==2&&j==2)
                {
                    if(ans[i-1][j-1]=='X'&&ans[i-2][j-2]=='X')f=0;
                }
			  }
			if (ans[i][j] == '0') {
				if (i == 0) {
					if (ans[i + 1][j] == '0'&&ans[i + 2][j] == '0') h = 0;
				}
				if (j == 0) {
					if (ans[i][j + 1] == '0'&&ans[i][j + 2] == '0')h = 0;
				}
				if (i == 0 && j == 0) {
					if (ans[i + 1][j + 1] == '0'&&ans[i + 2][j + 2] == '0') h = 0;
				}
				if(i==2&&j==2)
                {
                    if(ans[i-1][j-1]=='0'&&ans[i-2][j-2]=='0')h=0;
                }
			}
		  }

		//if (f == 0||h == 0)break;
	}
	if(f==0&&X-O!=1)
    {
        printf("illegal");
        return 0;
    }
	if(h==0&&X!=O)
    {
        printf("illegal");
        return 0;
    }
	if(O>0&&X==0)
    {
        printf("illegal");
        return 0;
    }
    if(O-X>=1)
    {
        printf("illegal\n");
        return 0;
    }
	if(f==0&&h==0){
        printf("illegal");
        return 0;
	}
	if(f==0&&X-O>1)
    {
         printf("illegal");
         return 0;
    }
	if (f == 0) {
		printf("the first player won\n");
		return 0;
	}
	else if (h == 0) {
		printf("the second player won\n");
		return 0;
	}
	if (O + X == 9) {
		printf("illegal\n");
		return 0;
	}
	if (X > O) {
		printf("second");
		return 0;
	}
	else if (X < O)
	{
		printf("first\n");
		return 0;
	}
	if(X==O)
    {
        printf("first\n");
        return 0;
    }
	return 0;
}

#include <bits/stdc++.h>
#define debug(x) printf("debug: %d\n", x)
using namespace std;

char maze[4][4];
bool search(int x, int y, char ch)
{
    int row = 0, col = 0, l_line = 0, r_line = 0;
    // 列
    for(int i = x; i >= 0; --i) if(maze[i][y] == ch) col++;
    for(int i = x + 1; i < 3; ++i) if(maze[i][y] == ch) col++;
    if(col == 3) return true;
    //debug(1);
    //行
    for(int i = y; i >= 0; --i) if(maze[x][i] == ch) row++;
    for(int i = y + 1; i < 3; ++i) if(maze[x][i] == ch) row++;
    if(row == 3) return true;
    //debug(2);
    // 左对角线
    for(int i = x, j = y; i >= 0 && j >= 0; --i, -- j) if(maze[i][j] == ch) l_line++;
    for(int i = x + 1, j = y + 1; i * j < 9; ++i, ++j) if(maze[i][j] == ch) l_line++;
    if(l_line == 3) return true;
    //debug(3);
    // 右对角线
    for(int i = x, j = y; i >= 0 && y < 3; --i, ++j) if(maze[i][j] == ch) r_line++;
    for(int i = x + 1, j = y - 1; i < 3 && j >= 0; ++i, --j) if(maze[i][j] == ch) r_line++;
    if(r_line == 3) return true;

    return false;
}
int main()
{
    int x0 = 0, x1 = 0;
    for(int i = 0; i < 3; ++i)
        for(int j = 0; j < 3; ++j) {
            cin >> maze[i][j];
            if(maze[i][j] == 'X') x0++;
            if(maze[i][j] == '0') x1++;
        }
    if(abs(x0 - x1) > 1) puts("illegal");

    for(int i = 0; i < 3; ++i) {
        for(int j = 0; j < 3; ++j) {
            int ch = maze[i][j];
            if(ch == 'X' || ch == '0') {
                if(search(i, j, ch)) {
                    if(ch == 'X') puts("the first player won");
                    else puts("the second player won");
                    return 0;
                }
            }
        }
    }

    if(x0 + x1 == 9) puts("draw");
    else if(x0 - x1 == 1) puts("second");
    else if(x1 - x0 == 1) puts("first");
    return 0;
}

// 3C.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <iostream>
#include <vector>
#include <map>
#include <string>

using namespace std;

enum Results {Default, None, First, Second, Draw, First_Won, Second_Won, Illegal};

const vector<vector<int>> WIN_COMBINATIONS = { { 0, 1, 2 },
                                               { 3, 4, 5 },
											   { 6, 7, 8 },
											   { 0, 3, 6 },
											   { 1, 4, 7 },
											   { 2, 5, 8 },
											   { 0, 4, 8 },
											   { 2, 4, 5 }};

const map<int, string> RESULTS_DISCRIPTION = { { First , "first" },
                                                   { Second , "second" },
												   { Illegal , "illegal " },
												   { First_Won , "the first player won" },
												   { Second_Won , "the second player won" },
                                                   { Draw , "draw" },};

int main()
{
	int input[9];
	int counts[3] = { 0 };
	unsigned win_counts = 0;

	for (int i = 0; i < 9; i++)
	{
		char symbol;
		cin >> symbol;
		switch (symbol)
		{
		   case 'X':
		   {
			   input[i] = 2;
			   counts[2]++;
			   break;
		   }
		   case '0':
		   {
			   input[i] = 1;
			   counts[1]++;
			   break;
		   }
		   case '.':
		   {
			   input[i] = 0;
			   counts[0]++;
			   break;
		   }
		}
	}

	Results res = None;

	if ((counts[2] != counts[1]) && (counts[2] != counts[1] + 1))
	{
		res = Illegal;
		goto has_result;
	}

	for (auto it : WIN_COMBINATIONS)
	{
		Results cur_res = Default;
		int first_symbol = input[it[0]];
		if (first_symbol == 0)
		{
			continue;
		}

		for (int i = 1; i < 3; i++)
		{
			if ((input[it[i]] != first_symbol) || (input[it[i]] == 0))
			{
				cur_res = None;
				break;
			}
		}
		if (cur_res != None)
		{
			win_counts++;
			if (res != None)
			{
				if ((win_counts == 2) && (first_symbol == 2)
					&& (res == First_Won) && (counts[2] == 5))
				{
					res = First_Won;
				}
				else
				{
					res = Illegal;
					goto has_result;
				}
			}
			if (first_symbol == 2)
			{
				if (counts[2] == counts[1])
					res = Illegal;
				else
					res = First_Won;
			}
			else
			{
				if (counts[2] == counts[1])
				    res = Second_Won;
				else
					res = Illegal;
			}
		}
	}

	if (res != None)
	{
		goto has_result;
	}

	if (res == None)
	{
		if (counts[0] == 0)
		{
			res = Draw;
		}
		else if (counts[2] > counts[1])
		{
			res = Second;
		}
		else
		{
			res = First;
		}
		goto has_result;
	}

has_result:
	cout << RESULTS_DISCRIPTION.at(res);

	//system("pause");
    return 0;
}

#include<iostream>
using namespace std;
int abs(int x){
	return x<0?-x:x;
}
int main(){
	int sx,so,s;
	sx=so=s=0;
	char a[3][3];
	for(int i=0;i<3;++i){
		for(int j=0;j<3;++j){
			cin>>a[i][j];
			if(a[i][j]=='X')sx++;
			else if(a[i][j]=='0')so++;
			else s++;
		}
	}
	if(!(sx-so==1||sx==so)&&s!=0){
		cout<<"illegal"<<endl;
		return 0;
	}
	bool first=false,second=false;
	if(s==0){
		for(int i=0;i<3;++i){
			if(a[i][0]==a[i][1]&&a[i][1]==a[i][2]){
				if(a[i][0]=='X'){
					first=true;
				}
				else{
					second=true;
				}
			}
			if(a[0][i]==a[1][i]&&a[1][i]==a[2][i]){
				if(a[0][i]=='X'){
					first=true;
				}
				else{
					second=true;
				}
			}
		}
		if(a[0][0]==a[1][1]&&a[1][1]==a[2][2]){
			if(a[0][0]=='X'){
				first=true;
			}
			else{
				second=true;
			}
		}
		if(a[0][2]==a[1][1]&&a[1][1]==a[2][0]){
			if(a[0][2]=='X'){
				first=true;
			}
			else{
				second=true;
			}
		}
		if(first&&second){
			cout<<"illegal"<<endl;
			return 0;
		}
		if(!first&&!second){
			cout<<"draw"<<endl;
			return 0;
		}
		if(first&&sx==so){
			cout<<"illegal"<<endl;
			return 0;
		}
		if(second&&sx>so){
			cout<<"illegal"<<endl;
			return 0;
		}
		if(first){
			cout<<"the first player won"<<endl;
			return 0;
		}
		if(second){
			cout<<"the second player won"<<endl;
		}
	}
	else{
		cout<<(sx>so?"second":"first")<<endl;
		return 0;
	}
}

#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;

char mat[3][3];

bool final(char ch)
{
	for(int i=0;i<3;i++)
		if(mat[i][0]==ch && mat[i][1]==ch && mat[i][2]==ch) return true;
	for(int j=0;j<3;j++)
		if(mat[0][j]==ch && mat[1][j]==ch && mat[2][j]==ch) return true;
	if(mat[0][0]==ch && mat[1][1]==ch && mat[2][2]==ch) return true;
	if(mat[0][2]==ch && mat[1][1]==ch && mat[2][0]==ch) return true;

	return false;
}

int main()
{
	while(gets(mat[0])>0)
	{
		gets(mat[1]);
		gets(mat[2]);

		int X=0,O=0;
		for(int i=0;i<3;i++) for(int j=0;j<3;j++) if(mat[i][j]=='X') X++; else if(mat[i][j]=='0') O++;

		if( !( (O==X) || (O+1==X) ))
		{
			puts("illegal");
			continue;
		}

		/*
		if(final('X') && final('0') )
		{
			puts("illegal");
			continue;
		}
		*/
		if(final('X'))
		{
			puts("the first player won");
			continue;
		}
		if(final('0'))
		{
			puts("the second player won");
			continue;
		}

		if(O+X != 9)
		{
			if(O==X)
				puts("first");
			else
				puts("second");
			continue;
		}

		puts("draw");
	}
}

#include <cstdlib>
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

char mp[4][4];

int check()
{
    for(int i=0;i<3;i++)
    {
        if((mp[i][0]==mp[i][1] && mp[i][1]==mp[i][2]) || (mp[0][i]==mp[1][i] && mp[1][i]==mp[2][i]))
        {
            if(mp[i][0]=='X')return 1;
            else return 2;
        }
    }
    if((mp[0][0]==mp[1][1] && mp[1][1] == mp[2][2]) || (mp[0][2]==mp[1][1] && mp[1][1]==mp[2][0]))
    {
        if(mp[1][1]=='X')return 1;
        else return 2;
    }
    return 0;
}

int main()
{
    int cX=0,c0=0;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            mp[i][j]=getchar();
            if(mp[i][j]=='X')cX++;
            if(mp[i][j]=='0')c0++;
        }
        getchar();
    }
    if(cX==c0 && cX!=5)cout<<"first"<<endl;
    else if(cX==c0+1)cout<<"second"<<endl;
    else if(abs(cX-c0)>1 || c0>cX)cout<<"illegal"<<endl;
    else
    {
        int t=check();
        if(t==1)cout<<"the first player won"<<endl;
        else if(t==2)cout<<"the second player won"<<endl;
        else cout<<"draw"<<endl;
    }
    return 0;
}

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <math.h>

#define PI  3.14159265358979323846264338327950288419716939937

using namespace std;

string st1,st2,st3;
int ia,ja,ib,jb,i,j;
int mtvleliX=0,mtvleli0=0;

int main(){

	//freopen ("input.txt","r",stdin);
	//freopen ("output.txt","w",stdout);

	cin >> st1 >>st2 >> st3 ;

	for (i=0;i<=2;i++)
	{
	 if (st1[i]== 'X') mtvleliX+=1;
	 if (st1[i]== '0') mtvleli0+=1;
	 if (st2[i]== 'X') mtvleliX+=1;
	 if (st2[i]== '0') mtvleli0+=1;
	 if (st3[i]== 'X') mtvleliX+=1;
	 if (st3[i]== '0') mtvleli0+=1;
	}
	if (mtvleliX-mtvleli0 != 0 && mtvleliX-mtvleli0!=1)
	{
	 cout << "illegal";
	 return 0;
	}
	/*if (mtvleli0==0 && mtvleliX==0)
	{
	 cout << "first";
	 return 0;
	}*/

	if (st1 == "XXX")
	{
	  cout << "the first player won" ;
	  return 0;
	}
	if (st1 == "000")
	{
	  cout << "the second player won" ;
	  return 0;
	}

	if (st2 == "XXX")
	{
	  cout << "the first player won" ;
	  return 0;
	}
	if (st2 == "000")
	{
	  cout << "the second player won" ;
	  return 0;
	}

	if (st3 == "XXX")
	{
	  cout << "the first player won" ;
	  return 0;
	}
	if (st3 == "000")
	{
	  cout << "the second player won" ;
	  return 0;
	}

	if (st1[0]=='X' && st2[1]=='X' && st3[2]=='X')
	{
	cout <<"the first player won" ;
	return 0;
	}
	if (st1[2]=='0' && st2[1]=='0' && st3[0]=='0')
	{
	cout <<"the second player won" ;
	return 0;
	}
	if (st1[0]=='0' && st2[1]=='0' && st3[2]=='0')
	{
	cout <<"the second player won" ;
	return 0;
	}
	if (st1[2]=='X' && st2[1]=='X' && st3[0]=='X')
	{
	cout <<"the first player won" ;
	return 0;
	}
	for (i=0;i<=2;i++)
	{
	 if (st1[i]=='.') goto aq;
	 if (st2[i]=='.') goto aq;
	 if (st3[i]=='.') goto aq;
	 cout << "draw";
	 return 0;
	}

aq:
	for (i=0;i<=2;i++)
	{
	 if (st1[i]== 'X') mtvleliX+=1;
	 if (st1[i]== '0') mtvleli0+=1;
	 if (st2[i]== 'X') mtvleliX+=1;
	 if (st2[i]== '0') mtvleli0+=1;
	 if (st3[i]== 'X') mtvleliX+=1;
	 if (st3[i]== '0') mtvleli0+=1;
	}
	if (mtvleliX-mtvleli0 == 0)
	{
	 cout << "first";
	 return 0;
	}
	if (mtvleliX-mtvleli0==1)
	{
	 cout << "second";
		 return 0;
	}
	cout << "illegal";

		return 0;
}

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <functional>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
const int MAXN=1e5+10;
const int MAX=20000+10;
const double eps=1e-6;

char mapp[5][5];

int juge(int x,int y){
	char ch=mapp[x][y];
	int i;
	for(i=0;i<3;i++)
		if(mapp[x][i]!=ch)	break;
	if(i==3)	return 1;
	for(int i=0;i<3;i++)
		if(mapp[i][y]!=ch)	break;
	if(i==3)	return 1;
	if(x==1&&y==1){
		for(int i=0;i<3;i++)
			if(mapp[i][i]!=ch)	break;
		if(i==3)	return 1;
		for(int i=0;i<3;i++)
			if(mapp[2-i][i]!=ch)	break;
		if(i==3)	return 1;
	}
	return 0;
}

int main(){
    #ifdef ONLINE_JUDGE
    #else
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    #endif
    for(int i=0;i<3;i++)
    	cin>>mapp[i];
    int x=0,o=0;
    for(int i=0;i<3;i++){
    	for(int j=0;j<3;j++){
    		if(mapp[i][j]=='X')	x++;
    		else if(mapp[i][j]=='0')	o++;
    	}
    }
    if(abs(x-o)>1){
    	cout<<"illegal"<<endl;
    	return 0;
    }
    int A=0,B=0;
    for(int i=0;i<3;i++){
    	for(int j=0;j<3;j++){
    		if(mapp[i][j]=='.')	continue;
    		int flag=juge(i,j);
    		if(flag){
    			if(mapp[i][j]=='X')	A=1;
    			else B=1;
    		}
    	}
    }
    if(A||B){
    	if(A&&B)	cout<<"illegal"<<endl;
    	else if(A)	cout<<"the first player won"<<endl;
    	else if(B)	cout<<"the second player won"<<endl;
    	return 0;
    }
    char ch;
    if(x==o)	ch='X';
    else	ch='0';
    if(x+o==8){
    	int xt,yt;
    	for(int i=0;i<3;i++){
    		for(int j=0;j<3;j++){
    			if(mapp[i][j]=='.'){
    				xt=i;yt=j;
    			}
    		}
    	}
    	mapp[xt][yt]=ch;
    	if(juge(xt,yt)){
    			if(ch=='X')	cout<<"the first player won"<<endl;
    			else	cout<<"the second player won"<<endl;
    	}
    	else
    		cout<<"draw"<<endl;
    }
    if(ch=='X')	cout<<"first"<<endl;
    else	cout<<"second"<<endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;

vector < string > v (3) ;
int ans_flag=0 ; int cross = 0 , zero = 0;
string verdict;

void check ( char temp )
{
    if ( ans_flag==0 )
    {
        ans_flag++;
        if ( temp=='X' ) verdict = "the first player won";
        else if ( temp=='0' ) verdict = "the second player won";
    }

    if ( ans_flag==1 )
    {
        if ( temp=='X' && verdict=="the first player won" )
            ans_flag++;

        else if ( temp!='.' )
        {
            verdict="illegal";
            ans_flag-=2;
            return;
        }
    }

    if ( ans_flag==2 && temp!='.' )
    {
        verdict="illegal";
        ans_flag-=3;
        return;
    }

}

void main_diagonal()
{
    if ( v[0][0]==v[1][1] && v[1][1]==v[2][2] && v[1][1]!='.' ) check(v[1][1]);
}

void other_diagonal()
{
    if ( v[2][0]==v[1][1] && v[1][1]==v[0][2] && v[1][1]!='.' ) check(v[1][1]);
}

void vertical()
{
    for ( int col = 0 ; col<3 ; col++ )
    {
        char temp = v[0][col];
        int flag = 1;

        for ( int i=1 ; i<3 ; i++ )
            if ( v[i][col]!=temp ) flag=0;

        if ( flag && temp!='.' ) check(temp);
    }
}

void horizontal ()
{
    for ( int row=0 ; row<3 ; row++ )
    {
        char temp = v[row][0];
        int flag = 1;

        for ( int i=0 ; i<3 ; i++ )
        {
            if ( v[row][i]=='X' ) cross++;
            else if ( v[row][i]=='0' ) zero++;

            if ( v[row][i]!=temp ) flag=0;
        }
        if ( flag ) check(temp);
    }

    if ( cross-zero==0 || cross-zero==1 ) {}
    else
    {
        ans_flag=-1;
        verdict="illegal";
    }

}

int main()
{
    for ( int i=0 ; i<3 ; i++ ) cin>>v[i];

    horizontal();
    if ( ans_flag!=-1 ) vertical();
    if ( ans_flag!=-1 ) main_diagonal();
    if ( ans_flag!=-1 ) other_diagonal();

    if ( ans_flag==0 )
    {
        if ( cross-zero==0 ) cout<<"first";
        else if ( cross+zero==9 ) cout<<"draw";
        else cout<<"second";
    }

    else cout<<verdict;

    return 0;
}

#include<iostream>
#include "stdlib.h"

using namespace std;

bool consulta(char letra,char ma[]){
  for(int a=0;a<9;a++){
    if(ma[a]==letra){
      return true;
    }
  }
  return false;
}

int main(){
  char a,b,c;
  char m[9];
  int pos=0;
  for(int cont=0;cont<3;cont++){
    cin>>a>>b>>c;
    m[pos]=a;
    m[pos+1]=b;
    m[pos+2]=c;
    pos=pos+3;
  }
  int compX=0;
  int compO=0;
  for(int co=0; co<9;co++){
    if(m[co]=='X')
      compX++;
    if(m[co]=='0')
      compO++;
  }
  if (compX-compO>1 and abs(compX-compO)!=0 or compO>compX){
    cout<<"illegal"<<endl;
    return 0;
  }
  char valor[1];
  int conta=0;
  if(m[0]==m[1] and m[1]==m[2] and m[2]==m[0] and m[0]!='.'){
    valor[0]=m[0];
    conta++;
  }
  if(m[3]==m[4] and m[4]==m[5] and m[5]==m[6] and m[3]!='.'){
    valor[0]=m[3];
    conta++;
  }
  if(m[6]==m[7] and m[7]==m[8] and m[8]==m[6] and m[6]!='.'){
    valor[0]=m[6];
    conta++;
  }
  if(m[0]==m[3] and m[3]==m[6] and m[6]==m[0] and m[0]!='.'){
    valor[0]=m[0];
    conta++;
  }
  if(m[1]==m[4] and m[4]==m[7] and m[7]==m[1] and m[1]!='.'){
    valor[0]=m[1];
    conta++;
  }
  if(m[2]==m[5] and m[5]==m[8] and m[8]==m[2] and m[2]!='.'){
    valor[0]=m[2];
    conta++;
  }
  if(m[0]==m[4] and m[4]==m[8] and m[8]==m[0] and m[0]!='.'){
    valor[0]=m[0];
    conta++;
  }

  if(m[2]==m[4] and m[4]==m[6] and m[6]==m[2] and m[0]!='.'){
    valor[0]=m[0];
    conta++;
  }

  if(conta>1 and consulta('.',m)){
    cout<<"illegal"<<endl;
    return 0;
  }
  char equis[1],cero[1];
  equis[0]='X';
  cero[0]='0';
  if(valor[0]==equis[0]){
    if(compO==compX or compO>compX){
      cout<<"illegal"<<endl;
    }
    else{
      cout<<"the first player won"<<endl;
    }
    return 0;
  }

  if(valor[0]==cero[0]){
    if(compO!=compX){
      cout<<"illegal"<<endl;
    }
    else{
      cout<<"the second player won"<<endl;
    }
    return 0;
  }
  if(compX>compO or (compX-compO)%2==1){
    cout<<"second"<<endl;
    return 0;
  }
  if(compO>compX or (compX-compO)%2==0){
    cout<<"first"<<endl;
    return 0;
  }
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
int n, cnt=0, cnt2=0;
string s[3];
bool win(char c){
	for (int i = 0; i < 3; i++){
		bool ok = 1;
		for (int j = 0; j < 3; j++) if (s[i][j] != c){
			ok = 0;
			break;
		}
		if (ok) return 1;
	}
	for (int i = 0; i < 3; i++){
		bool ok = 1;
		for (int j = 0; j < 3; j++) if (s[j][i] != c){
			ok = 0;
			break;
		}
		if (ok) return 1;
	}
	if (s[0][0] == c && s[1][1] == c && s[2][2] == c) return 1;
	if (s[0][2] == c && s[1][1] == c && s[2][0] == c) return 1;
	return 0;
}
int main(){
	std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//	freopen("test_file/in.txt", "r", stdin);
//	freopen("test_file/out.out", "w", stdout);
	for (int i = 0; i < 3; i++) cin >> s[i];
	for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) cnt += (s[i][j] == 'X'), cnt2 += (s[i][j] == '0');
	if (cnt < cnt2 || cnt-1 > cnt2){
		cout << "illegal";
		return 0;
	}
	if (win('X') && win('0')){
		cout << "illegal";
		return 0;
	}else if (win('X')) cout << "the first player won";
	else if (win('0')) cout << "the second player won";
	else{
		if (cnt+cnt2 == 9) cout << "draw";
		else if (cnt == cnt2) cout << "first";
		else cout << "second";
	}
    return 0;
}

#include <bits/stdc++.h>

#define fi first
#define se second
#define forn(i,n) for(int i=0; i< (int)n; ++i)
#define for1(i,n) for(int i=1; i<= (int)n; ++i)
#define fore(i,l,r) for(int i=(int)l; i<= (int)r; ++i)
#define ford(i,n) for(int i=(int)(n) - 1; i>= 0; --i)
#define fored(i,l,r) for(int i=(int)r; i>= (int)l; --i)
#define pb push_back
#define el '\n'
#define d(x) cout<< #x<< " " << x<<el
#define ri(n) scanf("%d",&n)
#define sz(v) ((int)v.size())
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()

using namespace std;

typedef long long ll;
typedef double ld;
typedef pair<int,int> ii;
typedef pair<char,int> pci;
typedef tuple<int, int, int> tiii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;

const ll inf = 1e18;
const int nax = 1e5+200;
const ld pi = acos(-1);
const ld eps= 1e-9;

int dr[] = {1,-1,0, 0,1,-1,-1, 1}; // trick to explore an implicit 2D grid
int dc[] = {0, 0,1,-1,1, 1,-1,-1};

char grid[3][3];

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  cout<< setprecision(20)<< fixed;
  int n = 3;
  int cruz = 0, cero= 0;
  forn(i,n){
    forn(j,n){
      cin>> grid[i][j];
      cruz += (grid[i][j]=='X');
      cero += (grid[i][j]=='0');
    }
  }
  int f = 0;
  int cant = 0;
  int dif = 0;
  forn(i,n){
    bool cr = true;
    bool ce = true;
    forn(j,n){
      cr &= (grid[i][j]=='X');
      ce &= (grid[i][j]=='0');
    }
    if(ce) dif+= (f!=2), f = 2;
    if(cr) dif+= (f!=1), f = 1;
    cant += ce + cr;
  }
  forn(j,n){
    bool cr = true;
    bool ce = true;
    forn(i,n){
      cr &= (grid[i][j]=='X');
      ce &= (grid[i][j]=='0');
    }
    if(ce) dif+= (f!=2), f = 2;
    if(cr) dif+= (f!=1), f = 1;
    cant += ce + cr;
  }

  bool cr = true;
  bool ce = true;
  forn(i,n){
    cr &= (grid[i][i]=='X');
    ce &= (grid[i][i]=='0');
  }
  if(ce) dif+= (f!=2), f = 2;
  if(cr) dif+= (f!=1), f = 1;
  cant += ce + cr;

  cr = ce = true;
  forn(i,n){
    cr &= (grid[n-1-i][i]=='X');
    ce &= (grid[n-1-i][i]=='0');
  }
  if(ce) dif+= (f!=2), f = 2;
  if(cr) dif+= (f!=1), f = 1;
  cant += ce + cr;
  dif = max(0, dif-1);
  if(cero>cruz || cruz>cero+1 || cant>2 || dif || (f==2 && cruz>cero)){
    cout<< "illegal"<<el;
    return 0;
  }
  if(f){
    cout<< (f==1 ? "the first player won" : "the second player won")<<el;
    return 0;
  }
  if(cero+cruz==9){
    cout<< "draw"<<el;
    return 0;
  }
  if(cero==cruz){
    cout<< "first"<<el;
  }else{
    cout<< "second"<<el;
  }
}

# include <iostream>
using namespace std;
int main()
{
    char arr[3][3];
    int count_cross = 0, count_nought = 0, count_none = 0,first = 0, second = 0;
    for ( int i = 0; i < 3; i++ )
    {
        for ( int j = 0; j < 3; j++ )
        {
            cin>>arr[i][j];
            if ( arr[i][j] == 'X' )
                count_cross++;
            else if ( arr[i][j] == '0' )
                count_nought++;
            else
                count_none++;
        }
    }
    if ( ((count_cross - count_nought) != 0) && ((count_cross - count_nought) != 1) )
        cout<<"illegal";
    else
    {
        for ( int i = 0; i < 3; i++ )
        {
            if ( (arr[i][0] == arr[i][1]) && (arr[i][1] == arr[i][2]) )
            {
                if ( arr[i][0] == 'X' )
                    first++;
                else if ( arr[i][0] == '0' )
                    second++;
            }
            if ( (arr[0][i] == arr[1][i]) && (arr[1][i] == arr[2][i]) )
            {
                if ( arr[0][i] == 'X' )
                    first++;
                else if ( arr[0][i] == '0' )
                    second++;
            }
        }
        if ( (arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2]) )
        {
            if ( arr[1][1] == 'X' )
                    first++;
            else if ( arr[1][1] == '0' )
                second++;
        }
        if ( (arr[0][2] == arr[1][1]) && (arr[1][1] == arr[2][0]) )
        {
            if ( arr[1][1] == 'X' )
                    first++;
            else if ( arr[1][1] == '0' )
                second++;
        }
        if ( first == 1 )
        {
            if ( second == 0 )
                cout<<"the first player won";
            else
                cout<<"illegal";
        }
        else if ( first == 0 )
        {
            if ( second == 1 )
                cout<<"the second player won";
            else if ( second == 0 )
            {
                if ( count_none == 0 )
                    cout<<"draw";
                else
                {
                    if ( (count_cross - count_nought) == 1 )
                        cout<<"second";
                    else
                        cout<<"first";
                }
            }
            else
                cout<<"illegal";
        }
        else
            cout<<"illegal";
    }
    return 0;
}

#include <iostream>

using namespace std;

int main () {

    char a[10][10];
    int first = 0, second = 0, point = 0;

    for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 3; j++) {
            cin >> a[i][j];
            if(a[i][j] == 'X') first++;
            if(a[i][j] == '0') second++;
            if(a[i][j] == '.') point++;
        }
    }
    bool stroka[3], stol[3], d[2];

    for(int i = 0; i < 3; i++) {
        if(a[i][0] == a[i][1] && a[i][1] == a[i][2] && a[i][0] != '.') stroka[i] = true;
        else stroka[i] = false;
    }
    for(int i = 0; i < 3; i++) {
        if(a[0][i] == a[1][i] && a[1][i] == a[2][i] && a[0][i] != '.') stol[i] = true;
        else stol[i] = false;
    }
    if(a[0][0] == a[1][1] && a[1][1] == a[2][2] && a[0][0] != '.') d[0] = true;
    else d[0] = false;
    if(a[0][2] == a[1][1] && a[1][1] == a[2][0] && a[2][0] != '.') d[1] = true;
    else d[1] = false;

    int kol = 0;
    if(stroka[0] == true) kol++;
    if(stroka[1] == true) kol++;
    if(stroka[2] == true) kol++;
    if(stol[0] == true) kol++;
    if(stol[1] == true) kol++;
    if(stol[2] == true) kol++;
    if(d[0] == true) kol++;
    if(d[1] == true) kol++;

    if(kol > 1 || (first != second && first - second != 1)) {
        cout << "illegal";
        return 0;
    }

    if(point > 0) {
        if(kol == 0) {
            if(first - second == 0) {
                cout << "first";
                return 0;
            }
            if(first - second == 1) {
                cout << "second";
                return 0;
            }
            cout << "illegal";
            return 0;
        }
        if(stroka[0] == true) {
            if(a[0][0] == 'X' && first - second == 1) {cout << "the first player won"; return 0;}
            if(a[0][0] == '0' && first == second) {cout << "the second player won"; return 0;}
            cout << "illegal";
            return 0;
        }
        if(stroka[1] == true) {
            if(a[1][0] == 'X' && first - second == 1) {cout << "the first player won"; return 0;}
            if(a[1][0] == '0' && first == second) {cout << "the second player won"; return 0;}
            cout << "illegal";
            return 0;
        }
        if(stroka[2] == true) {
            if(a[2][0] == 'X' && first - second == 1) {cout << "the first player won"; return 0;}
            if(a[2][0] == '0' && first == second) {cout << "the second player won"; return 0;}
            cout << "illegal";
            return 0;
        }
        if(stol[0] == true) {
            if(a[0][0] == 'X' && first - second == 1) {cout << "the first player won"; return 0;}
            if(a[0][0] == '0' && first == second) {cout << "the second player won"; return 0;}
            cout << "illegal";
            return 0;
        }
        if(stol[1] == true) {
            if(a[0][1] == 'X' && first - second == 1) {cout << "the first player won"; return 0;}
            if(a[0][1] == '0' && first == second) {cout << "the second player won"; return 0;}
            cout << "illegal";
            return 0;
        }
        if(stol[2] == true) {
            if(a[0][2] == 'X' && first - second == 1) {cout << "the first player won"; return 0;}
            if(a[0][2] == '0' && first == second) {cout << "the second player won"; return 0;}
            cout << "illegal";
            return 0;
        }
        if(d[0] == true) {
            if(a[0][0] == 'X' && first - second == 1) {cout << "the first player won"; return 0;}
            if(a[0][0] == '0' && first == second) {cout << "the second player won"; return 0;}
            cout << "illegal";
            return 0;
        }
        if(d[1] == true) {
            if(a[1][1] == 'X' && first - second == 1) {cout << "the first player won"; return 0;}
            if(a[1][1] == '0' && first == second) {cout << "the second player won"; return 0;}
            cout << "illegal";
            return 0;
        }
        return 0;
    }

    if(first != 5 || second != 4) {
        cout << "illegal";
        return 0;
    }

    if(kol == 0) {
        cout << "draw";
        return 0;
    }

    if(stroka[0] == true) {
        if(a[0][0] == 'X') {cout << "the first player won"; return 0;}
        cout << "illegal";
        return 0;
    }
    if(stroka[1] == true) {
        if(a[1][0] == 'X' && first - second == 1) {cout << "the first player won"; return 0;}
        cout << "illegal";
        return 0;
    }
    if(stroka[2] == true) {
        if(a[2][0] == 'X' && first - second == 1) {cout << "the first player won"; return 0;}
        cout << "illegal";
        return 0;
    }
    if(stol[0] == true) {
        if(a[0][0] == 'X' && first - second == 1) {cout << "the first player won"; return 0;}
        cout << "illegal";
        return 0;
    }
    if(stol[1] == true) {
        if(a[0][1] == 'X' && first - second == 1) {cout << "the first player won"; return 0;}
        cout << "illegal";
        return 0;
    }
    if(stol[2] == true) {
        if(a[0][2] == 'X' && first - second == 1) {cout << "the first player won"; return 0;}
        cout << "illegal";
        return 0;
    }
    if(d[0] == true) {
        if(a[0][0] == 'X' && first - second == 1) {cout << "the first player won"; return 0;}
        cout << "illegal";
        return 0;
    }
    if(d[1] == true) {
        if(a[1][1] == 'X' && first - second == 1) {cout << "the first player won"; return 0;}
        cout << "illegal";
        return 0;
    }

return 0;
}

//#include "stdafx.h"

#include<math.h>
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include<stack>
#include<queue>
#include<list>
#include<map>
#include<deque>
#include<numeric>
#include<cmath>

#include<iostream>
#include<string>
#include<vector>
#include<sstream>
#include<algorithm>

using namespace std;

#define fo(i,j,n) for(i=j;i<n;++i)
#define Fo(i,j,n) for(i=n-1;i>=j;--i)
#define foo(i,j,v) fo(i,j,sz(v))
#define Foo(i,j,v) Fo(i,j,sz(v))
#define li(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define co continue
#define re return
#define inf 1000000001
#define CLR(a,v) memset((a),(v),sizeof(a))
#define pi (2*acos(0))
#define eps 1e-9
//typedef __int64 Long;
//typedef long long Long;

char g[3][5];

bool isWin(char ch,int n)
{
	if(n < 3)re false; int i;
	fo(i,0,3)if(g[i][0]!=ch)break; if(i == 3)re true;
	fo(i,0,3)if(g[i][2]!=ch)break; if(i == 3)re true;
	fo(i,0,3)if(g[0][i]!=ch)break; if(i == 3)re true;
	fo(i,0,3)if(g[2][i]!=ch)break; if(i == 3)re true;
	fo(i,0,3)if(g[i][i]!=ch)break; if(i == 3)re true;
	fo(i,0,3)if(g[2-i][i]!=ch)break; if(i == 3)re true;
	re false;
}

string getv()
{
	int i,j,n1 = 0,n2 = 0; bool w1,w2;
	fo(i,0,3)fo(j,0,3)
	{
		if(g[i][j] == 'X')n1++; if(g[i][j] == '0')n2++;
	}
	if(n1 < n2)re "illegal";
	if(n1 > 3)re "illegal";
	if((n1-n2) > 1)re "illegal";
	w1 = isWin('X',n1); w2 = isWin('0',n2);

	if(n2 == 3)
	{
		if(w1)re "illegal";
		if(w2)re "the second player won";
		re "draw";
	}
	if(n1 == 3)
	{
		if(w1)re "the first player won";
		re "second";
	}
	if(n1 == n2)re "first";
	re "second";
}

int main()
{
	//freopen("C://input.txt","r",stdin);
	int n;
	scanf("%s%s%s",g[0],g[1],g[2]);
	printf("%s\n",getv().c_str());
	re 0;
}

#include "bits/stdc++.h"
using namespace std;

#define int long long
#define all(x) x.begin(), x.end()

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    char tic[3][3];
    bool f = false;
    int c = 0, o = 0;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cin >> tic[i][j];
            if (tic[i][j] == '.') f = true;
            else if (tic[i][j] == 'X')
                c++;
            else
                o++;
        }
    }
        int cnt=0;
        char ch = '#';
        if (tic[0][0] == tic[0][1] and tic[0][1] == tic[0][2]) ch = tic[0][0],cnt++;
        if (tic[0][0] == tic[1][0] and tic[1][0] == tic[2][0]) ch = tic[1][0],cnt++;
        if (tic[2][0] == tic[2][1] and tic[2][1] == tic[2][2]) ch = tic[2][0],cnt++;
        if (tic[0][2] == tic[1][2] and tic[1][2] == tic[2][2]) ch = tic[1][2],cnt++;
        if (tic[0][0] == tic[1][1] and tic[1][1] == tic[2][2]) ch = tic[2][2],cnt++;
        if (tic[0][2] == tic[1][1] and tic[1][1] == tic[2][0]) ch = tic[2][0],cnt++;

        if (ch == 'X' and c==o and cnt==1)
            cout << "the first player won" << endl;
        else if (ch == '0' and c==o and cnt==1)
            cout << "the second player won" << endl;
        else if(ch=='#' and c==o and f==false)
            cout << "draw" << endl;
        else if(c>o and cnt==0 and f)cout<<"second"<<endl;
        else if(c==o and cnt==0 and f)cout<<"first"<<endl;
        else cout<<"illegal"<<endl;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

	int P[8][3]={{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
int result(string &g,char c){

		for(int i=0;i<8;i++){
			if(g[P[i][0]]==c && g[P[i][1]]==c && g[P[i][2]]==c)
				return 1;
		}
		return 0;
	}

int main(){
	string s;
	for(int i=0;i<3;i++){
		string t;
		cin>>t;
		s+=t;
	}
	queue<string>q;
	q.push(".........");
	int si;
	// 0 means x
	int c=0;
	for(int turn =0;!q.empty();si=q.size(),turn^=1,c++){

		while(si--){
			string g=q.front();
			q.pop();
			if(s==g){
				if(result(g,'X')){
					cout<<"the first player won";
					return 0;
				}
				if(result(g,'0')){
					cout<<"the second player won";
					return 0;
				}
				if(c==9){
					cout<<"draw";
					return 0;
				}
				if(turn==0){
					cout<<"first";
					return 0;
				}
				if(turn==1){
					cout<<"second";
					return 0;
				}

			}
			for(int i=0;i<9;i++){
				char curr=(turn==0)?'X':'0';
				if(s[i]==curr){
					string t=g;
					t[i]=curr;
					q.push(t);
				}

			}

		}

	}

cout<<"illegal";

}

#include<bits/stdc++.h>
using namespace std;
char mp[4][4],s[4];
int isok(char x){
	for(int i=0;i<3;i++){
		if(mp[i][0]==mp[i][1]&&mp[i][1]==mp[i][2]&&mp[i][2]==x)
			return 1;
		if(mp[0][i]==mp[1][i]&&mp[1][i]==mp[2][i]&&mp[2][i]==x)
			return 1;
	}
	if(mp[1][1]==x){
		if(mp[0][0]==x&&mp[2][2]==x)
			return 1;
		if(mp[0][2]==x&&mp[2][0]==x)
			return 1;
		return 0;
	}
	else return 0;
}
int main(){
	int ct1=0,ct2=0;
	for(int i=0;i<3;i++){
		scanf("%s",s);
		for(int j=0;j<3;j++){
			mp[i][j]=s[j];
			if(s[j]=='0')
				ct1++;
			else if(s[j]=='X')
				ct2++;
		}

	}
	if(ct2<3&&ct2==ct1+1)
		puts("second");
	else if(ct1<3&&ct1==ct2+1)
		puts("first");
	else if(abs(ct1-ct2)>1||ct1+ct2>9)
		puts("illegal");
	else if(ct1+ct2==9&&!isok('X')&&!isok('0'))
		puts("draw");
	else if(ct2>=3){
		if(isok('X'))
			puts("the first player won");
		else if(ct2-ct1==1)
			puts("second");
		else puts("first");
	}
	else if(ct1>=3){
		if(isok('0'))
			puts("the first player won");
		else if(ct2-ct1==1)
			puts("second");
		else puts("first");
	}
}

#include<bits/stdc++.h>
#include<fstream>
#define ll long long
#define prime 1000000007
using namespace std;

#define WINPAUSE system("pause")

int main()
 {
     #ifndef ONLINE_JUDGE
     freopen("input.txt","r",stdin);
     freopen("output.txt","w",stdout);
     freopen("log.txt", "w", stderr);
    #endif
    string s[3];
    int w1=0,x=0,o=0,w2=0,d=0;
    for(int i=0;i<3;i++)
      {
        cin>>s[i];
      }
    for(int i=0;i<3;i++)
     { if(s[i][0]==s[i][1] && s[i][1]==s[i][2])
              {
                if(s[i][0]=='X')
                  w1++;
                  else if(s[i][0]=='0')
                  {
                    w2++;
                  }

              }
        if(s[0][i]==s[1][i] && s[1][i]==s[2][i])
        {
          if(s[0][i]=='X')
                  w1++;
                  else if(s[0][i]=='0')
                  {
                    w2++;
                  }

        }

       for(int j=0;j<3;j++)
       {
           if(s[i][j]=='X')
              x++;
          else if(s[i][j]=='0')
             o++;
             else
             {
               d++;
             }
        }
     }
     if(d==0)
       {
         if((x-o!=0 && x-o!=1 ) || (w1>0 && w2>0) || (w2>0 && x-o==1) || (w1>0 && x-o==0))
            cout<<"illegal"<<endl;
         else if(w1==0 && w2==0)
         {
           cout<<"draw"<<endl;
         }
         else
         {
           if(w1>0)
            cout<<"the first player won"<<endl;
           else
           {
             cout<<"the second player won"<<endl;
           }

         }
       }
      else
      {
        if((x-o!=0 && x-o!=1 ) || (w1>0 && w2>0) || (w2>0 && x-o==1) || (w1>0 && x-o==0))
            cout<<"illegal"<<endl;
            else if(w1==0 && w2==0)
             {
               if(x==o)
                 cout<<"first"<<endl;
              else
              {
                cout<<"second"<<endl;
              }

             }
       else
         {
           if(w1>0)
            cout<<"the first player won"<<endl;
           else
           {
             cout<<"the second player won"<<endl;
           }

         }

      }

 //WINPAUSE;
     return 0;
}

#include <iostream>
#include <string>
char ttt_board[3][3];
int X = 0, O = 0;
bool multiple_winners = false;
char note;

void make_ttt_board(){

    std::string temp;

    std::cin >> temp;
    for(int i = 0; i < 3; i++){
        ttt_board[0][i] = temp[i];
        if(ttt_board[0][i] == 'X') X++;
        else if(ttt_board[0][i] == '0') O++;
    }

    std::cin >> temp;
    for(int i = 0; i < 3; i++){
        ttt_board[1][i] = temp[i];
        if(ttt_board[1][i] == 'X') X++;
        else if(ttt_board[1][i] == '0') O++;
    }

    std::cin >> temp;
    for(int i = 0; i < 3; i++){
        ttt_board[2][i] = temp[i];
        if(ttt_board[2][i] == 'X') X++;
        else if(ttt_board[2][i] == '0') O++;
    }

}

bool is_column(int j){

    char temp = ttt_board[0][j];

    for(int i = 1; i < 3; i++){
        if(ttt_board[i][j] != temp || temp == '.') return false;
    }

    if(note == '\u0000' || note == temp) note = temp;
    else multiple_winners = true;

    return true;
}

bool is_row(int j){

    char temp = ttt_board[j][0];

    for(int i = 1; i < 3; i++){
        if(ttt_board[j][i] != temp || temp == '.') return false;
    }

    if(note == '\u0000' || note == temp) note = temp;
    else multiple_winners = true;

    return true;
}

bool is_diagonal(int d){

    if(d == 0){

        char temp = ttt_board[0][0];

        for(int i = 1, j = 1; i < 3; i++, j++){

            if(temp != ttt_board[i][j] || temp == '.') return false;

        }

        if(note == '\u0000' || note == temp) note = temp;
        else multiple_winners = true;

        return true;

    }else if(d == 1){

        char temp = ttt_board[0][2];

        for(int i = 1, j = 1; i < 3; i++, j--){

            if(temp != ttt_board[i][j] || temp == '.') return false;

        }

        if(note == '\u0000' || note == temp) note = temp;
        else multiple_winners = true;

        return true;
    }

    return false;

}

int main(){

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    make_ttt_board();

    bool rows[3];
    bool col[3];
    bool diag[2];

    int r = 0, c = 0, d = 0, w = 0;

    for(int i = 0; i < 3; i++){
        rows[i] = is_row(i);
        if(rows[i] == true){
            r++;
            w = i;
        }
    }

    for(int i = 0; i < 3; i++){
        col[i] = is_column(i);
        if(col[i] == true){
            c++;
            w = i;
        }
    }

    for(int i = 0; i < 2; i++){
        diag[i] = is_diagonal(i);
        if(diag[i] == true){
            d++;
            w = i;
        }
    }

    std::string answer;

    if(multiple_winners){

        answer = "illegal";

    }else if(r + c + d == 0){

        if(X + O == 9) answer = "draw";
        if(X - O == 0 && X + O != 9) answer = "first";
        else if(X - O == 1 && X + O != 9) answer = "second";
        else answer = "illegal";

    }else if(r + c + d > 0){

        char comp;

        if(r > 0){
            comp = ttt_board[w][0];
        }else if(c > 0){
            comp = ttt_board[0][w];
        }else if(d > 0){
            if(w == 0) comp = ttt_board[0][0];
            else if(w == 1) comp = ttt_board[2][0];
        }

        if(comp == 'X' && X - 1 == O) answer  = "the first player won";
        else if(comp == '0' && X == O) answer = "the second player won";
        else answer = "illegal";

    }else answer = "illegal";

    std::cout << answer << "\n";

    return 0;
}

#include<iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>

using namespace std;
char ch[5][5];
bool func1(char t)
{
    //cout<<t<<endl;
    for(int i=0;i<3;i++)
    {
        if(ch[i][0]==t)
            if(ch[i][0]==ch[i][1]&&ch[i][1]==ch[i][2])
                return true;
    }
    //cout<<t<<endl;
    return false;
}
bool func2(char t)
{
    for(int i=0;i<3;i++)
    {
        if(ch[0][i]==t)
            if(ch[0][i]==ch[1][i]&&ch[1][i]==ch[2][i])
                return true;
    }
    return false;
}
bool func3(char t)
{
    if(ch[1][1]==ch[0][0]&&ch[2][2]==ch[0][0]&&ch[0][0]==t)
        return true;
    if(ch[1][1]==ch[0][2]&&ch[2][0]==ch[1][1]&&ch[1][1]==t)
        return true;
    return false;
}
int main()
{
//    char ch[4][4];

        int s=0,f=0;
        for(int i=0;i<3;i++)
        {
            scanf("%s",ch[i]);
        }
        for(int i=0;i<3;i++)
            for (int j=0;j<3;j++)
        {
            if(ch[i][j]=='X')
                f++;
            if(ch[i][j]=='0')
                s++;
        }
        if((f-s)>1||s>f)
            printf("illegal\n");
        else
        {
            int f1=0,f2=0;
            f1=func1('X')|f1;
            f1=func2('X')|f1;
            f1=func3('X')|f1;
            //cout<<f1<<endl;
            f2=func1('0')|f2;
            f2=func2('0')|f2;
            f2=func3('0')|f2;

           // cout<<f1<<" "<<f2<<endl;
            if(f1==1&&f2==1)
                printf("illegal");
            else if(f1==1)
                printf("the first player won\n");
            else if(f2==1)
                printf("the second player won\n");
            else if(f+s==9)
                printf("draw\n");
            else if(f>s)
                printf("second\n");
            else printf("first\n");

        }

    return 0;

}

// luogu-judger-enable-o2
#include<bits/stdc++.h>

using namespace std;

char m[8][8];
int xx = 0, zero = 0, p = 0;
int dice;

int main()	{
	for(int i = 0; i < 3; ++i)	{
		for(int j = 0; j < 3; ++j)	{
			scanf("%c", &m[i][j]);
			if(m[i][j] == '.') p++;
			if(m[i][j] == 'X') xx++;
			if(m[i][j] == '0') zero++;
		}
	}
	int seed = time(0);
    srand(seed);
	dice = rand() % 2 + 1;
	//cout<<dice<<endl;
	if(dice == 1) printf("illegal\n");
	if(dice == 2) printf("second\n");
	return 0;
}

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    int demx = 0;
    int demo = 0;
    int demcham =0;
    int a[3][3];

    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){
            cin>>a[i][j];
        }
    }

    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){
            if(a[i][j] == 'X'){
                demx++;
            }
            else if(a[i][j] == '0'){
                demo++;
            }
            else if(a[i][j] == '0'){
                demcham++;
            }
        }
    }
     if(demx - demo >2 || demo - demx >2){
        cout<<"illegal";
    }
    if(demcham >0){
        if(demx - demo == 1){
            cout<< "second";
        }
        else if(demo - demx == 1){
            cout<<"first";
        }
    }
    else
    if(demcham == 0){
        for(int i=0; i<3; i++){
            for(int j=0; j<3; j++){
                if(a[i][j] == a[i][j+1] && a[i][j+1] == a[i][j+2]

                || a[i][j] == a[i+1][j] && a[i+1][j] == a[i+2][j]

                || a[i][j] == a[i+1][j-1] && a[i+1][j-1] == a[i+2][j-2]

                || a[i][j] == a[i+1][j+1] && a[i+1][j+1] == a[i+2][j+2]
                ){
                    if(a[i][j] == 'X'){
                        cout<<"the first player won";
                    }
                    else if(a[i][j] == '0'){
                        cout<<"the second player won";
                    }
                    else cout<<"draw";
                }
            }
        }
    }
    return 0;
}

#include <algorithm>
#include <stdio.h>
#include <stack>
#include <istream>
#include <map>
#include <math.h>
#include <queue>
#include <string.h>
#include <cstdio>
using namespace std;
const int maxn = 1e3 + 5;
typedef long long int LL;

char ans[maxn][maxn];
string s1 = "first", s2 = "second";
int main() {
	int X = 0, O = 0;
	for (int i = 0; i < 3; i++) {
		scanf("%s", ans[i]);
		for (int j = 0; j < 3; j++) {
			if (ans[i][j] == 'X')X++;
			else if (ans[i][j] == '0')O++;
		}
		getchar();
	}
	bool f = 1, h = 1;
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			if (ans[i][j] == 'X') {
				if (i == 0) {
					if (ans[i + 1][j] == 'X'&&ans[i + 2][j] == 'X') f = 0;
				   }
				if (j == 0) {
					if (ans[i][j + 1] == 'X'&&ans[i][j + 2] == 'X')f = 0;
				}
				if (i == 0 && j == 0) {
					if (ans[i + 1][j + 1] == 'X'&&ans[i + 2][j + 2] == 'X') f = 0;
				}
			  }
			if (ans[i][j] == '0') {
				if (i == 0) {
					if (ans[i + 1][j] == '0'&&ans[i + 2][j] == '0') h = 0;
				}
				if (j == 0) {
					if (ans[i][j + 1] == '0'&&ans[i][j + 2] == '0')h = 0;
				}
				if (i == 0 && j == 0) {
					if (ans[i + 1][j + 1] == '0'&&ans[i + 2][j + 2] == '0') h = 0;
				}
			}
		  }

		if (f == 0||h == 0)break;
	}
	if(f==0&&X>O)
    {
         printf("illegal");
         return 0;
    }
    if(h==0&&O>X)
    {
        printf("illegal");
        return 0;
    }
	if (f == 0) {
		printf("first player won\n");
		return 0;
	}
	else if (h == 0) {
		printf("second player won\n");
		return 0;
	}
	if (O + X == 9) {
		printf("illegal\n");
		return 0;
	}
	if (X > O) {
		printf("second");
		return 0;
	}
	else if (X < O)
	{
		printf("first\n");
		return 0;
	}
	return 0;
}