import os

import sys



import numpy as np

from heapq import heappop, heappush





def solve(h, w, s, t, field):

    WARP = (-(w * 2 + 10), -(w * 2 + 9), -(w * 2 + 8), -(w * 2 + 7), -(w * 2 + 6),

            -(w + 6), -(w + 5), -(w + 3), -(w + 2),

            -2, 2,

            w + 2, w + 3, w + 5, w + 6,

            w * 2 + 6, w * 2 + 7, w * 2 + 8, w * 2 + 9, w * 2 + 10)

    WALK = (-(w + 4), -1, 1, w + 4)



    q = [(0, s)]

    visited = np.zeros((h + 4) * (w + 4), np.int8)

    queued = np.zeros((h + 4) * (w + 4), np.int8)

    queued[s] = 1

    while q:

        cost, v = heappop(q)

        if v == t:

            return cost

        if visited[v]:

            continue

        visited[v] = 1

        for d in WALK:

            u = v + d

            if field[u] or visited[u] or queued[u]:

                continue

            queued[u] = 1

            heappush(q, (cost, u))

        for d in WARP:

            u = v + d

            if field[u] or visited[u] or queued[u]:

                continue

            heappush(q, (cost + 1, u))



    return -1





if sys.argv[-1] == 'ONLINE_JUDGE':

    from numba.pycc import CC



    cc = CC('my_module')

    cc.export('solve', '(i8,i8,i8,i8,i1[:],)')(solve)

    cc.compile()

    exit()



if os.name == 'posix':

    # noinspection PyUnresolvedReferences

    from my_module import solve

else:

    from numba import njit



    solve = njit('(i8,i8,i8,i8,i1[:],)', cache=True)(solve)

    print('compiled', file=sys.stderr)



h, w = map(int, sys.stdin.readline().split())

ch, cw = map(int, sys.stdin.readline().split())

dh, dw = map(int, sys.stdin.readline().split())

field = np.ones((h + 4) * (w + 4), dtype=np.int8)

for i in range(h):

    line = sys.stdin.readline().strip()

    field[(i + 2) * (w + 4) + 2:(i + 2) * (w + 4) + w + 2] = list(map('.#'.index, line))

s = (ch + 1) * (w + 4) + cw + 1

t = (dh + 1) * (w + 4) + dw + 1

ans = solve(h, w, s, t, field)

print(ans)

import os

import sys



import numpy as np

from heapq import heappop, heappush





def solve(h, w, s, t, field):

    WARP = (-(w * 2 + 10), -(w * 2 + 9), -(w * 2 + 8), -(w * 2 + 7), -(w * 2 + 6),

            -(w + 6), -(w + 5), -(w + 3), -(w + 2),

            -2, 2,

            w + 2, w + 3, w + 5, w + 6,

            w * 2 + 6, w * 2 + 7, w * 2 + 8, w * 2 + 9, w * 2 + 10)

    WALK = (-(w + 4), -1, 1, w + 4)



    qc = np.zeros(2000000, dtype=np.int32)

    qv = np.zeros(2000000, dtype=np.int32)

    qv[0] = s

    l, r = 0, 1

    visited = np.zeros((h + 4) * (w + 4), np.int8)

    queued = np.zeros((h + 4) * (w + 4), np.int8)

    queued[s] = 1

    while l < r:

        cost = qc[l]

        v = qv[l]

        l += 1

        if v == t:

            return cost

        if visited[v]:

            continue

        visited[v] = 1

        for d in WALK:

            u = v + d

            if field[u] or visited[u] or queued[u]:

                continue

            queued[u] = 1

            l -= 1

            qc[l] = cost

            qv[l] = u

        for d in WARP:

            u = v + d

            if field[u] or visited[u] or queued[u]:

                continue

            qc[r] = cost + 1

            qv[r] = u

            r += 1



    return -1





if sys.argv[-1] == 'ONLINE_JUDGE':

    from numba.pycc import CC



    cc = CC('my_module')

    cc.export('solve', '(i8,i8,i8,i8,i1[:],)')(solve)

    cc.compile()

    exit()



if os.name == 'posix':

    # noinspection PyUnresolvedReferences

    from my_module import solve

else:

    from numba import njit



    solve = njit('(i8,i8,i8,i8,i1[:],)', cache=True)(solve)

    print('compiled', file=sys.stderr)



h, w = map(int, sys.stdin.readline().split())

ch, cw = map(int, sys.stdin.readline().split())

dh, dw = map(int, sys.stdin.readline().split())

field = np.ones((h + 4) * (w + 4), dtype=np.int8)

for i in range(h):

    line = sys.stdin.readline().strip()

    field[(i + 2) * (w + 4) + 2:(i + 2) * (w + 4) + w + 2] = list(map('.#'.index, line))

s = (ch + 1) * (w + 4) + cw + 1

t = (dh + 1) * (w + 4) + dw + 1

ans = solve(h, w, s, t, field)

print(ans)

import os
import sys
import numpy as np
from heapq import heappop, heappush


