#!/usr/bin/env python3


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
import itertools as it
from collections import deque

def main(H, W, Ch, Cw, Dh, Dw, S):

    # visited[H][W]
    visited =  [[False for _ in range(W)] for __ in range(H)]

    # pick root 
    root = (Ch, Cw)
    
    # flag
    empty_while = True
    count = 0

    """DFS"""
    def DFS(node, block=set([])):
        def pick_next(node):
            h,w = node
            assert S[h][w]
            for next_h, next_w in [
                (h+1, w), # down
                (h, w+1), # right
                (h, w-1), # left
                (h-1, w)  # up
            ]:
                try:
                    if next_w < 0 or next_h < 0:
                        raise IndexError
                    if not visited[next_h][next_w]:
                        if S[next_h][next_w]:
                            return next_h, next_w
                except IndexError:
                    pass
            return False

        # mark root as traversed
        visited[node[0]][node[1]] = True
        block.add(node)

        # if there is a node to go to 
        while pick_next(node):
            next_node = pick_next(node)
            if node:
                # mark node as traversed
                h, w = next_node
                visited[h][w] = True
                block.add(next_node)

                # go to next node
                DFS(next_node, block)
        
        return frozenset(block)

    """BFS"""
    def BFS(node):
        def pick_next(node):
            h,w = node
            r = range(-2, 3)
            for h_offset, w_offset in it.product(r,r):
                next_h = h + h_offset
                next_w = w + w_offset
                try:
                    if next_w < 0 or next_h < 0:
                        raise IndexError
                    if not visited[next_h][next_w]:
                        if S[next_h][next_w]:
                            return next_h, next_w
                except IndexError:
                    pass
            return False

        queue = deque([])
        levels = {}

        start = DFS(node)
        if visited[Dh][Dw]: 
            return 0

        queue.append(start)  
        levels[start] = 0

        while queue:
            block = queue.popleft()
            
            if visited[Dh][Dw]:
                    return levels[next_block]

            for node in block:

                while pick_next(node):
                    next_node = pick_next(node)

                    # add node to queue
                    next_block = DFS(next_node, block=set([]))
                    queue.append(next_block)
                    levels[next_block] = levels[block] + 1


        return -1 

    node = root
    count = BFS(node)    
        
    print(count)
    pass

if __name__ == '__main__':
    H, W = list(map(int, input().split()))
    Ch, Cw = list(map(int, input().split()))
    Dh, Dw = list(map(int, input().split()))
    S = [[False for _ in range(W)] for __ in range(H)]

    for h in range(H):
        S_row = input()
        for w, s in enumerate(S_row):
            if s=='.':
                S[h][w] = True
            else:
                S[h][w] = False

    Ch -= 1
    Cw -= 1
    Dh -= 1
    Dw -= 1

    main(H, W, Ch, Cw, Dh, Dw, S)


Measured memory_bytes: 184584