#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#define ldb long double
#define LL long long
const ldb eps = 1e-9;
const ldb EPS = 1e-9;
const ldb LDINF = 1e42;
const int INF = 1 << 28;
const LL LINF = 1LL << 60;
const ldb pi = fabs(atan2(0.0, -1.0));
#define pb push_back
#define y1 y6
#define x1 x6
#define x0 x7
#define PII pair<int, int>
using namespace std;
#define problem ""

//#define DEBUG

void Load()
{
}

const int maxn = 1000005;
LL x[maxn];
inline void genX(LL x0)
{
	x[0] = x0;
	for (int i = 1; i < maxn; i++)
		x[i] = (x[i - 1] * 17235617LL + 4311271LL) % 87465851LL;
}

char board[2][20][20];
int bsts, bstr = -1;
int score;

int cs, cr;

inline int getY(int k, int s)
{
	int y = -1;
	while (y + 1 < 16 && board[k][y + 1][s] == '.')
		y++;
	return y;
}

inline bool place(int s, int r, int cstep, int k)
{
	int c[2] = {0};
	c[0] = x[2 * cstep - 2] % 5 + 1;
	c[1] = x[2 * cstep - 1] % 5 + 1;
	if (r == 0)
	{
		if (s == 7) return false;
		int y1 = getY(k, s);
		int y2 = getY(k, s + 1);
		if (y1 == -1 || y2 == -1) return false;
		board[k][y1][s] = char('0' + c[0]);
		board[k][y2][s + 1] = char('0' + c[1]);
	}
	else if (r == 1)
	{
		int y = getY(k, s);
		if (y < 1) return false;
		board[k][y][s] = char('0' + c[1]);
		board[k][y - 1][s] = char('0' + c[0]);
	}
	else if (r == 2)
	{
		if (s == 0) return false;
		int y1 = getY(k, s);
		int y2 = getY(k, s - 1);
		if (y1 == -1 || y2 == -1) return false;
		board[k][y1][s] = char('0' + c[0]);
		board[k][y2][s - 1] = char('0' + c[1]);
	}
	else if (r == 3)
	{
		int y = getY(k, s);
		if (y < 1) return false;
		board[k][y][s] = char('0' + c[0]);
		board[k][y - 1][s] = char('0' + c[1]);
	}
	return true;
}

const int dx[4] = {1, 0,  0, -1};
const int dy[4] = {0, 1, -1,  0};

int cnt;
vector <PII> comps[maxn];
bool was[20][20];

inline bool good(int x, int y)
{
	return x >= 0 && y >= 0 && x < 16 && y < 8;
}

void go(int k, int x, int y)
{
	comps[cnt].push_back(make_pair(x, y));
	was[x][y] = 1;
	int i;
	for (i = 0; i < 4; i++)
	{
		if (good(x + dx[i],y + dy[i]) && was[x + dx[i]][y + dy[i]] == 0 
		&& board[k][x + dx[i]][y + dy[i]] == board[k][x][y])
		{
			go(k, x + dx[i], y + dy[i]);		
		}
	}
}

void liftDown(int k)
{
	int i, j, l;
	for (i = 0; i < 8; i++)
	{
		for (j = 15; j >= 0; j--)
		{
			if (board[k][j][i] == -1) continue;
			if (board[k][j][i] == '.') continue;
			for (l = j; l < 15; l++)
			{
				if (board[k][l + 1][i] == -1 || board[k][l + 1][i] == '.')
				{
					board[k][l + 1][i] = board[k][l][i];
					board[k][l][i] = -1;
				}
				else
					break;
			}
		}
		for (j = 0; j < 16; j++)
		{
			if (board[k][j][i] == -1)
				board[k][j][i] = '.';
		}
	}
}

inline int f(int k)
{
	if (k == 1) return 0;
	if (k == 2) return 3;
	if (k == 3) return 6;
	if (k == 4) return 12;
	if (k == 5) return 24;
	if (k == 6) return 50;
	return 100;
}

inline int doWave(int k)
{	
	int len = 0;
	int res = 0;
	for (int change = 1; change; len++)
	{
		change = 0;
		memset(was, false, sizeof(was));
		for (int i = 0; i < cnt; i++)
			comps[i].resize(0);
		cnt = 0;
		for (int i = 0; i < 16; i++)
			for (int j = 0; j < 8; j++)
				if (!was[i][j] && '1' <= board[k][i][j] && board[k][i][j] <= '5')
				{
					go(k, i, j);
					cnt++;
				}

		for (int i = 0; i < cnt; i++)
		{
			if (comps[i].size() > 3)
			{
				res += comps[i].size() - 1;
				change = 1;
				for (int j = 0; j < comps[i].size(); j++)
				{
					int x = comps[i][j].first;
					int y = comps[i][j].second;
					board[k][x][y] = -1;
					// killing black cells.
					for (int l = 0; l < 4; l++)
					{
						if (good(x + dx[l], y + dy[l]) && board[k][x + dx[l]][y + dy[l]] == '*')
						{
							board[k][x + dx[l]][y + dy[l]] = -1;
						}
					}
				}
			}	
		}
		// lifting down picture.
		if (change)
		{
			res -= 2;
			liftDown(k);
		}
	}
	if (res <= 0) return 0;
	return res + f(len);
}