def solve(h, w, s, t, field):
    WARP = (
        -(w * 2 + 10),
        -(w * 2 + 9),
        -(w * 2 + 8),
        -(w * 2 + 7),
        -(w * 2 + 6),
        -(w + 6),
        -(w + 5),
        -(w + 3),
        -(w + 2),
        -2,
        2,
        w + 2,
        w + 3,
        w + 5,
        w + 6,
        w * 2 + 6,
        w * 2 + 7,
        w * 2 + 8,
        w * 2 + 9,
        w * 2 + 10,
    )
    WALK = (-(w + 4), -1, 1, w + 4)
    q = [(0, s)]
    visited = np.zeros((h + 4) * (w + 4), np.int8)
    queued = np.zeros((h + 4) * (w + 4), np.int8)
    queued[s] = 1
    while q:
        cost, v = heappop(q)
        if v == t:
            return cost
        if visited[v]:
            continue
        visited[v] = 1
        for d in WALK:
            u = v + d
            if field[u] or visited[u] or queued[u]:
                continue
            queued[u] = 1
            heappush(q, (cost, u))
        for d in WARP:
            u = v + d
            if field[u] or visited[u] or queued[u]:
                continue
            heappush(q, (cost + 1, u))
    return -1


if sys.argv[-1] == "ONLINE_JUDGE":
    from numba.pycc import CC

    cc = CC("my_module")
    cc.export("solve", "(i8,i8,i8,i8,i1[:],)")(solve)
    cc.compile()
    exit()
if os.name == "posix":
    # noinspection PyUnresolvedReferences
    from my_module import solve
else:
    from numba import njit

    solve = njit("(i8,i8,i8,i8,i1[:],)", cache=True)(solve)
    print("compiled", file=sys.stderr)
h, w = map(int, sys.stdin.readline().split())
ch, cw = map(int, sys.stdin.readline().split())
dh, dw = map(int, sys.stdin.readline().split())
field = np.ones((h + 4) * (w + 4), dtype=np.int8)
for i in range(h):
    line = sys.stdin.readline().strip()
    field[(i + 2) * (w + 4) + 2 : (i + 2) * (w + 4) + w + 2] = list(
        map(".#".index, line)
    )
s = (ch + 1) * (w + 4) + cw + 1
t = (dh + 1) * (w + 4) + dw + 1
ans = solve(h, w, s, t, field)
print(ans)

import os
import sys
import numpy as np
from heapq import heappop, heappush


def solve(h, w, s, t, field):
    WARP = (
        -(w * 2 + 10),
        -(w * 2 + 9),
        -(w * 2 + 8),
        -(w * 2 + 7),
        -(w * 2 + 6),
        -(w + 6),
        -(w + 5),
        -(w + 3),
        -(w + 2),
        -2,
        2,
        w + 2,
        w + 3,
        w + 5,
        w + 6,
        w * 2 + 6,
        w * 2 + 7,
        w * 2 + 8,
        w * 2 + 9,
        w * 2 + 10,
    )
    WALK = (-(w + 4), -1, 1, w + 4)
    qc = np.zeros(2000000, dtype=np.int32)
    qv = np.zeros(2000000, dtype=np.int32)
    qv[0] = s
    l, r = 0, 1
    visited = np.zeros((h + 4) * (w + 4), np.int8)
    queued = np.zeros((h + 4) * (w + 4), np.int8)
    queued[s] = 1
    while l < r:
        cost = qc[l]
        v = qv[l]
        l += 1
        if v == t:
            return cost
        if visited[v]:
            continue
        visited[v] = 1
        for d in WALK:
            u = v + d
            if field[u] or visited[u] or queued[u]:
                continue
            queued[u] = 1
            l -= 1
            qc[l] = cost
            qv[l] = u
        for d in WARP:
            u = v + d
            if field[u] or visited[u] or queued[u]:
                continue
            qc[r] = cost + 1
            qv[r] = u
            r += 1
    return -1


if sys.argv[-1] == "ONLINE_JUDGE":
    from numba.pycc import CC

    cc = CC("my_module")
    cc.export("solve", "(i8,i8,i8,i8,i1[:],)")(solve)
    cc.compile()
    exit()
if os.name == "posix":
    # noinspection PyUnresolvedReferences
    from my_module import solve
else:
    from numba import njit

    solve = njit("(i8,i8,i8,i8,i1[:],)", cache=True)(solve)
    print("compiled", file=sys.stderr)
h, w = map(int, sys.stdin.readline().split())
ch, cw = map(int, sys.stdin.readline().split())
dh, dw = map(int, sys.stdin.readline().split())
field = np.ones((h + 4) * (w + 4), dtype=np.int8)
for i in range(h):
    line = sys.stdin.readline().strip()
    field[(i + 2) * (w + 4) + 2 : (i + 2) * (w + 4) + w + 2] = list(
        map(".#".index, line)
    )
s = (ch + 1) * (w + 4) + cw + 1
t = (dh + 1) * (w + 4) + dw + 1
ans = solve(h, w, s, t, field)
print(ans)

-    q = [(0, s)] +    qc = np.zeros(2000000, dtype=np.int32) +    qv = np.zeros(2000000, dtype=np.int32) +    qv[0] = s +    l, r = 0, 1 -    while q: -        cost, v = heappop(q) +    while l < r: +        cost = qc[l] +        v = qv[l] +        l += 1 -            heappush(q, (cost, u)) +            l -= 1 +            qc[l] = cost +            qv[l] = u -            heappush(q, (cost + 1, u)) +            qc[r] = cost + 1 +            qv[r] = u +            r += 1