max2 = lambda x,y: x if x > y else y

min2 = lambda x,y: x if x < y else y



import sys

from itertools import count

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline





MOVES = [

    ((1, 0), [(2,-1),(2,0),(2,1),(2,2),(1,1)]),

    ((-1, 0),[(-2,-1),(-2,0),(-2,1),(-2,-2),(-1,-1)]),

    ((0, 1), [(1,2),(0,2),(-1,2),(-2,2),(-1,1)]),

    ((0, -1), [(1,-2),(0,-2),(-1,-2),(2,-2),(1,-1)]),

    ]



CONVERTED_MOVES = None



def solve(dist, start, goal):

    q = [start]

    cnt = 0

    while q:

        nq = []

        while q:

            x = q.pop()

            if x == goal:

                return cnt

            if dist[x] < cnt:

                continue

            for d,l in CONVERTED_MOVES:

                nx = x+d

                if dist[nx] > cnt:

                    dist[nx] = cnt

                    q.append(nx)

                if dist[nx] < 0:

                    for t in l:

                        p = x+t

                        if dist[p] > cnt+1:

                            dist[p] = cnt+1

                            nq.append(p)

        q = nq

        cnt += 1



    return -1









if __name__ == '__main__':

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

    start = tuple([int(i)+1 for i in readline().split()])

    goal = tuple([int(i)+1 for i in readline().split()])



    H += 4

    W += 4



    dist = [-1]*(W*H)



    start = start[0]*W+start[1]

    goal = goal[0]*W+goal[1]



    for t,s in zip(count(2*W,W),read().split()):

        for i,c in zip(count(t+2),s.decode()):

            if c == '.':

                dist[i] = 10**7



    convert = lambda x,y: x*W+y

    CONVERTED_MOVES = []

    for d,l in MOVES:

        d = convert(*d)

        l = [convert(*d) for d in l]

        CONVERTED_MOVES.append((d,l))



    # print(CONVERTED_MOVES)



    print((solve(dist,start,goal)))

import sys

from itertools import count

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline





MOVES = [

    ((1, 0), [(2,-1),(2,0),(2,1),(2,2),(1,1)]),

    ((-1, 0),[(-2,-1),(-2,0),(-2,1),(-2,-2),(-1,-1)]),

    ((0, 1), [(1,2),(0,2),(-1,2),(-2,2),(-1,1)]),

    ((0, -1), [(1,-2),(0,-2),(-1,-2),(2,-2),(1,-1)]),

    ]



CONVERTED_MOVES = None



def solve(dist, start, goal):

    q = [start]

    cnt = 0

    while q:

        nq = []

        while q:

            x = q.pop()

            if x == goal:

                return cnt

            if dist[x] < cnt:

                continue

            for d,l in CONVERTED_MOVES:

                nx = x+d

                if dist[nx] > cnt:

                    dist[nx] = cnt

                    q.append(nx)

                if dist[nx] < 0:

                    for t in l:

                        p = x+t

                        if dist[p] > cnt+1:

                            dist[p] = cnt+1

                            nq.append(p)

        q = nq

        cnt += 1



    return -1









if __name__ == '__main__':

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

    start = tuple([int(i)+1 for i in readline().split()])

    goal = tuple([int(i)+1 for i in readline().split()])



    H += 4

    W += 4



    dist = [-1]*(W*H)



    start = start[0]*W+start[1]

    goal = goal[0]*W+goal[1]



    for t,s in zip(count(2*W,W),read().split()):

        for i,c in zip(count(t+2),s.decode()):

            if c == '.':

                dist[i] = 10**7



    convert = lambda x,y: x*W+y

    CONVERTED_MOVES = []

    for d,l in MOVES:

        d = convert(*d)

        l = [convert(*d) for d in l]

        CONVERTED_MOVES.append((d,l))



    print((solve(dist,start,goal)))

