### BUGGY CODE ###

# coding: utf-8
from functools import reduce
from queue import Queue

H, W = list(map(int, input().split(" ")))
current_h, current_w = list(map(int, input().split(" ")))
current_h -= 1
current_w -= 1
goal_h, goal_w = list(map(int, input().split(" ")))
goal_h -= 1
goal_w -= 1

table = [list(input()) for _ in range(H)]


class DFS:

    def __init__(self, table, current_h, current_w):
        self.table = table
        self.current_h = current_h
        self.current_w = current_w
        self.islands = []
        self.start_island_index = -1
        self.end_island_index = -1

    def delta(self, h, w):
        return [(h+1, w), (h, w-1), (h, w), (h, w+1), (h-1, w)]

    def dfs(self, start_h, start_w):
        next = self.delta(start_h, start_w)
        island = [(start_h, start_w)]
        if start_h == self.current_h and start_w == self.current_w:
            self.start_island_index = len(self.islands)

        for h, w in next:
            if h == self.current_h and w == self.current_w:
                self.start_island_index = len(self.islands)
            if h == goal_h and w == goal_w:
                self.end_island_index = len(self.islands)
            if not(0 <= h < H and 0 <= w < W):
                continue
            elif self.table[h][w] == "#":
                continue
            else:
                island.append((h, w))
                self.table[h][w] = "#"
                next.extend(self.delta(h, w))
        self.islands.append(island)

    def make_islands(self):
        for h in range(H):
            for w in range(W):
                if self.table[h][w] == ".":
                    self.dfs(h, w)

    def delta_five(self, pos):
        delta = []
        h = pos[0]
        w = pos[1]
        for dh in range(-2, 3):
            for dw in range(-2, 3):
                if dh == 0 and dw == 0:
                    continue
                delta.append((h+dh, w+dw))
        return delta

    def make_islands_graph(self):
        islands_graph = []
        for start_island_index in range(len(self.islands)):
            for end_island_index in range(len(self.islands)):
                if start_island_index == end_island_index:
                    continue
                if self.is_connect(self.islands[start_island_index], self.islands[end_island_index]):
                    islands_graph.append((start_island_index, end_island_index))
        return islands_graph

    def is_connect(self, islands_a, islands_b):
        a = set(reduce(lambda a, b: a + b, map(self.delta_five, islands_a)))
        for h, w in islands_b:
            if (h, w) in a:
                return True
        return False

class BFS:

    def __init__(self, length_of_islands, graph, start_island_index, end_island_index):
        self.islands = [0 for i in range(0, length_of_islands)]
        self.graph = graph
        self.start = start_island_index
        self.end = end_island_index

    def bfs(self, start, end):
        q = Queue()
        q.put(start)
        count = 0
        while not q.empty():
            now = q.get()

            if now == end:
                return count
            if self.islands[now] == -1:
                continue
            for s, e in self.graph:
                if self.islands[s] == -1:
                    continue
                if s == now:
                    q.put(e)
            self.islands[now] = -1
            count += 1
        return -1

dfs = DFS(table, current_h, current_w)
dfs.make_islands()
graph = dfs.make_islands_graph()
bfs = BFS(len(dfs.islands), graph, dfs.start_island_index, dfs.end_island_index)
print(bfs.bfs(dfs.start_island_index, dfs.end_island_index))



### FIXED CODE ###

# coding: utf-8
from functools import reduce
from queue import Queue
from collections import deque
H, W = list(map(int, input().split(" ")))
current_h, current_w = list(map(int, input().split(" ")))
current_h -= 1
current_w -= 1
goal_h, goal_w = list(map(int, input().split(" ")))
goal_h -= 1
goal_w -= 1

table = [list(input()) for _ in range(H)]

class ZeroOneBFS:

    def __init__(self, table):
        self.table = table
        self.cost_table = []
        for _ in range(H):
            self.cost_table.append([-1 for _ in range(W)])

    def delta(self, h, w):
        res = [(h+1, w),
               (h, w-1), (h, w+1),
               (h-1, w)]
        return res

    def warp(self, h, w):
        res = []
        for dh in range(-2, 3):
            for dw in range(-2, 3):
                if dh == 0 and dw == 0:
                    pass
                else:
                    res.append((h+dh, w+dw))
        return res

    def bfs(self, start_h, start_w):
        dq = deque()
        dq.appendleft((start_h, start_w, 0))
        while dq:
            now_h, now_w, now_c = dq.pop()
            if self.table[now_h][now_w] == "#" or self.cost_table[now_h][now_w] != -1:
                pass
            else:
                self.cost_table[now_h][now_w] = now_c
                for h, w in self.delta(now_h, now_w):
                    if 0 <= h < H and 0 <= w < W and self.table[h][w] != "#":
                        dq.append((h, w, now_c))
                for h, w in self.warp(now_h, now_w):
                    if 0 <= h < H and 0 <= w < W and self.table[h][w] != "#":
                        dq.appendleft((h, w, now_c+1))
        return self.cost_table[goal_h][goal_w]


bfs = ZeroOneBFS(table)
print(bfs.bfs(current_h, current_w))