#include <cstdlib> 
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
ofstream fout("secret.out");

#ifdef _DEBUG
	#define __OK "OK"
	#define __WA "WA"
	#define __PE "PE"
	#define __FL "FL"
#else
	#define __OK 0
	#define __WA 1
	#define __PE 2
	#define __FL 3
#endif


#define fassert(cond, msg) if (!(cond)) { fout << __FL << endl << msg << endl; fout.close(); exit(1); };
#define wassert(cond, msg) if (!(cond)) { fout << __WA << endl << msg << endl; fout.close(); exit(1); };
#define passert(cond, msg) if (!(cond)) { fout << __PE << endl << msg << endl; fout.close(); exit(1); };

#include "random.h"
#include "shareddata.h"
#include "horse.h"

#include "horses/random_horse.h"
#include "horses/indeterminacy_horse.h"
#include "horses/square_horse.h"
#include "horses/stand_horse.h"
#include "horses/sum_horse.h"
#include "horses/updown_horse.h"
#include "horses/leftright_horse.h"
#include "horses/example_horse.h"

SharedData sh;

const int maxMoves = 10000;
const int MAXN = 30;

int lastHedgehodMoveDX;
int lastHedgehodMoveDY;
int lastHedgehodMoveRequest;

const int exampleHorseX = 2;
const int exampleHorseY = 1;

int horseType;

int requests = 0;
int testType; // 0 - preliminary, 1 - first group, 2 - second group (request limit added)

Horse* horses[] = { new RandomHorse(), new SquareHorse(), new StandHorse(), new IndeterminacyHorse(), 
	new SumDistHorse(), new LeftRightHorse(), new UpDownHorse(), new ExampleHorse() };
int horses_count = 8;

int getHorseMove()
{
	fassert(0 <= horseType && horseType < horses_count, "Illegal horse number");
	return horses[horseType] -> move(sh);
}

bool hasMoved = false;

void getHedgehogMove()
{
	if (horseType == 7 && !hasMoved) {
		for (int i = 1; i <= sh.m; i ++)
			for (int j = 1; j <= sh.n; j ++)
				sh.can[i][j] = 0;
		sh.can[exampleHorseX][exampleHorseY] = 1;
		sh.validpositions = 1;
	}
	hasMoved = true;
	string s;
	getline(cin, s);
	stringstream sin(s);
	passert(sin >> lastHedgehodMoveDX >> lastHedgehodMoveDY >> lastHedgehodMoveRequest, "Not enough tokens in line");
	wassert(abs(lastHedgehodMoveDX) <= 1 && abs(lastHedgehodMoveDY) <= 1, "Illegal move " << lastHedgehodMoveDX << " " << lastHedgehodMoveDY);
	wassert(lastHedgehodMoveRequest == 0 || lastHedgehodMoveRequest == 1, "Illegal request value");
}

char get(int x, int y)
{
	if (x == sh.hedgehogX && y == sh.hedgehogY) 
		if (sh.can[x][y])
			return '@';
		else
			return '*';
	return bool(sh.can[x][y]) + '0';
}


int main()
{
	initrand(2354);
	ios_base::sync_with_stdio(false);
	ifstream fin("horse.in");
	fassert(fin >> horseType, "cannot read horseType");
	fassert(fin >> sh.n >> sh.m, "cannot read N M");
	fassert(fin >> sh.hedgehogX >> sh.hedgehogY, "cannot read hedgehogX hedgehogY");
	fassert(fin >> testType, "cannot read test type");
	fassert(0 <= testType && testType <= 2, "incorrect test type");
	sh.init();
	fassert(2 <= sh.n && sh.n <= MAXN, "wrong N = " << sh.n);
	fassert(2 <= sh.m && sh.m <= MAXN, "wrong M = " << sh.m);
	fassert(1 <= sh.hedgehogX && sh.hedgehogX <= sh.m, "wrong hedgehogX = " << sh.hedgehogX);
	fassert(1 <= sh.hedgehogY && sh.hedgehogY <= sh.n, "wrong hedgehogX = " << sh.hedgehogY);
	cout << sh.n << " " << sh.m << endl;	
	cout << sh.hedgehogX << " " << sh.hedgehogY << endl;
	cout.flush();
	for (int moves = 1; moves <= maxMoves; moves ++)
	{
		getHedgehogMove();
		sh.hedgehogX += lastHedgehodMoveDX;
		sh.hedgehogY += lastHedgehodMoveDY;
		wassert(sh.isValidPosition(sh.hedgehogX, sh.hedgehogY), "Illegal hedgehog move number " << moves);
		bool found = 0;
#ifdef _DEBUG_OUTPUT
		cerr << "Possible horse positions before move " << moves << ":" << endl;
		for (int j = 1; j <= sh.n; j ++)
		{
			for (int i = 1; i <= sh.m; i ++)
				cerr << get(i,j);
			cerr << endl;
		}
		cerr << "Hedgehog move: " << lastHedgehodMoveDX << " " << lastHedgehodMoveDY << " " << lastHedgehodMoveRequest << endl;
#endif // _DEBUG_OUTPUT
		if (lastHedgehodMoveRequest)
		{
			requests ++;
			wassert(testType != 2 || requests <= (sh.n * sh.m), "too many requests");
			found = sh.catchHorse();
		}
		if (found)
		{
			cout << "1 0 0" << endl;
			cout.flush();
			fout << __OK << endl << moves << endl;
			return 0;			
		}
#ifdef _DEBUG_OUTPUT
		cerr << "Possible horse positions after hedgehog move " << moves << ":" << endl;
		for (int j = 1; j <= sh.n; j ++)
		{
			for (int i = 1; i <= sh.m; i ++)
				cerr << get(i,j); 
			cerr << endl;
		}
		cerr << endl;
#endif // _DEBUG_OUTPUT
		int move = getHorseMove();
		cout << 0 << " " << dx[move] << " " << dy[move] << endl;
		cout.flush();
		sh.makeHorseMove(dx[move], dy[move]);
		fassert(sh.validpositions > 0, "Invalid horse move");
#ifdef _DEBUG_OUTPUT
		cerr << "Possible horse positions after horse move " << moves << ":" << endl;
		for (int j = 1; j <= sh.n; j ++)
		{
			for (int i = 1; i <= sh.m; i ++)
				cerr << get(i,j);
			cerr << endl;
		}
		cerr << endl;
#endif //_DEBUG_OUTPUT
	}
	wassert(false,"WA too many moves" << endl); 
	return 0;
};
