### BUGGY CODE ###

from collections import deque
#
# H, W, = 4, 4
# CH, CW = 1, 1
# DH, DW = 4, 4
#
# ARR = [
#     "..#.",
#     "..#.",
#     ".#..",
#     ".#.."
# ]
#
#
#
# H,W = 4, 4
# CH, CW = 1, 4
# DH, DW = 4, 1
# ARR = [
#     ".##.",
#     "####",
#     "####",
#     ".##."
# ]
#
#
#
# H,W = 4, 4
# CH,CW = 2, 2
# DH,DW = 3, 3
# ARR = [
#     "....",
#     "....",
#     "....",
#     "...."
# ]
#
#
# H,W = 4, 5
# CH,CW = 1, 2
# DH,DW = 2, 5
#
# ARR = [
#     "#.###",
#     "####.",
#     "#..##",
#     "#..##"
# ]
#
#

H,W = map(int,input().split())
CH,CW = map(int,input().split())
DH,DW = map(int,input().split())
ARR = []

for i in range(H):
    ARR.append(input())




def calculate(h, w, ch, cw, dh, dw, arr):

    status = [[-1 for j in range(w)] for i in range(h)]

    q = deque()

    q.append((ch - 1, cw - 1))
    status[ch - 1][cw - 1] = 0

    while len(q) > 0:
        currentY, currentX = q.popleft()
        childrens = [(currentY, currentX - 1), (currentY + 1, currentX), (currentY, currentX + 1),
                     (currentY - 1, currentX)]

        for child in childrens:
            childY, childX = child
            if childX >= 0 and childX < w and childY >= 0 and childY < h:
                if arr[childY][childX] == ".":
                    if status[childY][childX] == -1:
                        q.append((childY, childX))
                        status[childY][childX] = status[currentY][currentX]
                    else:
                        if status[childY][childX] > status[currentY][currentX]:
                            status[childY][childX] = status[currentY][currentX]
                            q.append((childY, childX))

        for i in range(-2, 3):
            for j in range(-2, 3):
                childY = currentY + i
                childX = currentX + j
                if (childY, childX) in childrens:
                    continue

                if childX >= 0 and childX < w and childY >= 0 and childY < h:
                    if arr[childY][childX] == ".":
                        if status[childY][childX] == -1:
                            status[childY][childX] = (status[currentY][currentX] + 1)
                            q.append((childY, childX))
                        else:
                            if status[childY][childX] > (status[currentY][currentX] + 1):
                                status[childY][childX] = (status[currentY][currentX] + 1)
                                q.append((childY, childX))
    print(status[dh-1][dw-1])
calculate(H,W,CH,CW,DH,DW,ARR)


### FIXED CODE ###

from collections import deque

H, W = map(int, input().split())
C0, C1 = map(int, input().split())
D0, D1 = map(int, input().split())

LARGEST = 999999999
ARR = []

for i in range(H):
    ARR.append(input())


def calculate(h, w, c0, c1, d0, d1, arr):
    c0 = c0 - 1
    c1 = c1 - 1
    d0 = d0 - 1
    d1 = d1 - 1

    status = []
    for i in range(H):
        tmp = [LARGEST] * W
        status.append(tmp)

    status[c0][c1] = 0
    q = deque()

    q.append((c0, c1))

    while len(q) > 0:
        hIndex, wIndex = q.popleft()


        sss = [(hIndex, wIndex - 1), (hIndex, wIndex + 1), (hIndex - 1, wIndex), (hIndex + 1, wIndex)]

        for sH, sW in sss:
            if sH < 0 or sH >= h or sW < 0 or sW >= w:
                continue

            if arr[sH][sW] == '#':
                continue

            if status[sH][sW] > status[hIndex][wIndex]:
                status[sH][sW] = status[hIndex][wIndex]
                q.insert(0, (sH, sW))

        for _hIndex in range(hIndex - 2, hIndex + 3):
            for _wIndex in range(wIndex - 2, wIndex + 3):
                if (_hIndex < 0) or (_hIndex >= h) or (_wIndex < 0) or (_wIndex >= w):
                    continue

                if arr[_hIndex][_wIndex] == '#':
                    continue

                tmpIndex = status[hIndex][wIndex]+1
                if status[_hIndex][_wIndex] > tmpIndex:
                    status[_hIndex][_wIndex] = tmpIndex
                    q.append((_hIndex, _wIndex))

    if status[d0][d1] == LARGEST:
        print(-1)
    else:
        print(status[d0][d1])


calculate(H, W, C0, C1, D0, D1, ARR)