import sys
from collections import defaultdict
from collections import deque


def get_mazes(filepath):
    maze = []
    with open(filepath) as f:
        for line in f:
            line = line.strip()
            if line == "":
                yield maze
                maze = []
            else:
                maze.append(line)
    yield maze


# Directions: (dr, dc)
DIRS = {"U": (-1, 0), "D": (1, 0), "L": (0, -1), "R": (0, 1)}


# Function to check if movement is possible between cells
def can_move(maze, r, c, direction):
    if direction == "U":
        return maze[2 * r][4 * c + 2] == " "
    elif direction == "D":
        return maze[2 * r + 2][4 * c + 2] == " "
    elif direction == "L":
        return maze[2 * r + 1][4 * c] == " "
    elif direction == "R":
        return maze[2 * r + 1][4 * c + 4] == " "
    return False


def dfs(maze, rows, cols, visited, component, r, c):
    visited[r][c] = True
    component.append((r, c))
    for d in DIRS:
        dr, dc = DIRS[d]
        nr, nc = r + dr, c + dc
        if 0 <= nr < rows and 0 <= nc < cols:
            if not visited[nr][nc] and can_move(maze, r, c, d):
                dfs(maze, rows, cols, visited, component, nr, nc)


def get_cell_to_component(components):
    # Let's build a mapping from cell to component index
    cell_to_component = {}
    for idx, comp in enumerate(components):
        for cell in comp:
            cell_to_component[cell] = idx
    return cell_to_component


def get_component_graph(maze, rows, cols, cell_to_component):
    graph = defaultdict(set)

    # Check adjacency of cells in different components
    for r in range(rows):
        for c in range(cols):
            current_comp = cell_to_component[(r, c)]
            for dr, dc in [
                (0, 1),
                (1, 0),
            ]:  # right and down neighbors only to avoid duplicates
                nr, nc = r + dr, c + dc
                if 0 <= nr < rows and 0 <= nc < cols:
                    neighbor_comp = cell_to_component[(nr, nc)]
                    if neighbor_comp != current_comp:
                        # If there's a wall between them, add edge
                        if (
                            not can_move(maze, r, c, "R")
                            and dc == 1
                            or not can_move(maze, r, c, "D")
                            and dr == 1
                        ):
                            graph[current_comp].add(neighbor_comp)
                            graph[neighbor_comp].add(current_comp)

    return graph


# BFS to find shortest path
def bfs_shortest_path(graph, start, end):
    queue = deque([(start, [start])])
    visited = set([start])
    path = []

    while queue:
        node, path = queue.popleft()
        if node == end:
            return path
        for neighbor in graph[node]:
            if neighbor not in visited:
                visited.add(neighbor)
                queue.append((neighbor, path + [neighbor]))
    return None


if len(sys.argv) < 2:
    assert False, "missing filepath"
path = sys.argv[1]

solution = []
for maze in get_mazes(path):
    rows = (len(maze) - 1) // 2
    cols = (len(maze[0]) - 1) // 4

    # DFS to find connected components
    visited = [[False] * cols for _ in range(rows)]
    components = []

    # Find all components
    for r in range(rows):
        for c in range(cols):
            if not visited[r][c]:
                comp = []
                dfs(maze, rows, cols, visited, comp, r, c)
                components.append(comp)

    cell_to_component = get_cell_to_component(components)
    graph = get_component_graph(maze, rows, cols, cell_to_component)

    # Identify start and end components
    start_cell = (0, 0)
    end_cell = (rows - 1, cols - 1)
    start_comp = cell_to_component[start_cell]
    end_comp = cell_to_component[end_cell]

    # Find shortest path
    shortest_path_comps = bfs_shortest_path(graph, start_comp, end_comp)
    solution.append(len(shortest_path_comps) - 1)

print(",".join(map(str, solution)))
