# -*- coding: utf-8 -*-

import sys

from collections import defaultdict, deque



H,W=list(map(int, sys.stdin.readline().split()))

CH,CW=list(map(int, sys.stdin.readline().split()))

CH-=1

CW-=1

DH,DW=list(map(int, sys.stdin.readline().split()))

DH-=1

DW-=1

S=[ sys.stdin.readline().strip() for _ in range(H) ]



D=[(0,1),(0,-1),(1,0),(-1,0)]

WARP=[]

for dh in range(-2,3):

    for dw in range(-2,3):

        if abs(dh)+abs(dw)<=1: continue

        WARP.append((dh,dw))



d=defaultdict(lambda: float("inf")) #スタート地点から各マスまでの距離



q=deque()

q.append((0,CH,CW))

d[(CH,CW)]=0



while q:

    kyori, fro_h, fro_w=q.popleft()

    if d[(fro_h, fro_w)]<kyori: continue

    #歩いて移動

    for dh,dw in D:

        to_h, to_w=fro_h+dh, fro_w+dw

        if 0<=to_h<H and 0<=to_w<W and S[to_h][to_w]==".":

            if kyori < d[(to_h, to_w)]:

                d[(to_h, to_w)]=kyori

                q.appendleft((kyori, to_h, to_w))

    #ワープ

    for dh,dw in WARP:

        to_h, to_w=fro_h+dh, fro_w+dw

        if 0<=to_h<H and 0<=to_w<W and S[to_h][to_w]==".":

            if kyori+1 < d[(to_h, to_w)]:

                d[(to_h, to_w)]=kyori+1

                q.append((kyori+1, to_h, to_w))



if d[(DH,DW)]==float("inf"):

    print(-1)

else:

    print(d[(DH,DW)])

# -*- coding: utf-8 -*-

import sys

from collections import defaultdict, deque



H,W=list(map(int, sys.stdin.readline().split()))

CH,CW=list(map(int, sys.stdin.readline().split()))

CH-=1

CW-=1

DH,DW=list(map(int, sys.stdin.readline().split()))

DH-=1

DW-=1

S=[ sys.stdin.readline().strip() for _ in range(H) ]



D=[(0,1),(0,-1),(1,0),(-1,0)]

WARP=[]

for dh in range(-2,3):

    for dw in range(-2,3):

        if abs(dh)+abs(dw)<=1: continue

        WARP.append((dh,dw))



d=defaultdict(lambda: float("inf")) #スタート地点から各マスまでの距離



q=deque()

q.append((0,CH,CW))

d[(CH,CW)]=0



while q:

    kyori, fro_h, fro_w=q.popleft()

    if (fro_h, fro_w)==(DH,DW):

        print(d[(fro_h, fro_w)])

        quit()

    if d[(fro_h, fro_w)]<kyori: continue

    #歩いて移動

    for dh,dw in D:

        to_h, to_w=fro_h+dh, fro_w+dw

        if 0<=to_h<H and 0<=to_w<W and S[to_h][to_w]==".":

            if kyori < d[(to_h, to_w)]:

                d[(to_h, to_w)]=kyori

                q.appendleft((kyori, to_h, to_w))

    #ワープ

    for dh,dw in WARP:

        to_h, to_w=fro_h+dh, fro_w+dw

        if 0<=to_h<H and 0<=to_w<W and S[to_h][to_w]==".":

            if kyori+1 < d[(to_h, to_w)]:

                d[(to_h, to_w)]=kyori+1

                q.append((kyori+1, to_h, to_w))



print(-1)

# -*- coding: utf-8 -*-
import sys
from collections import defaultdict, deque

H, W = list(map(int, sys.stdin.readline().split()))
CH, CW = list(map(int, sys.stdin.readline().split()))
CH -= 1
CW -= 1
DH, DW = list(map(int, sys.stdin.readline().split()))
DH -= 1
DW -= 1
S = [sys.stdin.readline().strip() for _ in range(H)]
D = [(0, 1), (0, -1), (1, 0), (-1, 0)]
WARP = []
for dh in range(-2, 3):
    for dw in range(-2, 3):
        if abs(dh) + abs(dw) <= 1:
            continue
        WARP.append((dh, dw))
d = defaultdict(lambda: float("inf"))  # スタート地点から各マスまでの距離
q = deque()
q.append((0, CH, CW))
d[(CH, CW)] = 0
while q:
    kyori, fro_h, fro_w = q.popleft()
    if d[(fro_h, fro_w)] < kyori:
        continue
    # 歩いて移動
    for dh, dw in D:
        to_h, to_w = fro_h + dh, fro_w + dw
        if 0 <= to_h < H and 0 <= to_w < W and S[to_h][to_w] == ".":
            if kyori < d[(to_h, to_w)]:
                d[(to_h, to_w)] = kyori
                q.appendleft((kyori, to_h, to_w))
    # ワープ
    for dh, dw in WARP:
        to_h, to_w = fro_h + dh, fro_w + dw
        if 0 <= to_h < H and 0 <= to_w < W and S[to_h][to_w] == ".":
            if kyori + 1 < d[(to_h, to_w)]:
                d[(to_h, to_w)] = kyori + 1
                q.append((kyori + 1, to_h, to_w))
if d[(DH, DW)] == float("inf"):
    print(-1)
else:
    print(d[(DH, DW)])

# -*- coding: utf-8 -*-
import sys
from collections import defaultdict, deque

H, W = list(map(int, sys.stdin.readline().split()))
CH, CW = list(map(int, sys.stdin.readline().split()))
CH -= 1
CW -= 1
DH, DW = list(map(int, sys.stdin.readline().split()))
DH -= 1
DW -= 1
S = [sys.stdin.readline().strip() for _ in range(H)]
D = [(0, 1), (0, -1), (1, 0), (-1, 0)]
WARP = []
for dh in range(-2, 3):
    for dw in range(-2, 3):
        if abs(dh) + abs(dw) <= 1:
            continue
        WARP.append((dh, dw))
d = defaultdict(lambda: float("inf"))  # スタート地点から各マスまでの距離
q = deque()
q.append((0, CH, CW))
d[(CH, CW)] = 0
while q:
    kyori, fro_h, fro_w = q.popleft()
    if (fro_h, fro_w) == (DH, DW):
        print(d[(fro_h, fro_w)])
        quit()
    if d[(fro_h, fro_w)] < kyori:
        continue
    # 歩いて移動
    for dh, dw in D:
        to_h, to_w = fro_h + dh, fro_w + dw
        if 0 <= to_h < H and 0 <= to_w < W and S[to_h][to_w] == ".":
            if kyori < d[(to_h, to_w)]:
                d[(to_h, to_w)] = kyori
                q.appendleft((kyori, to_h, to_w))
    # ワープ
    for dh, dw in WARP:
        to_h, to_w = fro_h + dh, fro_w + dw
        if 0 <= to_h < H and 0 <= to_w < W and S[to_h][to_w] == ".":
            if kyori + 1 < d[(to_h, to_w)]:
                d[(to_h, to_w)] = kyori + 1
                q.append((kyori + 1, to_h, to_w))
print(-1)

+    if (fro_h, fro_w) == (DH, DW): +        print(d[(fro_h, fro_w)]) +        quit() -if d[(DH, DW)] == float("inf"): -    print(-1) -else: -    print(d[(DH, DW)]) +print(-1)