from collections import deque



"""

ダイクストラ　ギリギリ

"""

h, w = list(map(int, input().split()))

ch, cw = list(map(int, input().split()))

dh, dw = list(map(int, input().split()))

ch-=1; cw-=1; dh-=1; dw-=1

area = [list(eval(input())) for _ in range(h)]

inf = 10**100

dist = [[inf]*w for i in range(h)]



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



def Dijkstra(ch, cw):

    import heapq

    edgelist = [(0, ch, cw)]

    heapq.heapify(edgelist)

    dist[ch][cw] = 0

    while len(edgelist):

        cost, y, x = heapq.heappop(edgelist)

        if y==dh and x==dw:

            return dist

        # この枝刈がないと通らない

        if dist[y][x] < cost: continue

        for next_y, next_x in dwalk:

            if 0<=y+next_y<h and 0<=x+next_x<w:

                if area[y+next_y][x+next_x]=='#': continue

                if dist[y+next_y][x+next_x]>cost:

                    dist[y+next_y][x+next_x] = cost

                    heapq.heappush(edgelist, (cost, y+next_y, x+next_x))

        for next_y in range(-2, 3):

            for next_x in range(-2, 3):

                    if 0<=y+next_y<h and 0<=x+next_x<w:

                        if area[y+next_y][x+next_x]=='#': continue

                        if dist[y+next_y][x+next_x]>cost+1:

                            dist[y+next_y][x+next_x] = cost+1

                            heapq.heappush(edgelist, (cost+1, y+next_y, x+next_x))

    return dist



dist = Dijkstra(ch, cw)

if dist[dh][dw] == inf:

    print((-1))

else:

    print((dist[dh][dw]))
from collections import deque



h, w = list(map(int, input().split()))

ch, cw = list(map(int, input().split()))

dh, dw = list(map(int, input().split()))

ch-=1; cw-=1; dh-=1; dw-=1

area = [list(eval(input())) for _ in range(h)]

inf = 10**100

dist = [[inf]*w for i in range(h)]

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



q = deque([(ch, cw)])

dist[ch][cw] = 0

while q:

    y, x = q.popleft()

    for dy, dx in dwalk:

        py = y+dy; px = x+dx

        if not 0<=py<h: continue

        if not 0<=px<w: continue

        if area[py][px]=='#': continue

        if dist[py][px]>dist[y][x]:

            dist[py][px] = dist[y][x]

            q.appendleft((py, px))

    for dy in range(-2, 3):

        for dx in range(-2, 3):

            py = y+dy; px = x+dx

            if not 0<=py<h: continue

            if not 0<=px<w: continue

            if area[py][px]=='#': continue

            if dist[py][px]>dist[y][x]+1:

                dist[py][px] = dist[y][x]+1

                q.append((py, px))



if dist[dh][dw] == inf:

    print((-1))

else:

    print((dist[dh][dw]))
from collections import deque

"""
ダイクストラ　ギリギリ
"""
h, w = list(map(int, input().split()))
ch, cw = list(map(int, input().split()))
dh, dw = list(map(int, input().split()))
ch -= 1
cw -= 1
dh -= 1
dw -= 1
area = [list(eval(input())) for _ in range(h)]
inf = 10**100
dist = [[inf] * w for i in range(h)]
dwalk = [(0, 1), (0, -1), (1, 0), (-1, 0)]


def Dijkstra(ch, cw):
    import heapq

    edgelist = [(0, ch, cw)]
    heapq.heapify(edgelist)
    dist[ch][cw] = 0
    while len(edgelist):
        cost, y, x = heapq.heappop(edgelist)
        if y == dh and x == dw:
            return dist
        # この枝刈がないと通らない
        if dist[y][x] < cost:
            continue
        for next_y, next_x in dwalk:
            if 0 <= y + next_y < h and 0 <= x + next_x < w:
                if area[y + next_y][x + next_x] == "#":
                    continue
                if dist[y + next_y][x + next_x] > cost:
                    dist[y + next_y][x + next_x] = cost
                    heapq.heappush(edgelist, (cost, y + next_y, x + next_x))
        for next_y in range(-2, 3):
            for next_x in range(-2, 3):
                if 0 <= y + next_y < h and 0 <= x + next_x < w:
                    if area[y + next_y][x + next_x] == "#":
                        continue
                    if dist[y + next_y][x + next_x] > cost + 1:
                        dist[y + next_y][x + next_x] = cost + 1
                        heapq.heappush(edgelist, (cost + 1, y + next_y, x + next_x))
    return dist


