chars = [input() for _ in range(6)] costs = [0, 1, 1, 1, 1, 2] inf = 99999 dp = [(0, 0)] * 6 for i in range(16): new = [None] * 6 for j in range(6): char = ord(chars[j][i]) cost = costs[j] best = inf for prev_char, prev_cost in dp: if prev_char <= char: best = min(best, prev_cost + cost) if char == ord("Q"): char = ord("U") new[j] = (char, best) dp = new ans = inf for j in range(6): _, cost = dp[j] ans = min(ans, cost) if ans >= inf: print("impossible") else: print(ans)