max2 = lambda x, y: x if x > y else y
min2 = lambda x, y: x if x < y else y
import sys
from itertools import count

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
MOVES = [
    ((1, 0), [(2, -1), (2, 0), (2, 1), (2, 2), (1, 1)]),
    ((-1, 0), [(-2, -1), (-2, 0), (-2, 1), (-2, -2), (-1, -1)]),
    ((0, 1), [(1, 2), (0, 2), (-1, 2), (-2, 2), (-1, 1)]),
    ((0, -1), [(1, -2), (0, -2), (-1, -2), (2, -2), (1, -1)]),
]
CONVERTED_MOVES = None


def solve(dist, start, goal):
    q = [start]
    cnt = 0
    while q:
        nq = []
        while q:
            x = q.pop()
            if x == goal:
                return cnt
            if dist[x] < cnt:
                continue
            for d, l in CONVERTED_MOVES:
                nx = x + d
                if dist[nx] > cnt:
                    dist[nx] = cnt
                    q.append(nx)
                if dist[nx] < 0:
                    for t in l:
                        p = x + t
                        if dist[p] > cnt + 1:
                            dist[p] = cnt + 1
                            nq.append(p)
        q = nq
        cnt += 1
    return -1


if __name__ == "__main__":
    H, W = list(map(int, readline().split()))
    start = tuple([int(i) + 1 for i in readline().split()])
    goal = tuple([int(i) + 1 for i in readline().split()])
    H += 4
    W += 4
    dist = [-1] * (W * H)
    start = start[0] * W + start[1]
    goal = goal[0] * W + goal[1]
    for t, s in zip(count(2 * W, W), read().split()):
        for i, c in zip(count(t + 2), s.decode()):
            if c == ".":
                dist[i] = 10**7
    convert = lambda x, y: x * W + y
    CONVERTED_MOVES = []
    for d, l in MOVES:
        d = convert(*d)
        l = [convert(*d) for d in l]
        CONVERTED_MOVES.append((d, l))
    # print(CONVERTED_MOVES)
    print((solve(dist, start, goal)))

import sys
from itertools import count

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
MOVES = [
    ((1, 0), [(2, -1), (2, 0), (2, 1), (2, 2), (1, 1)]),
    ((-1, 0), [(-2, -1), (-2, 0), (-2, 1), (-2, -2), (-1, -1)]),
    ((0, 1), [(1, 2), (0, 2), (-1, 2), (-2, 2), (-1, 1)]),
    ((0, -1), [(1, -2), (0, -2), (-1, -2), (2, -2), (1, -1)]),
]
CONVERTED_MOVES = None


def solve(dist, start, goal):
    q = [start]
    cnt = 0
    while q:
        nq = []
        while q:
            x = q.pop()
            if x == goal:
                return cnt
            if dist[x] < cnt:
                continue
            for d, l in CONVERTED_MOVES:
                nx = x + d
                if dist[nx] > cnt:
                    dist[nx] = cnt
                    q.append(nx)
                if dist[nx] < 0:
                    for t in l:
                        p = x + t
                        if dist[p] > cnt + 1:
                            dist[p] = cnt + 1
                            nq.append(p)
        q = nq
        cnt += 1
    return -1


if __name__ == "__main__":
    H, W = list(map(int, readline().split()))
    start = tuple([int(i) + 1 for i in readline().split()])
    goal = tuple([int(i) + 1 for i in readline().split()])
    H += 4
    W += 4
    dist = [-1] * (W * H)
    start = start[0] * W + start[1]
    goal = goal[0] * W + goal[1]
    for t, s in zip(count(2 * W, W), read().split()):
        for i, c in zip(count(t + 2), s.decode()):
            if c == ".":
                dist[i] = 10**7
    convert = lambda x, y: x * W + y
    CONVERTED_MOVES = []
    for d, l in MOVES:
        d = convert(*d)
        l = [convert(*d) for d in l]
        CONVERTED_MOVES.append((d, l))
    print((solve(dist, start, goal)))

-max2 = lambda x, y: x if x > y else y -min2 = lambda x, y: x if x < y else y -    # print(CONVERTED_MOVES)