### BUGGY CODE ###

import sys
from collections import deque
H, W = map(int, input().split())
sh, sw = map(int, input().split())
sh -= 1
sw -= 1
gh, gw = map(int, input().split())
gh -= 1
gw -= 1

C = []
for i in range(H):
    C.append(list(input()))

d = deque()
kabe = deque()
d.append([sh, sw])
looked = [[0 for i in range(W)]for j in range(H)]
looked[sh][sw] = 1
ans = 0
dx_dy = [[1, 0], [0, 1], [-1, 0], [0, -1]]
kabe.append([sh, sw])
while d:
    x = d.pop()
    lookx = x[0]
    looky = x[1]
    for i in dx_dy:
        a = lookx+i[0]
        b = looky+i[1]
        if not(0 <= a < H) or not(0 <= b < W) or looked[a][b] == 1:
            continue
        if C[a][b] == "#":
            kabe.append([lookx, looky])
            continue
        if a == gh and b == gw:
            print(ans)
            sys.exit()
        d.append([a, b])
        looked[a][b] = 1

    if len(d) == 0:
        ans += 1
        ok = 0
        for _ in range(len(kabe)):
            kx, ky = kabe.pop()
            wapuxm = max(kx-2, 0)
            wapuxM = min(kx+2, H-1)
            wapuym = max(ky-2, 0)
            wapuyM = min(ky+2, W-1)
            for ki in range(wapuxm, wapuxM+1):
                for kj in range(wapuym, wapuyM+1):
                    if C[ki][kj] == "#" or looked[ki][kj] == 1:
                        continue
                    if C[ki][kj] == ".":
                        d.append([ki, kj])
                        looked[ki][kj] = 1
                        ok = 1
                    if ki == gh and kj == gw:
                        print(ans)
                        sys.exit()
        if ok == 0:
            print(-1)
            sys.exit()



### FIXED CODE ###

import sys
from collections import deque
H, W = map(int, input().split())
sh, sw = map(int, input().split())
sh -= 1
sw -= 1
gh, gw = map(int, input().split())
gh -= 1
gw -= 1

C = []
for i in range(H):
    C.append(list(input()))

d = deque()
kabe = deque()
d.append([sh, sw])
looked = [[0 for i in range(W)]for j in range(H)]
looked[sh][sw] = 1
ans = 0
dx_dy = [[1, 0], [0, 1], [-1, 0], [0, -1]]
kabe.append([sh, sw])
while d:
    x = d.pop()
    lookx = x[0]
    looky = x[1]
    for i in dx_dy:
        a = lookx+i[0]
        b = looky+i[1]
        if not(0 <= a < H) or not(0 <= b < W) or looked[a][b] == 1:
            continue
        if C[a][b] == "#":
            kabe.append([lookx, looky])
            continue
        if a == gh and b == gw:
            print(ans)
            sys.exit()
        d.append([a, b])
        looked[a][b] = 1

    if len(d) == 0:
        ans += 1
        ok = 0
        for _ in range(len(kabe)):
            kx, ky = kabe.pop()
            wapuxm = max(kx-2, 0)
            wapuxM = min(kx+2, H-1)
            wapuym = max(ky-2, 0)
            wapuyM = min(ky+2, W-1)
            for ki in range(wapuxm, wapuxM+1):
                for kj in range(wapuym, wapuyM+1):
                    if C[ki][kj] == "#" or looked[ki][kj] == 1:
                        continue
                    if C[ki][kj] == ".":
                        d.append([ki, kj])
                        looked[ki][kj] = 1
                        ok = 1
                    if ki == gh and kj == gw:
                        print(ans)
                        sys.exit()
        if ok == 0:
            print(-1)
            sys.exit()