### BUGGY 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))




### 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))