// Arup Guha
// 10/10/2026
// Solution to COP 3503 Program 4: Block Game

// Note: I wanted to rewrite this, to see how different my implementation would
//       be this many years later.

import java.util.*;

/*** We'll store each game state as a String of digits.
     The digit in index i means that letter block is at position state[i].
	 A position is defined as either a row or a column, depending on which
	 way that block can move. If the row is fixed, the position is the 
	 minimum column number etc.
***/

public class blockgame {
	
	final public static int N = 6;

	// dir[i] stores true if block i is moving horizontal, false otherwise.
	public static boolean[] dir;
	
	// size[i] = Stores size of block i.
	public static int[] size;
	
	// Stores either fixed row or column of each piece.
	public static int[] fixedIdx;
	
	// This stores the item to extract.
	public static int keyItem;
	
	// Stores the start state.
	public static String start;
	
	// Number of cars.
	public static int nCars;
	
	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		char item = stdin.next().charAt(0);
		
		// Process cases.
		while (item != '*') {
		
			// Get grid.
			char[][] grid = new char[6][];
			for (int i=0; i<6; i++)
				grid[i] = stdin.next().toCharArray();
			
			// Update our shared variables.
			updateData(grid, item);
			
			// Run it!
			System.out.println(bfs());
			
			// Get next.
			item = stdin.next().charAt(0);
		}
	}
	
	// Runs the BFS on the game and returns the fewest number of moves to get the
	// the main car out or -1 if it's not possible.
	public static int bfs() {
		
		// Set up distance map.
		HashMap<String,Integer> dist = new HashMap<String,Integer>();
		dist.put(start, 0);
		
		// Queue for BFS.
		ArrayDeque<String> q = new ArrayDeque<String>();
		q.offer(start);
		
		// Run BFS.
		while (q.size() > 0) {
			
			// Get the current state of the board.
			String curS = q.poll();
			
			// We made it!
			if (done(curS)) return Math.max(1, dist.get(curS));
			
			// Get all of the next possible positions.
			ArrayList<String> next = getNext(curS);
			
			// Go through these.
			for (String item: next) {
				
				// Don't do this again.
				if (dist.containsKey(item)) continue;
				
				// Update the map.
				dist.put(item, dist.get(curS)+1);
				q.offer(item);
			}
		}
		
		// Never got there.
		return -1;	
	}
	
	// Returns true iff state is a state where the car got out.
	public static boolean done(String state) {
		
		// We we need our car to be, to be out.
		int needLoc = N - size[keyItem];
		
		// Check if it's there.
		return state.charAt(keyItem) - '0' == needLoc;
	}
	
	// Returns all possible next states from state.
	public static ArrayList<String> getNext(String state) {
		
		ArrayList<String> res = new ArrayList<String>();
		
		// i is the piece we are moving.
		for (int i=0; i<nCars; i++) {
			
			// newL is where we want to try to move this piece.
			for (int newL=0; newL<=N-size[i]; newL++) {
			
				// Not a move.
				if (newL == state.charAt(i) - '0') continue;
				
				// All of the indexes that have to be "free" for this move.
				int minIdx = Math.min(newL, state.charAt(i) - '0');
				int maxIdx = Math.max(newL + size[i]-1, state.charAt(i) - '0' + size[i]-1);
				
				// If not, we can't get here.
				if (!free(i, minIdx, maxIdx, state)) continue;
				
				// Compute new state.
				char[] newState = new char[nCars];
				for (int z=0; z<nCars; z++) {
					if (z == i)
						newState[z] = (char)(newL + '0');
					else
						newState[z] = state.charAt(z);
				}
				res.add(new String(newState));
			}
		}
		
		// All the new states.
		return res;
	}
	
	// Returns true if each piece in state (except for piece i) does NOT 
	// conflict with all of the squares piece i could slide to.
	public static boolean free(int i, int minIdx, int maxIdx, String state) {
			
		// Loop through each cell.
		for (int z=minIdx; z<=maxIdx; z++) {
				
			// Loop through each piece that isn't piece i.
			for (int other=0; other<nCars; other++) {
				if (other == i) continue;
				
				// Changes which piece we check so make the appropriate call.
				if (dir[i]) {
					if (conflict(fixedIdx[i], z, state, other)) return false;
				}
				else {
					if (conflict(z, fixedIdx[i], state, other)) return false;
				}						
			}
		}
		
		// It's good.
		return true;
	}
	
	// Returns true if the car with index sIdx in state resides in myR, myC.
	public static boolean conflict(int myR, int myC, String state, int sIdx) {
		
		// Where I am.
		int tmp = state.charAt(sIdx)-'0';		
		
		// Horizontal check for each tile of piece sIdx.
		if (dir[sIdx]) {
			for (int i=tmp; i<tmp+size[sIdx]; i++)
				if (myR == fixedIdx[sIdx] && myC == i)
					return true;
		}
		
		// Vertical check.
		else {
			
			for (int i=tmp; i<tmp+size[sIdx]; i++)
				if (myR == i && myC == fixedIdx[sIdx])
					return true;
		}
		
		// No conflict if we get here.
		return false;
	}
	
	// Updates the data for this case.
	public static void updateData(char[][] grid, char item) {
		
		TreeSet<Integer> all = new TreeSet<Integer>();
		for (int i=0; i<grid.length; i++)
			for (int j=0; j<grid[0].length; j++)
				if (grid[i][j] >= 'A' && grid[i][j] <= 'Z')
					all.add(grid[i][j]-'A');
					
		// Store letters in order here.
		int idx = 0;
		int[] map = new int[26];
		Arrays.fill(map, -1);
		while (all.size() > 0) map[all.pollFirst()] = idx++;
		
		// Assign this.
		nCars = idx;
		
		// Assign the key item.
		keyItem = map[item-'A'];
		
		// Set up arrays that store the fixed state.
		dir = new boolean[idx];
		size = new int[idx];
		char[] tmpStart = new char[idx];
		fixedIdx = new int[idx];
		
		// Will store if we've assigned info for this block or not.
		boolean[] assigned = new boolean[idx];
		
		// Go through the grid.
		for (int i=0; i<grid.length; i++) {
			for (int j=0; j<grid[0].length; j++) {
			
				// Not a tile.
				if (grid[i][j] == '.') continue;
				
				// We did this one already.
				int myIdx = map[grid[i][j] - 'A'];
				if (assigned[myIdx]) continue;
				
				// Mark it.
				assigned[myIdx] = true;
				
				// Horizontal.
				if (j+1<grid[0].length && grid[i][j+1] == grid[i][j]) {
					dir[myIdx] = true;
					size[myIdx] = (j+2<grid[0].length && grid[i][j+2] == grid[i][j]) ? 3 : 2;
					tmpStart[myIdx] = (char)(j+'0');
					fixedIdx[myIdx] = i;
				}
				
				// Vertical.
				else {
					dir[myIdx] = false;
					size[myIdx] = (i+2<grid.length && grid[i+2][j] == grid[i][j]) ? 3 : 2;
					tmpStart[myIdx] = (char)(i+'0');
					fixedIdx[myIdx] = j;
				}
			}
		}
				
		// Last item to assign.
		start = new String(tmpStart);
	}
}

