//Jackson Simoneau
//COP 3503H - Fall 2026
//P4: Block Game

import java.util.*;
public class blockgame {
    //maps states to minimum # of moves from starting state
    public static TreeMap<state, Integer> m;
    public static char c;

    //returns a list of cars in the current state
    public static ArrayList<car> getCars(state grid) {
        ArrayList<car> res = new ArrayList<>();
        for(int i = 0; i < 6; i++) {
            for(int j = 0; j < 6; j++) {
                if(grid.g[i][j] != '.') {
                    int x = i, y = j;
                    char l = grid.g[i][j];
                    int len = 0;
                    for(; j < 6 && grid.g[i][j] == l; j++) len++;
                    j--;
                    if(len < 2) continue;

                    res.add(new car(x, y, len, 1, l));
                }
            }
        }

        for(int j = 0; j < 6; j++) {
            for(int i = 0; i < 6; i++) {
                if(grid.g[i][j] != '.') {
                    int x = i, y = j;
                    char l = grid.g[i][j];
                    int len = 0;
                    for(; i < 6 && grid.g[i][j] == l; i++) len++;
                    i--;
                    if(len < 2) continue;

                    res.add(new car(x, y, len, 0, l));
                }
            }
        }

        return res;
    }

    //constructs grid to verify a valid state
    public static boolean makeGrid(ArrayList<car> cars, char[][] g) {
        for(int i = 0; i < 6; i++)
            for(int j = 0; j < 6; j++) g[i][j] = '.';

        for(car c : cars) {
            int x = c.x, y = c.y;
            for(int i = 0; i < c.len; i++) {
                if(g[x][y] != '.') return false;
                g[x][y] = c.l;
                if(c.dir == 0) x++;
                else y++;
            }
        }
        return true;
    }

    //runs BFS over the possible states of the game
    public static int bfs(state s) {
        //initialize map
        m = new TreeMap<>();
        m.put(s, 0);

        //create queues
        Queue<state> q = new LinkedList<>();
        Queue<state> nq = new LinkedList<>();
        q.offer(s);

        while(!q.isEmpty()) {
            while(!q.isEmpty()) {
                state u = q.poll();
                int d = u.dist;

                //check all moves from this state
                ArrayList<car> cars = getCars(u);
                for(int i = 0; i < cars.size(); i++) {
                    if(cars.get(i).dir == 0) {
                        moveUp(cars, i, d, nq);
                        moveDown(cars, i, d, nq);
                    } else {
                        moveLeft(cars, i, d, nq);
                        //check if the special car can exit
                        if(moveRight(cars, i, d, nq)) return d+1;
                    }
                }
            }

            //swap queues
            while(!nq.isEmpty()) q.offer(nq.poll());
        }

        //no way to reach winning state
        return -1;
    }

    public static void moveDown(ArrayList<car> cars, int idx, int d, Queue<state> q) {
        int x = cars.get(idx).x+1;
        for(; x+cars.get(idx).len <= 6; x++) {
            ArrayList<car> cur = new ArrayList<>();
            car t = new car(cars.get(idx));
            t.x = x;

            for(int i = 0; i < cars.size(); i++) {
                if(i == idx) cur.add(t);
                else cur.add(cars.get(i));
            }

            //check if valid
            char[][] g = new char[6][6];
            if(!makeGrid(cur, g)) return;
            
            state ns = new state(g, d+1);
            if(m.containsKey(ns)) continue;

            m.put(ns, d+1);
            q.offer(ns);
        }
    }

    public static void moveUp(ArrayList<car> cars, int idx, int d, Queue<state> q) {
        int x = cars.get(idx).x-1;
        for(; x >= 0; x--) {
            ArrayList<car> cur = new ArrayList<>();
            car t = new car(cars.get(idx));
            t.x = x;

            for(int i = 0; i < cars.size(); i++) {
                if(i == idx) cur.add(t);
                else cur.add(cars.get(i));
            }

            //check if valid
            char[][] g = new char[6][6];
            if(!makeGrid(cur, g)) return;
            
            state ns = new state(g, d+1);
            if(m.containsKey(ns)) continue;

            m.put(ns, d+1);
            q.offer(ns);
        }
    }

    public static boolean moveRight(ArrayList<car> cars, int idx, int d, Queue<state> q) {
        int y = cars.get(idx).y;
        for(; y+cars.get(idx).len <= 6; y++) {
            ArrayList<car> cur = new ArrayList<>();
            car t = new car(cars.get(idx));
            t.y = y;

            for(int i = 0; i < cars.size(); i++) {
                if(i == idx) cur.add(t);
                else cur.add(cars.get(i));
            }

            //check if valid
            char[][] g = new char[6][6];
            if(!makeGrid(cur, g)) return false;

            //check if special car can exit
            if(y+cars.get(idx).len == 6 && cars.get(idx).l == c) return true;
            
            state ns = new state(g, d+1);
            if(m.containsKey(ns)) continue;

            m.put(ns, d+1);
            q.offer(ns);
        }
        return false;
    }

    public static void moveLeft(ArrayList<car> cars, int idx, int d, Queue<state> q) {
        int y = cars.get(idx).y-1;
        for(; y >= 0; y--) {
            ArrayList<car> cur = new ArrayList<>();
            car t = new car(cars.get(idx));
            t.y = y;

            for(int i = 0; i < cars.size(); i++) {
                if(i == idx) cur.add(t);
                else cur.add(cars.get(i));
            }

            //check if valid
            char[][] g = new char[6][6];
            if(!makeGrid(cur, g)) return;
            
            state ns = new state(g, d+1);
            if(m.containsKey(ns)) continue;

            m.put(ns, d+1);
            q.offer(ns);
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        c = input.next().charAt(0);
        while(c != '*') {
            //read input grid
            char[][] grid = new char[6][6];
            for(int i = 0; i < 6; i++) {
                String st = input.next();
                for(int j = 0; j < 6; j++) grid[i][j] = st.charAt(j);
            }
            state s = new state(grid, 0);

            //display answer from BFS
            System.out.println(bfs(s));

            //read input for next case
            c = input.next().charAt(0);
        }
    }   
}

//stores a 2D character list representing the board state
class state implements Comparable<state> {
    char[][] g;
    int dist;

    public state(char[][] gin, int d) {
        g = new char[6][6];
        dist = d;
        for(int i = 0; i < 6; i++)
            for(int j = 0; j < 6; j++)
                g[i][j] = gin[i][j];
    }

    //compares two states by value
    @Override
    public int compareTo(state o) {
        for(int i = 0; i < 6; i++)
            for(int j = 0; j < 6; j++) {
                if(g[i][j] != o.g[i][j]) return Character.compare(g[i][j], o.g[i][j]);
            }

        return 0;
    }
}

//represents a car on the board
class car {
    char l;
    int x, y, len, dir;

    public car(int x, int y, int len, int dir, char l) {
        this.x = x; this.y = y;
        this.len = len; this.dir = dir;
        this.l = l;
    }

    public car(car o) {
        this.x = o.x; this.y = o.y;
        this.len = o.len; this.dir = o.dir;
        this.l = o.l;
    }

    public String toString() {
        return new String(l + ": (" + x + "," + y + ") " + len + " -> " + dir);
    }
}