### BUGGY CODE ###

import sys, threading
 
threading.stack_size(2**27)
sys.setrecursionlimit(10**5 + 100)

def main():
    def is_sosed(h1, w1):
        if h1 > 0:
            if (dp[h1-1][w1] == -1):
                if (m[h1-1][w1] == '.'):
                    dp[h1-1][w1] = dp[h1][w1]
                    is_sosed(h1-1, w1)
                else:
                    dp[h1-1][w1] = 10000
        if w1 > 0:
            if (dp[h1][w1-1] == -1):
                if (m[h1][w1-1] == '.'):
                    dp[h1][w1-1] = dp[h1][w1]
                    is_sosed(h1, w1-1)
                else:
                    dp[h1][w1-1] = 10000
        if h1 < h - 1:
            if (dp[h1+1][w1] == -1):
                if (m[h1+1][w1] == '.'):
                    dp[h1+1][w1] = dp[h1][w1]
                    is_sosed(h1+1, w1)
                else:
                    dp[h1+1][w1] = 10000
        if w1 < w - 1:
            if (dp[h1][w1+1] == -1):
                if (m[h1][w1+1] == '.'):
                    dp[h1][w1+1] = dp[h1][w1]
                    is_sosed(h1, w1+1)
                else:
                    dp[h1][w1+1] = 10000

    def find_warps(now):
        if dp[dh-1][dw-1] > -1:
            return True
        fl = True
        for i in range(h):
            for j in range(w):
                if dp[i][j] == now:
                    for k in range(max(0, i-2), min(h, i+3)):
                        for p in range(max(0, j-2), min(w, j+3)):
                            if dp[k][p] == -1:
                                if m[k][p] == '.':
                                    fl = False
                                    dp[k][p] = dp[i][j] + 1
                                    if dp[dh-1][dw-1] > -1:
                                        return True
                                    stack.append([k, p])
                                else:
                                    dp[k][p] = 10000
        return fl

    h, w = map(int, input().split())
    ch, cw = map(int, input().split())
    dh, dw = map(int, input().split())
    m = [[] for i in range(h)]
    dp = [[-1]*w for i in range(h)]
    for i in range(h):
        m[i] = input()
    dp[ch-1][cw-1] = 0
    stack = [[ch-1, cw-1]]
    now = 0
    fl = False
    while dp[dh-1][dw-1] == -1:
        while stack:
            h11, w11 = stack.pop()
            is_sosed(h11, w11)
            if dp[dh-1][dw-1] > -1:
                break
        fl = find_warps(now)
        now += 1
        if fl:
            break
    print(dp[dh-1][dw-1])

t = threading.Thread(target=main)
t.start()
t.join()


### FIXED CODE ###

from collections import deque


def main():

    def bfs(h1, w1, dp):
        """
        -1 - unsearched
        -2 - candidate for warp
        -3 - wall '#'
        """
        queue = deque()
        queue_warp = deque()
        queue.append([h1, w1])
        now = 0

        while queue:
            h1, w1 = queue.popleft()
            fl_up = False
            fl_left = False
            fl_down = False
            fl_right = False

            if h1 > 0:
                if dp[h1-1][w1] == -1 or dp[h1-1][w1] == -2:
                    if m[h1-1][w1] == '.':
                        dp[h1-1][w1] = now
                        queue.append([h1-1, w1])
                    else:
                        dp[h1-1][w1] = -3
                if dp[h1-1][w1] == -3:
                    fl_up = True
                    if h1 > 1:
                        if dp[h1-2][w1] == -1:
                            if m[h1-2][w1] == '.':
                                dp[h1-2][w1] = -2
                                queue_warp.append([h1-2, w1])
                            else:
                                dp[h1-2][w1] = -3
            
            if w1 > 0:
                if dp[h1][w1-1] == -1  or dp[h1][w1-1] == -2:
                    if m[h1][w1-1] == '.':
                        dp[h1][w1-1] = now
                        queue.append([h1, w1-1])
                    else:
                        dp[h1][w1-1] = -3
                if dp[h1][w1-1] == -3:
                    fl_left = True
                    if w1 > 1:
                        if dp[h1][w1-2] == -1:
                            if m[h1][w1-2] == '.':
                                dp[h1][w1-2] = -2
                                queue_warp.append([h1, w1-2])
                            else:
                                dp[h1][w1-2] = -3
            
            if h1 < h - 1:
                if dp[h1+1][w1] == -1 or dp[h1+1][w1] == -2:
                    if m[h1+1][w1] == '.':
                        dp[h1+1][w1] = now
                        queue.append([h1+1, w1])
                    else:
                        dp[h1+1][w1] = -3
                if dp[h1+1][w1] == -3:
                    fl_down = True
                    if h1 < h - 2:
                        if dp[h1+2][w1] == -1:
                            if m[h1+2][w1] == '.':
                                dp[h1+2][w1] = -2
                                queue_warp.append([h1+2, w1])
                            else:
                                dp[h1+2][w1] = -3
            
            if w1 < w - 1:
                if dp[h1][w1+1] == -1 or dp[h1][w1+1] == -2:
                    if m[h1][w1+1] == '.':
                        dp[h1][w1+1] = now
                        queue.append([h1, w1+1])
                    else:
                        dp[h1][w1+1] = -3
                if dp[h1][w1+1] == -3:
                    fl_right = True
                    if w1 < w - 2:
                        if dp[h1][w1+2] == -1:
                            if m[h1][w1+2] == '.':
                                dp[h1][w1+2] = -2
                                queue_warp.append([h1, w1+2])
                            else:
                                dp[h1][w1+2] = -3
            
            if fl_up and fl_left:
                for i in range(max(0, h1-2), h1):
                    for j in range(max(0, w1-2), w1):
                        if dp[i][j] == -1:
                            if m[i][j] == '.':
                                dp[i][j] = -2
                                queue_warp.append([i, j])
                            else:
                                dp[i][j] = -3

            if fl_left and fl_down:
                for i in range(h1+1, min(h, h1+3)):
                    for j in range(max(0, w1-2), w1):
                        if dp[i][j] == -1:
                            if m[i][j] == '.':
                                dp[i][j] = -2
                                queue_warp.append([i, j])
                            else:
                                dp[i][j] = -3
            
            if fl_down and fl_right:
                for i in range(h1+1, min(h, h1+3)):
                    for j in range(w1+1, min(w, w1+3)):
                        if dp[i][j] == -1:
                            if m[i][j] == '.':
                                dp[i][j] = -2
                                queue_warp.append([i, j])
                            else:
                                dp[i][j] = -3

            if fl_right and fl_up:
                for i in range(max(0, h1-2), h1):
                    for j in range(w1+1, min(w, w1+3)):
                        if dp[i][j] == -1:
                            if m[i][j] == '.':
                                dp[i][j] = -2
                                queue_warp.append([i, j])
                            else:
                                dp[i][j] = -3

            if not queue:
                now += 1
                while queue_warp:
                    h1, w1 = queue_warp.popleft()
                    if dp[h1][w1] == -2:
                        dp[h1][w1] = now
                        queue.append([h1, w1])
            
            if dp[dh-1][dw-1] >= 0:
                return dp[dh-1][dw-1]
        
        return dp[dh-1][dw-1]

    h, w = map(int, input().split())
    ch, cw = map(int, input().split())
    dh, dw = map(int, input().split())

    m = [[] for i in range(h)]
    dp = [[-1]*w for i in range(h)]
    dp[ch-1][cw-1] = 0
    
    for i in range(h):
        m[i] = input()
    
    ans = bfs(ch-1, cw-1, dp)

    print(ans)

main()