### BUGGY CODE ###

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=tf-8
#

"""
"""

from collections import defaultdict
import bisect
from itertools import product
import heapq

import sys
input = sys.stdin.readline

def solve(h,w,ch,cw,dh,dw,s):
    graph = defaultdict(list)
    for i,j in product(range(h),range(w)):
        if s[i][j] == ".":
            if i > 0 and s[i-1][j] == ".":
                graph[(i,j)].append((0,(i-1,j)))
                graph[(i-1,j)].append((0,(i,j)))
            if j > 0 and s[i][j-1] == ".":
                graph[(i,j)].append((0,(i,j-1)))
                graph[(i,j-1)].append((0,(i,j)))
            for k, l in product(range(max(0,i-2),i),range(max(0,j-2),j)):
                if s[k][l] == ".":
                    graph[(k,l)].append((1,(i,j)))
                    graph[(i,j)].append((1,(k,l)))
            for k, l in product(range(i+1,min(i+3,h)),range(max(0,j-2),j)):
                if s[k][l] == ".":
                    graph[(k,l)].append((1,(i,j)))
                    graph[(i,j)].append((1,(k,l)))
            if i > 1 and s[i-2][j] == ".":
                    graph[(i-2,j)].append((1,(i,j)))
                    graph[(i,j)].append((1,(i-2,j)))
            if j > 1 and s[i][j-2] == ".":
                    graph[(i,j-2)].append((1,(i,j)))
                    graph[(i,j)].append((1,(i,j-2)))
    d = [[10**7]*w for _ in range(h)]
    d[ch][cw] = 0
    Q = []
    heapq.heappush(Q,(0,(ch,cw)))
    seen = set([])
    while Q:
        dis, pos1 = heapq.heappop(Q)
        i, j = pos1
        for dd, pos2 in graph[pos1]:
            k, l = pos2
            alt = d[i][j] + dd
            if d[k][l] > alt:
                d[k][l] = alt
                heapq.heappush(Q,(alt,pos2))
    ans = d[dh][dw]
    if ans < 10**7:
        print(ans)
    else:
        print(-1)


            

def main():
    h, w = list(map(int,input().split()))
    ch, cw = list(map(int,input().split()))
    dh, dw = list(map(int,input().split()))
    ch -= 1
    cw -= 1
    dh -= 1
    dw -= 1
    s = []
    for _ in range(h):
        s.append(list(input().rstrip()))
    solve(h,w,ch,cw,dh,dw,s)


if __name__ == "__main__":
    main()

### FIXED CODE ###

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=tf-8
#

"""
"""

from collections import defaultdict
import bisect
from itertools import product
from collections import deque

import sys
input = sys.stdin.readline

def solve(h,w,ch,cw,dh,dw,s):
    d = [[10**7]*w for _ in range(h)]
    d[ch][cw] = 0
    dis0 = [(1,0),(0,1),(-1,0),(0,-1)]
    dis1 = [(i,j) for i,j in product(range(-2,3),range(-2,3)) if 1 < abs(i)+abs(j)]
    Q = deque([(ch,cw)])
    ans = -1
    while Q:
        pos1 = Q.pop()
        i, j = pos1
        if i == dh and j==dw:
            ans = d[dh][dw]
            break
        for pos2 in [(i+a,j+b) for a,b in dis0 if 0 <= i+a < h and 0 <= j+b <w and s[i+a][j+b]=="."]:
            k, l = pos2
            alt = d[i][j] 
            if d[k][l] > alt:
                d[k][l] = alt
                Q.append((k,l))
        for pos2 in [(i+a,j+b) for a,b in dis1 if 0 <= i+a < h and 0 <= j+b <w and s[i+a][j+b]=="."]:
            k, l = pos2
            alt = d[i][j]+1
            if d[k][l] > alt:
                d[k][l] = alt
                Q.appendleft((k,l))
    print(ans)



def main():
    h, w = list(map(int,input().split()))
    ch, cw = list(map(int,input().split()))
    dh, dw = list(map(int,input().split()))
    ch -= 1
    cw -= 1
    dh -= 1
    dw -= 1
    s = []
    for _ in range(h):
        s.append(list(input().rstrip()))
    solve(h,w,ch,cw,dh,dw,s)


if __name__ == "__main__":
    main()