### BUGGY CODE ###

# Date [ 2020-08-22 21:07:07 ]
# Problem [ d.py ]
# Author Koki_tkg

import sys;      from decimal import Decimal
import math;     from itertools import combinations, product
import bisect;   from collections import Counter, deque, defaultdict

# sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def lcm(a: int, b: int) -> int: return (a * b) // math.gcd(a, b)

def Main():
    H, W = read_ints()
    wiz = read_int_list()
    goal = read_int_list()
    grid = [read_str() for _ in range(H)]
    visit = [[False] * W for _ in range(H)]
    dist = [[-1] * W for _ in range(H)]
    
    que = deque([[wiz[0] - 1, wiz[1] - 1]])

    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    dx8 = [0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, -2, -2, -2, -1]
    dy8 = [2, 2, 2, 1, 0, -1, -2, -2, -2, -2, -2, -1, 0, 1, 2, 2]
    cnt = 0

    while que or que2:
        que2 = deque()
        while que:
            y, x = que.popleft()
            dist[y][x] = cnt
            que2.append([y, x])

            if y == goal[0] - 1 and x == goal[1] - 1:
                print(dist[goal[0] - 1][goal[1] - 1])
                sys.exit()

            for i in range(4):
                nx = x + dx[i]
                ny = y + dy[i]
                
                if 0 <= nx < W and 0 <= ny < H and grid[ny][nx] != '#' and visit[ny][nx] == False:
                    que.append([ny, nx])
                    visit[ny][nx] = True
        
        cnt += 1
        while que2:
            y, x = que2.popleft()
            
            if y == goal[0] - 1 and x == goal[1] - 1:
                print(dist[goal[0] - 1][goal[1] - 1])
                sys.exit()

            for i in range(16):
                nx = x + dx8[i]
                ny = y + dy8[i]
                
                if 0 <= nx < W and 0 <= ny < H and grid[ny][nx] != '#' and visit[ny][nx] == False:
                    que.append([ny, nx])

    print(-1)


if __name__ ==  '__main__':
    Main()


### FIXED CODE ###

# Date [ 2020-08-22 21:07:07 ]
# Problem [ d.py ]
# Author Koki_tkg

import sys;      from decimal import Decimal
import math;     from itertools import combinations, product
import bisect;   from collections import Counter, deque, defaultdict

# sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def lcm(a: int, b: int) -> int: return (a * b) // math.gcd(a, b)

import numpy as np
from numba import i8, njit, u1, b1
from heapq import heappop, heappush

@njit(i8(b1[:,:], i8, i8, i8, i8), cache=True)
def solve(grid, ch, cw, dh, dw):
    h, w = grid.shape
    
    dist = np.full((h, w), INF, dtype=np.int64)
    dist[ch][cw] = 0

    que = [(0, ch, cw)]

    while que:
        cnt, y, x = heappop(que)

        if y == dh and x == dw:
            break

        for dy in range(-2, 3):
            for dx in range(-2, 3):
                if dx == 0 and dy == 0:
                    continue

                if abs(dx) == 2 or abs(dy) == 2 or (abs(dx) == 1 and abs(dy) == 1):
                    warp = 1
                else:
                    warp = 0

                nx, ny = x + dx, y + dy
                if nx < 0 or nx >= w or ny < 0 or ny >= h or grid[ny][nx]:
                    continue
                
                if dist[ny][nx] > cnt + warp:
                    dist[ny][nx] = cnt + warp
                    heappush(que, (cnt + warp, ny, nx))

    if dist[dh][dw] == INF:
        return - 1
    else:
        return dist[dh][dw]


def Main():
    H, W = read_ints()
    ch, cw = read_ints(); ch -= 1; cw -= 1
    dh, dw = read_ints(); dh -= 1; dw -= 1

    grid = np.array([[g == '#' for g in read_str()] for _ in range(H)])
    
    print(solve(grid, ch, cw, dh, dw))

def cc_export(config):
    from numba.pycc import CC
    cc = CC('my_module')
    for func, signature in config:
        vars()[func.__name__] = njit(signature)(func)
        cc.export(func.__name__, signature)(func)
    cc.compile()

if __name__ == '__main__':
    Main()