dist = Dijkstra(ch, cw)
if dist[dh][dw] == inf:
    print((-1))
else:
    print((dist[dh][dw]))

from collections import deque

h, w = list(map(int, input().split()))
ch, cw = list(map(int, input().split()))
dh, dw = list(map(int, input().split()))
ch -= 1
cw -= 1
dh -= 1
dw -= 1
area = [list(eval(input())) for _ in range(h)]
inf = 10**100
dist = [[inf] * w for i in range(h)]
dwalk = [(0, 1), (0, -1), (1, 0), (-1, 0)]
q = deque([(ch, cw)])
dist[ch][cw] = 0
while q:
    y, x = q.popleft()
    for dy, dx in dwalk:
        py = y + dy
        px = x + dx
        if not 0 <= py < h:
            continue
        if not 0 <= px < w:
            continue
        if area[py][px] == "#":
            continue
        if dist[py][px] > dist[y][x]:
            dist[py][px] = dist[y][x]
            q.appendleft((py, px))
    for dy in range(-2, 3):
        for dx in range(-2, 3):
            py = y + dy
            px = x + dx
            if not 0 <= py < h:
                continue
            if not 0 <= px < w:
                continue
            if area[py][px] == "#":
                continue
            if dist[py][px] > dist[y][x] + 1:
                dist[py][px] = dist[y][x] + 1
                q.append((py, px))
if dist[dh][dw] == inf:
    print((-1))
else:
    print((dist[dh][dw]))

-""" -ダイクストラ　ギリギリ -""" - - -def Dijkstra(ch, cw): -    import heapq - -    edgelist = [(0, ch, cw)] -    heapq.heapify(edgelist) -    dist[ch][cw] = 0 -    while len(edgelist): -        cost, y, x = heapq.heappop(edgelist) -        if y == dh and x == dw: -            return dist -        # この枝刈がないと通らない -        if dist[y][x] < cost: +q = deque([(ch, cw)]) +dist[ch][cw] = 0 +while q: +    y, x = q.popleft() +    for dy, dx in dwalk: +        py = y + dy +        px = x + dx +        if not 0 <= py < h: -        for next_y, next_x in dwalk: -            if 0 <= y + next_y < h and 0 <= x + next_x < w: -                if area[y + next_y][x + next_x] == "#": -                    continue -                if dist[y + next_y][x + next_x] > cost: -                    dist[y + next_y][x + next_x] = cost -                    heapq.heappush(edgelist, (cost, y + next_y, x + next_x)) -        for next_y in range(-2, 3): -            for next_x in range(-2, 3): -                if 0 <= y + next_y < h and 0 <= x + next_x < w: -                    if area[y + next_y][x + next_x] == "#": -                        continue -                    if dist[y + next_y][x + next_x] > cost + 1: -                        dist[y + next_y][x + next_x] = cost + 1 -                        heapq.heappush(edgelist, (cost + 1, y + next_y, x + next_x)) -    return dist - - -dist = Dijkstra(ch, cw) +        if not 0 <= px < w: +            continue +        if area[py][px] == "#": +            continue +        if dist[py][px] > dist[y][x]: +            dist[py][px] = dist[y][x] +            q.appendleft((py, px)) +    for dy in range(-2, 3): +        for dx in range(-2, 3): +            py = y + dy +            px = x + dx +            if not 0 <= py < h: +                continue +            if not 0 <= px < w: +                continue +            if area[py][px] == "#": +                continue +            if dist[py][px] > dist[y][x] + 1: +                dist[py][px] = dist[y][x] + 1 +                q.append((py, px))