using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;


public class KidsWordGame
{
	public int getCheater(string[] first, string[] second, string[] third)
	{
		int i = 0, j = 0, k = 0;
		int n = first.Length + second.Length + third.Length;

		int p = 0;
		string[] list = new string[n];
		while (p < n)
		{
			list[p] = first[i];
			p++; i++;
			if (p >= n) break;
			list[p] = second[j];
			p++; j++;
			if (p >= n) break;
			list[p] = third[k];
			p++; k++;
			if (p >= n) break;
		}

		string c = list[0];
		for (i = 1; i < n; i++)
		{
			if (!check(c, list[i]))
			{
				return ((i-1) % 3) + 1;
			}
			c = list[i];
		}
		return -1;
	}

	private bool check(string a, string b)
	{
		if (a.Length + 1 != b.Length)
		{
			return false;
		}

		if (b.Substring(1) != a && b.Substring(0, b.Length -1) != a)
		{
			return false;
		}

		return true;
	}

	// BEGIN KAWIGIEDIT TESTING
	// Generated by KawigiEdit 2.1.4 (beta) modified by pivanof
	#region Testing code generated by KawigiEdit
	[STAThread]
	private static Boolean KawigiEdit_RunTest(int testNum, string[] p0, string[] p1, string[] p2, Boolean hasAnswer, int p3) {
		Console.Write("Test " + testNum + ": [" + "{");
		for (int i = 0; p0.Length > i; ++i) {
			if (i > 0) {
				Console.Write(",");
			}
			Console.Write("\"" + p0[i] + "\"");
		}
		Console.Write("}" + "," + "{");
		for (int i = 0; p1.Length > i; ++i) {
			if (i > 0) {
				Console.Write(",");
			}
			Console.Write("\"" + p1[i] + "\"");
		}
		Console.Write("}" + "," + "{");
		for (int i = 0; p2.Length > i; ++i) {
			if (i > 0) {
				Console.Write(",");
			}
			Console.Write("\"" + p2[i] + "\"");
		}
		Console.Write("}");
		Console.WriteLine("]");
		KidsWordGame obj;
		int answer;
		obj = new KidsWordGame();
		DateTime startTime = DateTime.Now;
		answer = obj.getCheater(p0, p1, p2);
		DateTime endTime = DateTime.Now;
		Boolean res;
		res = true;
		Console.WriteLine("Time: " + (endTime - startTime).TotalSeconds + " seconds");
		if (hasAnswer) {
			Console.WriteLine("Desired answer:");
			Console.WriteLine("\t" + p3);
		}
		Console.WriteLine("Your answer:");
		Console.WriteLine("\t" + answer);
		if (hasAnswer) {
			res = answer == p3;
		}
		if (!res) {
			Console.WriteLine("DOESN'T MATCH!!!!");
		} else if ((endTime - startTime).TotalSeconds >= 2) {
			Console.WriteLine("FAIL the timeout");
			res = false;
		} else if (hasAnswer) {
			Console.WriteLine("Match :-)");
		} else {
			Console.WriteLine("OK, but is it right?");
		}
		Console.WriteLine("");
		return res;
	}
	public static void Main(string[] args) {
		Boolean all_right;
		all_right = true;
		
		string[] p0;
		string[] p1;
		string[] p2;
		int p3;
		
		// ----- test 0 -----
		p0 = new string[]{"e","ello"};
		p1 = new string[]{"el","hello"};
		p2 = new string[]{"ell","ello"};
		p3 = 2;
		all_right = KawigiEdit_RunTest(0, p0, p1, p2, true, p3) && all_right;
		// ------------------
		
		// ----- test 1 -----
		p0 = new string[]{"de","coder","topcoder"};
		p1 = new string[]{"der","pcoder","tipcoder"};
		p2 = new string[]{"oder","opcoder","cheatcoder"};
		p3 = 1;
		all_right = KawigiEdit_RunTest(1, p0, p1, p2, true, p3) && all_right;
		// ------------------
		
		// ----- test 2 -----
		p0 = new string[]{"world","sworld"};
		p1 = new string[]{"word"};
		p2 = new string[]{"sword"};
		p3 = 1;
		all_right = KawigiEdit_RunTest(2, p0, p1, p2, true, p3) && all_right;
		// ------------------
		
		// ----- test 3 -----
		p0 = new string[]{"","abcd"};
		p1 = new string[]{"a"};
		p2 = new string[]{"ab"};
		p3 = 3;
		all_right = KawigiEdit_RunTest(3, p0, p1, p2, true, p3) && all_right;
		// ------------------
		
		// ----- test 4 -----
		p0 = new string[]{""};
		p1 = new string[]{};
		p2 = new string[]{};
		p3 = -1;
		all_right = KawigiEdit_RunTest(4, p0, p1, p2, true, p3) && all_right;
		// ------------------
		
		// ----- test 5 -----
		p0 = new string[]{""};
		p1 = new string[]{""};
		p2 = new string[]{""};
		p3 = 1;
		all_right = KawigiEdit_RunTest(5, p0, p1, p2, true, p3) && all_right;
		// ------------------
		
		// ----- test 6 -----
		p0 = new string[]{"e","wyve","vffwyve","puvffwyvef","bpuvffwyveftl","tbpuvffwyveftlwz","ttbpuvffwyveftlwzcl","ttbpuvffwyveftlwzcletq","uuttbpuvffwyveftlwzcletqh","sjuuttbpuvffwyveftlwzcletqhd","vuysjuuttbpuvffwyveftlwzcletqhd","vjvuysjuuttbpuvffwyveftlwzcletqhdn","qsvjvuysjuuttbpuvffwyveftlwzcletqhdnn","hmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnj","ophmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnjm","ophmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnjmudk","jqophmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnjmudku"};
		p1 = new string[]{"ve","fwyve","uvffwyve","puvffwyveft","bpuvffwyveftlw","ttbpuvffwyveftlwz","ttbpuvffwyveftlwzcle","ttbpuvffwyveftlwzcletqh","uuttbpuvffwyveftlwzcletqhd","ysjuuttbpuvffwyveftlwzcletqhd","jvuysjuuttbpuvffwyveftlwzcletqhd","vjvuysjuuttbpuvffwyveftlwzcletqhdnn","qsvjvuysjuuttbpuvffwyveftlwzcletqhdnnj","hmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnjm","ophmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnjmu","ophmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnjmudku"};
		p2 = new string[]{"yve","ffwyve","uvffwyvef","puvffwyveftl","tbpuvffwyveftlw","ttbpuvffwyveftlwzc","ttbpuvffwyveftlwzclet","uttbpuvffwyveftlwzcletqh","juuttbpuvffwyveftlwzcletqhd","uysjuuttbpuvffwyveftlwzcletqhd","vjvuysjuuttbpuvffwyveftlwzcletqhd","svjvuysjuuttbpuvffwyveftlwzcletqhdnn","mqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnj","phmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnjm","ophmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnjmud","qophmqsvjvuysjuuttbpuvffwyveftlwzcletqhdnnjmudku"};
		p3 = -1;
		all_right = KawigiEdit_RunTest(6, p0, p1, p2, true, p3) && all_right;
		// ------------------
		
		if (all_right) {
			Console.WriteLine("You're a stud (at least on the example cases)!");
		} else {
			Console.WriteLine("Some of the test cases had errors.");
		}

		Console.ReadKey();
	}
	#endregion
	// END KAWIGIEDIT TESTING
}
//Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