bool placeBlack(int cnt, int k)
{
	for (int i = 0, ll = 0; ll < cnt; ll++, i = (i + 1) % 8)
	{
		int j;
		for (j = 0; j < 8; j++)
		{
			if (board[k][0][j] == '.') break;
		}
		if (j == 8) return false;
		j = -1;
		while (j == -1)
		{
			for (j = 0; j < 16 && board[k][j][i] == '.'; j++);
			j--;
			if (j == -1)
			{
				i = (i + 1) % 8;
				continue;
			}
			board[k][j][i] = '*';
			break;
		}	
	}
	return true;
}

void mark(int k, int x, int y)
{
	was[x][y] = 1;
	int i;
	for (i = 0; i < 4; i++)
	{
		if (good(x + dx[i],y + dy[i]) && was[x + dx[i]][y + dy[i]] == 0 
		&& board[k][x + dx[i]][y + dy[i]] == board[k][x][y])
		{
			mark(k, x + dx[i], y + dy[i]);		
		}
	}
}

inline int countComponents(int k)
{
	memset(was, false, sizeof(was));
	int res = 0;
	for (int i = 0; i < 16; i++)
		for (int j = 0; j < 8; j++)
			if (!was[i][j] && ('1' <= board[k][i][j] && board[k][i][j] <= '5'))
			{
				res++;
				mark(k, i, j);
			}
	return res;
}


int countScore(int k, int b)
{
	int cnt = 0, mh = 0, dmh = INF;
	for (int j = 0; j < 8; j++)
	{
		int i;
		for (i = 0; i < 16 && board[k][i][j] == '.'; i++);
		cnt += (16 - i);
		mh = max(mh, 16 - i);
		dmh = min(dmh, 16 - i);
	}
	return cnt * 3 - (dmh - mh) - 5 * b;
}

void rec(int cstep, int dstep, int cb)
{
	if (dstep == 3)
	{
		int cscore = countScore(0, cb);
		#ifdef DEBUG
		cerr << "cur score = " << cscore << endl;
		for (int i = 0; i < 16; i++)
		{
			for (int j = 0; j < 8; j++)
				cerr << board[0][i][j] << " ";
			cerr << endl;	
		}
		cerr << endl;
		#endif
		if (cscore < score)
		{
			score = cscore;
			bsts = cs;
			bstr = cr;
		}
		return;
	}
	char memboard[2][20][20];
	for (int fi = 0; fi < 7; fi++)
		for (int fk = 0; fk < 4; fk++)
		{
			memcpy(memboard[0], board[0], sizeof(board[0]));
			if (!place(fi, fk, cstep, 0)) continue;
			/*for (int si = 0; si < 7; si++)
				for (int sk = 0; sk < 4; sk++)
				{
					memcpy(memboard[1], board[1], sizeof(board[1]));
					if (!place(si, sk, cstep, 1)) continue;
					int b1 = doWave(0);
					int b2 = doWave(1);
					placeBlack(b1, 1);
					placeBlack(b2, 0);
					rec(cstep + 1, dstep + 1);
					memcpy(board[1], memboard[1], sizeof(board[1]));
				}
				*/
			int nb = cb + doWave(0);
			if (dstep == 0)
			{
				cs = fi;
				cr = fk;
			}
			rec(cstep + 1, dstep + 1, nb);
			memcpy(board[0], memboard[0], sizeof(board[0]));
		}
}

void Solve()
{
	char buf[15];
	int x0;
	for (int step = 0; ; step++)
	{
		scanf("%s", buf);
		if (strcmp(buf, "Quit") == 0)
		{
			break;
		}
		else if (strcmp(buf, "Move") == 0)
		{
		
			for (int k = 0; k < 2; k++)
			{
				for (int i = 0; i < 16; i++)
					scanf("%s", board[k][i]);
			}
			score = INF;
			rec(step, 0, 0);
			if (score == INF)
				printf("0 0\n");
			else printf("%d %d\n", bsts, bstr);
		}
		else if (strcmp(buf, "Start") == 0)
		{
			scanf("%d", &x0);
			genX(x0);
			puts("Petrovich v3.14");
		}
		fflush(stdout);
	}
}

int main()
{
#ifdef DEBUG
	freopen("debug.log", "wt", stderr);
#endif	
	Load();
	Solve();
	return 0;
}
