// Arup Guha
// 6/23/2012
// Solution to 2009 Southeast Regional Problem A: Block Game
// Note: There has to be a much more elegant way to write this!!! I stored the board in multiple formats
//       and really should just store the board as a combination of a long (the state of the pieces), and
//       a simple list of which row or column each piece is in.

import java.util.*;

class piece {

	public char letter;
	public boolean vertical;
	public int row;
	public int col;
	public int size;

	// Returns a new piece that is the same as p, moved by offset.
	public piece(piece p, int offset) {

		letter = p.letter;
		vertical = p.vertical;
		size = p.size;

		if (vertical) {
			row = p.row + offset;
			col = p.col;
		}
		else {
			col = p.col + offset;
			row = p.row;
		}
	}

	// For debugging.
	public String toString() {
		return letter+" "+"("+row+", "+col+") ";
	}

	// Creates a piece for c assuming that c is in grid.
	public piece(char[][] grid, char c) {

		int colsize = grid[0].length;
		int n = grid.length*grid[0].length;
		letter = c;

		int[] place = new int[3];
		Arrays.fill(place, -1);
		int cnt = 0;

		// Find piece c and store all cells it's located.
		for (int i=0; i<n; i++) {

			if (grid[i/colsize][i%colsize] == c) {
				place[cnt] = i;

				// Marking the start position.
				if (cnt == 0) {
					row = i/colsize;
					col = i%colsize;
				}
				cnt++;
			}
		}

		// How long the piece is.
		size = cnt;

		// Check if it's vertical or not.
		vertical = false;
		if (place[1] - place[0] == colsize)
			vertical = true;
	}

	// Returns true iff other overlaps with this.
	public boolean conflict(piece other) {

		int[] mine = this.getArrayRep(6);
		int[] theirs = other.getArrayRep(6);

		// Just checking to see if either occupies the same square.
		for (int i=0; i<mine.length; i++)
			for (int j=0; j<theirs.length; j++)
				if (mine[i] == theirs[j])
					return true;
		return false;
	}

	// Returns the physical location of pieces (using 1-D coordinates)
	public int[] getArrayRep(int colsize) {

		int[] ans = new int[size];
		int step = 1;
		if (vertical)
			step = 6;

		// Copy in each array location.
		for (int i=0; i<ans.length; i++) {
			ans[i] = colsize*row + col + i*step;
		}

		return ans;
	}

	// This is all we need to know to locate a piece.
	public int getHashCode() {
		if (vertical)
			return row;
		return col;
	}

	// Put this particular piece in the position value.
	public void setPos(int value) {
		if (vertical)
			row = value;
		else
			col = value;
	}

}

public class a {

	public char remove;
	public int numpieces;
	public piece[] blocks;
	public int iRemove;
	public int NUMROWS;
	public int NUMCOLS;

	// Creates a game object.
	public a(String[] g, char piece) {

		NUMROWS = g.length;
		NUMCOLS = g[0].length();

		// Store the grid and the special piece.
		remove = piece;
		char[][] grid = new char[g.length][];
		for (int i=0; i<g.length; i++)
			grid[i] = g[i].toCharArray();

		int[] freq = new int[26];
		Arrays.fill(freq,0);

		// Figure out how many pieces there are and their names.
		for (int i=0; i<grid.length; i++)
			for (int j=0; j<grid[i].length; j++)
				if (grid[i][j] >= 'A' && grid[i][j] <= 'Z')
					freq[grid[i][j]-'A'] = 1;

		for (int i=0; i<freq.length; i++)
			if (freq[i] == 1)
				numpieces++;

		blocks = new piece[numpieces];

		// Fill in each block, noting where special block is stored.
		int cnt = 0;
		for (int i=0; i<freq.length; i++) {

			if (freq[i] == 1) {

				// Set the block we're removing.
				if ((char)(i+'A') == remove)
					iRemove = cnt;

				blocks[cnt] = new piece(grid, (char)(i+'A'));
				cnt++;
			}
		}
	}

	// For debugging.
	public String toString() {
		String ans = "";

		for (int i=0; i<blocks.length; i++)
			ans = ans + blocks[i] + "\n";
		return ans;
	}

