### BUGGY CODE ###

import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    H, W = map(int, readline().split())
    sh, sw = map(int, readline().split())
    gh, gw = map(int, readline().split())
    sh -= 1
    sw -= 1
    gh -= 1
    gw -= 1
    S = read().split()

    dist = [[INF] * W for _ in range(H)]
    walk = ((1, 0), (-1, 0), (0, 1), (0, -1))
    warp = ((-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2), (-1, -2), (-1, -1), (-1, 1), (-1, 2),
            (0, -2), (0, 2), (1, -2), (1, -1), (1, 1), (1, 2), (2, -2), (2, -1), (2, 0), (2, 1), (2, 2))

    Q = deque()
    Q.append((sh, sw))
    Q2 = deque()
    dist[sh][sw] = 0

    bl = True

    while bl:
        while Q:
            h, w = Q.popleft()
            if h==gh and w == gw:
                bl = False
                break
            d = dist[h][w]
            flag = 0
            for dh, dw in walk:
                nh = h + dh
                nw = w + dw
                if 0<=nh<H and 0<=nw<W:
                    if S[nh][nw] == '.':
                        if dist[nh][nw] > d:
                            dist[nh][nw] = d
                            Q.append((nh,nw))
                    else:
                        flag += 1
            if flag:
                Q2.append((h,w))
        if bl==False:
            break
        while Q2:
            h, w = Q2.popleft()
            d = dist[h][w]
            for dh, dw in warp:
                nh = h + dh
                nw = w + dw
                if 0<=nh<H and 0<=nw<W:
                    if S[nh][nw] == '.' and dist[nh][nw] == INF:
                        dist[nh][nw] = d + 1
                        Q.append((nh,nw))
    if dist[gh][gw] != INF:
        print(dist[gh][gw])
    else:
        print(-1)



if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    H, W = map(int, readline().split())
    sh, sw = map(int, readline().split())
    gh, gw = map(int, readline().split())
    sh -= 1
    sw -= 1
    gh -= 1
    gw -= 1
    S = read().split()

    dist = [[INF] * W for _ in range(H)]
    walk = ((1, 0), (-1, 0), (0, 1), (0, -1))
    warp = ((-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2), (-1, -2), (-1, -1), (-1, 1), (-1, 2),
            (0, -2), (0, 2), (1, -2), (1, -1), (1, 1), (1, 2), (2, -2), (2, -1), (2, 0), (2, 1), (2, 2))

    Q = deque()
    Q.append((sh, sw))
    Q2 = deque()
    dist[sh][sw] = 0

    while Q:
        while Q:
            h, w = Q.popleft()
            d = dist[h][w]
            flag = 0
            for dh, dw in walk:
                nh = h + dh
                nw = w + dw
                if 0<=nh<H and 0<=nw<W:
                    if S[nh][nw] == '.':
                        if dist[nh][nw] > d:
                            dist[nh][nw] = d
                            Q.append((nh,nw))
                    else:
                        flag += 1
            if flag:
                Q2.append((h,w))
        while Q2:
            h, w = Q2.popleft()
            d = dist[h][w]
            for dh, dw in warp:
                nh = h + dh
                nw = w + dw
                if 0<=nh<H and 0<=nw<W:
                    if S[nh][nw] == '.' and dist[nh][nw] == INF:
                        dist[nh][nw] = d + 1
                        Q.append((nh,nw))
    if dist[gh][gw] != INF:
        print(dist[gh][gw])
    else:
        print(-1)



if __name__ == '__main__':
    main()