### BUGGY CODE ###

import sys
from collections import deque

def main():

    input = sys.stdin.readline

    H,W = map(int,input().split())
    Ch,Cw = map(int,input().split())
    Dh,Dw = map(int,input().split())

    S=[list(map(str,list(input()[:-1]))) for i in range(H)]

    dh = (1, 0, -1, 0)
    dw = (0, 1, 0, -1)
    warph = (2, 2, 2, 2 ,2,1,1,1,1,0,0,-1,-1,-1,-1,-2,-2,-2,-2,-2)
    warpw = (-2, -1, 0, 1,2,-2,-1,1,2,-2,2,-2,-1,1,2,-2,-1,0,1,2)

    d = deque()

    d.append((Ch-1,Cw-1))

    S[Ch-1][Cw-1] = 0

    while len(d) > 0:
        Ph,Pw = d.popleft()

        # if (Ph == Dh-1) & (Pw == Dw-1):
        #     goal = True
        #     break

        for i in range(4):
            nh = Ph + dh[i]
            nw = Pw + dw[i]
            if (nh < 0) | (nh >= H) | (nw < 0) | (nw >= W):
                continue
            if S[nh][nw] == "#":
                continue
            if S[nh][nw] == ".":
                S[nh][nw] = S[Ph][Pw]
                d.appendleft((nh,nw))
            elif S[nh][nw] > S[Ph][Pw]:
                S[nh][nw] = min(S[nh][nw],S[Ph][Pw])
                d.appendleft((nh,nw))





        for i in range(20):
            nh = Ph + warph[i]
            nw = Pw + warpw[i]
            if (nh < 0) | (nh >= H) | (nw < 0) | (nw >= W):
                continue
            if S[nh][nw] == "#":
                continue
            if S[nh][nw] == ".":
                S[nh][nw] = S[Ph][Pw] + 1
                d.append((nh,nw))
            elif S[nh][nw] > S[Ph][Pw] + 1:
                S[nh][nw] = min(S[nh][nw],S[Ph][Pw]+1)
                d.append((nh,nw))

    if S[Dh-1][Dw-1] == ".":
        print("-1")
    else:
        print(S[Dh-1][Dw-1])
         

if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys
from collections import deque

def main():

    input = sys.stdin.readline

    H,W = map(int,input().split())
    Ch,Cw = map(int,input().split())
    Dh,Dw = map(int,input().split())

    S=[list(map(str,list(input()[:-1]))) for i in range(H)]

    dh = (1, 0, -1, 0)
    dw = (0, 1, 0, -1)
    warph = (2, 2, 2, 2 ,2,1,1,1,1,0,0,-1,-1,-1,-1,-2,-2,-2,-2,-2)
    warpw = (-2, -1, 0, 1,2,-2,-1,1,2,-2,2,-2,-1,1,2,-2,-1,0,1,2)

    d = deque()

    d.append((Ch-1,Cw-1))

    S[Ch-1][Cw-1] = 0

    while len(d) > 0:
        Ph,Pw = d.popleft()

        # if (Ph == Dh-1) & (Pw == Dw-1):
        #     goal = True
        #     break

        for i in range(4):
            nh = Ph + dh[i]
            nw = Pw + dw[i]
            if (nh < 0) | (nh >= H) | (nw < 0) | (nw >= W):
                continue
            if S[nh][nw] == "#":
                continue
            if S[nh][nw] == ".":
                S[nh][nw] = S[Ph][Pw]
                d.appendleft((nh,nw))
            elif S[nh][nw] > S[Ph][Pw]:
                S[nh][nw] = min(S[nh][nw],S[Ph][Pw])
                d.appendleft((nh,nw))





        for i in range(20):
            nh = Ph + warph[i]
            nw = Pw + warpw[i]
            if (nh < 0) | (nh >= H) | (nw < 0) | (nw >= W):
                continue
            if S[nh][nw] == "#":
                continue
            if S[nh][nw] == ".":
                S[nh][nw] = S[Ph][Pw] + 1
                d.append((nh,nw))
            elif S[nh][nw] > S[Ph][Pw] + 1:
                S[nh][nw] = min(S[nh][nw],S[Ph][Pw]+1)
                d.append((nh,nw))

    if S[Dh-1][Dw-1] == ".":
        print("-1")
    else:
        print(S[Dh-1][Dw-1])
         

if __name__ == "__main__":
    main()