import sys from collections import deque def main(): H, W, r_a, c_a, r_b, c_b = map(int, sys.stdin.readline().split()) grid = [] for _ in range(H): grid.append(sys.stdin.readline().strip()) # Directions: up, down, left, right directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # BFS Initialization queue = deque() initial_state = (r_a - 1, c_a - 1, r_b - 1, c_b - 1) queue.append((initial_state[0], initial_state[1], initial_state[2], initial_state[3], 0)) visited = set() visited.add((initial_state[0], initial_state[1], initial_state[2], initial_state[3])) target = (r_b - 1, c_b - 1, r_a - 1, c_a - 1) while queue: x1, y1, x2, y2, steps = queue.popleft() # Check if current state is the target if (x1, y1, x2, y2) == target: print(steps) return # Generate all possible moves for cow for dx, dy in directions: new_x1 = x1 + dx new_y1 = y1 + dy if 0 <= new_x1 < H and 0 <= new_y1 < W: if grid[new_x1][new_y1] != '#' and (new_x1, new_y1) != (x2, y2): new_state = (new_x1, new_y1, x2, y2) if new_state not in visited: visited.add(new_state) queue.append((new_x1, new_y1, x2, y2, steps + 1)) # Generate all possible moves for girl for dx, dy in directions: new_x2 = x2 + dx new_y2 = y2 + dy if 0 <= new_x2 < H and 0 <= new_y2 < W: if grid[new_x2][new_y2] != '#' and (new_x2, new_y2) != (x1, y1): new_state = (x1, y1, new_x2, new_y2) if new_state not in visited: visited.add(new_state) queue.append((x1, y1, new_x2, new_y2, steps + 1)) # If target is not reachable print(-1) if __name__ == "__main__": main()