#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <set>
using namespace std;
typedef pair<int, int> PII;

struct node
{
	int x, y;
	int z, d;
	node() {}
	node(int a, int b, int c, int D): x(a), y(b), z(c), d(D) {}
};

int dx[5] = {-1,0,1,0,0};
int dy[5] = {0,-1,0,1,0};

int n;
int sx, sy;
char map[25][5][5];

int main()
{
	freopen("DATA5.txt", "r", stdin);
	freopen("OUT5.txt", "w", stdout);

	for (int t=0; t<5; t++)
	{
		cin >> n;
		for (int z=0; z<n; z++)
			for (int x=0; x<5; x++)
				for (int y=0; y<5; y++)
				{
					cin >> map[z][x][y];
					if (map[z][x][y] == 'A')
						sx = x, sy = y;
				}

		queue<node> q;
		q.push(node(sx, sy, 0, 0));
		while (q.size())
		{
			node now = q.front(); q.pop();
			int X=now.x, Y=now.y, Z=now.z, D=now.d;
			if (map[Z][X][Y] == 'B')
			{
				cout << D << endl;
				break;
			}

			for (int A=0; A<5; A++)
			{
				unsigned nx=X+dx[A], ny=Y+dy[A];
				if (Z < n-1 && nx < 5 && ny < 5 && map[Z+1][nx][ny] != '#')
					q.push(node(nx, ny, now.z+1, D+1));
			}
		}
	}
}

