### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**6)
from collections import deque
from copy import deepcopy
def main():
    movey = (0, -1, 0, 1)
    movex = (1, 0, -1, 0)
    warpy = (-2, -2, -2, -2, -2, -1, -1, -1, -1,  0, 0,  1,  1, 1, 1, 2, 2, 2, 2, 2)
    warpx = (-2, -1,  0,  1,  2, -2, -1,  1,  2, -2, 2, -2, -1, 1, 2, -2, -1, 0, 1, 2)
    warpy1 = (-2, -2, -1, -1, 0, 1, 1, 2, 2)
    warpx1 = ( 1,  2,  1,  2, 2, 1, 2, 1, 2)
    warpy2 = (-2, -2, -2, -2, -2, -1, -1, -1, -1)
    warpx2 = (-2, -1,  0,  1,  2, -2, -1,  1, 2)
    warpy3 = (-2, -2, -1, -1,  0,  1,  1,  2, 2)
    warpx3 = (-2, -1, -2, -1, -2, -2, -1, -2, -1)
    warpy4 = ( 1,  2,  1,  2,  2,  1,  1, 1, 2)
    warpx4 = (-2, -2, -1, -1,  0,  1,  2, 2, 2)
    warps = []
    warps.append((warpy1, warpx1))
    warps.append((warpy2, warpx2))
    warps.append((warpy3, warpx3))
    warps.append((warpy4, warpx4))
    h, w = map(int, input().split())
    ch, cw = map(int, input().split())
    dh, dw = map(int, input().split())
    ch += 1
    cw += 1
    dh += 1
    dw += 1
    dummy = ['#'] * (w + 4)
    maze = list()
    maze.append(dummy)
    maze.append(dummy)
    for _ in range(h):
        row = ['#'] + ['#'] + list(input()) + ['#'] + ['#']
        maze.append(row)
    maze.append(dummy)
    maze.append(dummy)
    maze[ch][cw] = 0
    #maze[dh][dw] = 'g'

    next_nodes = deque()
    next_nodes.append((ch, cw))
    next2_nodes = deque()
    #cnt = 0
    while next_nodes:
        while next_nodes:
            nextn = next_nodes.popleft()
            nextny = nextn[0]
            nextnx = nextn[1]
            curg = maze[nextny][nextnx]
            flag = False
            for j1 in range(4):
                ny = nextny + movey[j1]
                nx = nextnx + movex[j1]
                if maze[ny][nx] == '.':
                    maze[ny][nx] = curg
                    next2_nodes.append((ny, nx))
                elif maze[ny][nx] == '#':
                    warpnext_y = warps[j1][0]
                    warpnext_x = warps[j1][1]
                    for k1 in range(9):
                        ny = nextny + warpnext_y[k1]
                        nx = nextnx + warpnext_x[k1]
                        nextg = maze[ny][nx]
                        if nextg == '.':
                            next2_nodes.append((ny, nx))
                            maze[ny][nx] = curg + 1
                        elif nextg == '#':
                            pass
                        elif nextg > curg + 1:
                            maze[ny][nx] = curg + 1
                            next2_nodes.append((ny, nx))
                elif maze[ny][nx] > curg:
                    maze[ny][nx] = curg
                    next2_nodes.append((ny, nx))

        next_nodes = deque(set(deepcopy(next2_nodes)))
        next2_nodes = deque()
    g = maze[dh][dw]

    if g == '.':
        print(-1)
    else:
        print(g)

if __name__ == '__main__':
    main()


### FIXED CODE ###


import sys
from collections import deque
def main():
    movey = (0, -1, 0, 1)
    movex = (1, 0, -1, 0)
    warpy = (-2, -2, -2, -2, -2, -1, -1, -1, -1,  0, 0,  1,  1, 1, 1, 2, 2, 2, 2, 2)
    warpx = (-2, -1,  0,  1,  2, -2, -1,  1,  2, -2, 2, -2, -1, 1, 2, -2, -1, 0, 1, 2)

    h, w = map(int, input().split())
    ch, cw = map(int, input().split())
    dh, dw = map(int, input().split())
    ch += 1
    cw += 1
    dh += 1
    dw += 1
    dummy = tuple(['#'] * (w + 4))
    maze = {e: [] for e in range(h + 4)}
    maze[0] = dummy
    maze[1] = dummy
    for i in range(h):
        row = ['#'] + ['#'] + list(input()) + ['#'] + ['#']
        maze[i+2] = row
    maze[h+2] = dummy
    maze[h+3] = dummy
    maze[ch][cw] = 0
    maze[dh][dw] = 'g'

    nodes = deque()
    nodes.append((ch, cw))
    res = []
    while nodes:
        (n_y, n_x) = nodes.popleft()
        cur_masu = maze[n_y][n_x]
        flag = False
        for j1 in range(4):
            ny = n_y + movey[j1]
            nx = n_x + movex[j1]
            next_masu = maze[ny][nx]
            if next_masu == 'g':
                res.append(cur_masu)
            elif next_masu == '.':
                maze[ny][nx] = cur_masu
                nodes.appendleft((ny, nx))
            elif next_masu == '#':
                flag = True
            elif next_masu > cur_masu:
                maze[ny][nx] = cur_masu
                nodes.appendleft((ny, nx))
        if flag:
            for j1 in range(20):
                ny = n_y + warpy[j1]
                nx = n_x + warpx[j1]
                next_masu = maze[ny][nx]
                if next_masu == 'g':
                    res.append(cur_masu+1)
                elif next_masu == '.':
                    maze[ny][nx] = cur_masu + 1
                    nodes.append((ny, nx))
                elif next_masu == '#':
                    pass
                elif next_masu > cur_masu + 1:
                    maze[ny][nx] = cur_masu + 1
                    nodes.append((ny, nx))

    if res:
        print(min(res))
    else:
        print(-1)

if __name__ == '__main__':
    main()