import java.io.BufferedReader;
import java.io.InputStreamReader;

public class jeroenb {
	// well named variable
	static final int INF = 100;
	
	static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

	public static void main(String[] args) throws Exception {
		// Read input
		String[] lines = new String[6];
		for(int i = 0; i < 6; i++)
			lines[i] = in.readLine();

		// DP with index, letter
		int[][] dp = new int[17][26];
		for(int i = 0; i < 26; i++)
			dp[16][i] = 0;
		for (int i = 15; i >= 0; i--) {
			for (int j = 0; j < 26; j++) {
				dp[i][j] = INF;

				for (int k = 0; k < 6; k++) {
					int j2 = lines[k].charAt(i) - 'A';
					int j3 = lines[k].charAt(i) == 'Q' ? 'U' - 'A' : j2;
					int s = k == 0 ? 0 : k == 5 ? 2 : 1;
					if (j <= j2) {
						dp[i][j] = Math.min(dp[i][j], dp[i + 1][j3] + s);
					}
				}
			}
		}

		if (dp[0][0] == INF)
			System.out.println("impossible");
		else
			System.out.println(dp[0][0]);
	}
}