	// Returns piece piecenum shifted over by shift, if it's valid. null is returned otherwise.
	public piece noconflict(int piecenum, int shift) {

		// Create where this piece would move.
		piece temp = new piece(blocks[piecenum], shift);

		// See if the new location conflicts with any of the old.
		for (int i=0; i<numpieces; i++) {

			if (i == piecenum) continue;

			if (temp.conflict(blocks[i]))
				return null;
		}
		return temp;
	}

	// Makes the move by exchanging p for its counterpart in this object.
	public void makeMove(piece p) {

		for (int i=0; i<blocks.length; i++) {
			if (p.letter == blocks[i].letter)
				blocks[i] = p;
		}
	}

	// Returns the piece storing the letter c.
	public piece getPiece(char c) {
		for (int i=0; i<blocks.length; i++)
			if (blocks[i].letter == c)
				return blocks[i];
		return null;
	}

	// Returns a list of all the possible next moves from the current board.
	public ArrayList<piece> possibleMoves() {

		ArrayList<piece> list = new ArrayList<piece>();

		// Go through each piece.
		for (int i=0; i<blocks.length; i++) {

			int curpos = blocks[i].getHashCode();
			int size = blocks[i].size;

			// Go up or Left...
			for (int m=-1; m+curpos>=0; m--) {

				piece trypiece = noconflict(i, m);
				if (trypiece != null) {
					list.add(trypiece);
				}
				else break;
			}

			int limit;
			if (blocks[i].vertical) limit = NUMROWS;
			else limit = NUMCOLS;

			// Go down or right.
			for (int m=1; m+curpos+blocks[i].size<=limit; m++) {

				piece trypiece = noconflict(i, m);
				if (trypiece != null) {
					list.add(trypiece);
				}
				else break;
			}
		}

		return list;
	}

	// Returns true iff this position on the board is a solved position.
	public boolean solved() {
		int limit = NUMCOLS;

		if (blocks[iRemove].vertical) limit = NUMROWS;

		int end = blocks[iRemove].getHashCode() + blocks[iRemove].size;
		return end == limit;
	}

	// This is how we'll store this board position.
	public long getHashCode() {
		long ans = 0;
		for (int i=0; i<blocks.length; i++)
			ans = 6*ans + blocks[i].getHashCode();
		return ans;
	}

	// Given the position code (hash code), we set the pieces up to match it.
	public void setPos(long position) {

		for (int i=blocks.length-1; i>=0; i--) {
			int hash = (int)(position%6);
			position /= 6;
			blocks[i].setPos(hash);
		}
	}

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);

		String special = stdin.next();

		// Process all cases.
		while (!special.equals("*")) {

			char c = special.charAt(0);
			String[] grid = new String[6];
			for (int i=0; i<grid.length; i++)
				grid[i] = stdin.next();

			// Set up our puzzle.
			a puzzle = new a(grid, c);
			long hashCode = puzzle.getHashCode();

			// Store all positions we've gone to before in allPos.
			Hashtable<Long,Integer> allPos = new Hashtable<Long,Integer>();
			allPos.put(hashCode, 0);

			// This is our queue of moves, for the BFS.
			LinkedList<Long> q = new LinkedList<Long>();
			q.offer(hashCode);

			// Run BFS.
			int answer = -1;

			while (q.size() != 0) {

				// Get the next board position out of the queue.
				long nextPos = q.poll();
				int curmoves = allPos.get(nextPos);
				puzzle.setPos(nextPos);

				// Check if this is the solution
				if (puzzle.solved()) {
					answer = curmoves;
					break;
				}

				ArrayList<piece> nextMoves = puzzle.possibleMoves();

				// Add all new board positions.
				for (int i=0; i<nextMoves.size(); i++) {

					piece newp = nextMoves.get(i);
					piece oldp = puzzle.getPiece(newp.letter);

					// Change board.
					puzzle.makeMove(newp);

					// Only add to the queue if this move leads us to a new board position we've never seen.
					long newHash = puzzle.getHashCode();
					if (!allPos.containsKey(newHash)) {
						allPos.put(newHash, curmoves+1);
						q.offer(newHash);
					}

					// Undo move.
					puzzle.makeMove(oldp);
				}
			}

			// Special case where the block is in position to get out, but technically, not out yet.
			if (answer == 0) answer++;

			System.out.println(answer);
			special = stdin.next();
		}

	}
}