### BUGGY CODE ###

from collections import defaultdict,deque
def main():
    H, W = map(int, input().split())
    S = list(map(int, input().split()))
    G = list(map(int, input().split()))
    S[0] -= 1
    S[1] -= 1
    G[0] -= 1
    G[1] -= 1
    c = [input() for i in range(H)]
    dist = [[-1]*W for _ in range(H)]

    for i in range(H):
        for g in range(W):
            if c[i][g] == "#":
                dist[i][g] = -2

    checkList = set()
    que = deque()
    que.append(S)
    dist[S[0]][S[1]] = 0

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

    cost = 0

    while True:
        if len(que) == 0:
            cost += 1
            que = set()
            for h, w in checkList:
                for i in range(-2, 3):
                    for g in range(-2, 3):
                        nh = h+i
                        nw = w+g
                        if nw < 0 or nw > W-1 or nh < 0 or nh > H-1:
                            continue
                        else:
                            if dist[nh][nw] == -1:
                                que.add((nh, nw))
            que = deque(que)
            checkList = set()
            if len(que) == 0:
                print(-1)
                exit()
        else:
            h, w = que.popleft()
            if h == G[0] and w == G[1]:
                print(cost)
                exit()
            for i in range(4):
                nh = h+directions[i][0]
                nw = w+directions[i][1]
                if nw < 0 or nw > W-1 or nh < 0 or nh > H-1:
                    continue
                if dist[nh][nw] == -1:
                    que.append((nh, nw))
                    dist[nh][nw] = cost
                elif dist[nh][nw] == -2:
                    checkList.add((h, w))
main()

### FIXED CODE ###

from collections import defaultdict,deque

H, W = map(int, input().split())
S = list(map(int, input().split()))
G = list(map(int, input().split()))
S[0] -= 1
S[1] -= 1
G[0] -= 1
G[1] -= 1
c = [input() for i in range(H)]
dist = [[-1]*W for _ in range(H)]

for i in range(H):
    for g in range(W):
        if c[i][g] == "#":
            dist[i][g] = -2

checkList = set()
que = deque()
que.append(S)

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

cost = 0

while True:
    if len(que) == 0:
        cost += 1
        que = set()
        for h, w in checkList:
            for i in range(-2, 3):
                for g in range(-2, 3):
                    nh = h+i
                    nw = w+g
                    if nw < 0 or nw > W-1 or nh < 0 or nh > H-1:
                        continue
                    else:
                        if dist[nh][nw] == -1:
                            que.add((nh, nw))
        que = deque(que)
        checkList = set()
        if len(que) == 0:
            print(-1)
            exit()
    else:
        h, w = que.popleft()
        dist[h][w] = cost
        if h == G[0] and w == G[1]:
            print(cost)
            exit()
        for i in range(4):
            nh = h+directions[i][0]
            nw = w+directions[i][1]
            if nw < 0 or nw > W-1 or nh < 0 or nh > H-1:
                continue
            if dist[nh][nw] == -1:
                que.append((nh, nw))
                dist[nh][nw] = cost
            elif dist[nh][nw] == -2:
                checkList.add((h, w))