### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

sys.setrecursionlimit(1000000)

def yn(b):
    print("Yes" if b==1 else "No")
    return
def aaa(g,memo,www,c):
    ww = www[g]
    memo[g]=c
    c2 = (c+1)
    for i in range(len(ww)):
        w=ww[i]
        if w==1 and w!=g:
            if memo[w]==-1 or memo[w] > c2:
                aaa(w,memo,www,c2)

def tansaku(y,x,mp,done,grp,wrp,w,h,gm,xxx):
    wlkh=[y,y,y-1,y+1]
    wlkw=[x-1,x+1,x,x]
    nun=y*w+x
    grp.add(nun)
    gm[nun]=xxx
    for zzz in range(4):
        zh=wlkh[zzz]
        zw=wlkw[zzz]
        if zh < 0:
            continue
        if zh >= h:
            continue
        if zw < 0:
            continue
        if zw >= w:
            continue
        if mp[zh][zw] == "." and done[zh][zw]==0:
            done[zh][zw]=1
            tansaku(zh,zw,mp,done,grp,wrp,w,h,gm,xxx)
    for i in range(y-2,y+3):
        for j in range(x-2,x+3):
            if i < 0:
                continue
            if i >= h:
                continue
            if j < 0:
                continue
            if j >= w:
                continue
            if mp[i][j]==".":
                wrp.add(i*w+j)

def resolve():
    readline=sys.stdin.readline

    h,w=map(int, readline().rstrip().split())
    ch,cw=map(int, readline().rstrip().split())
    dh,dw=map(int, readline().rstrip().split())
    ch-=1
    cw-=1
    dh-=1
    dw-=1
    mp=[]

    cN=ch*w+cw
    dN=dh*w+dw

    done=[[0 for i in range(w)] for j in range(h)]
    gm=[-1] * ((h*w)+3)

    road=0
    for i in range(h):
        ss=readline().rstrip()
        road+=ss.count(".")
        mp.append(ss)

    gs = []
    ws=[]
    xxx=-1
    for y in range(h):
        for x in range(w):
            masu=y*w+x
            if mp[y][x] == "." and done[y][x]==0:
                grp=set()
                wrp=set()
                xxx+=1
                tansaku(y,x,mp,done,grp,wrp,w,h,gm,xxx)
                gs.append(grp)
                ws.append(wrp)
            else:
                done[y][x]=1
    cnt=len(gs)
    www=[[0 for i in range(cnt)] for j in range(cnt)]
    for i in range(cnt):
        gp = gs[i]
        wp = ws[i]
        for pp in wp:
            j=gm[pp]
            www[i][j]=1
            www[j][i]=1
    memo=[-1]*cnt
    aaa(gm[cN],memo,www,0)
    print(memo[gm[dN]])
    #n,m,k=map(int, readline().rstrip().split())
    #arr=list(map(int, readline().rstrip().split()))
    #n=int(readline())
    #ss=readline().rstrip()
    #yn(1)

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------


### FIXED CODE ###

### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest
from collections import deque

def resolve():
    readline=sys.stdin.readline

    h,w=map(int, readline().rstrip().split())
    ch,cw=map(int, readline().rstrip().split())
    dh,dw=map(int, readline().rstrip().split())
    ch-=1
    cw-=1
    dh-=1
    dw-=1

    mp=[[None for i in range(w)] for j in range(h)]

    for i in range(h):
        ss = readline().rstrip()
        for j in range(w):
            if ss[j]=="#":
                mp[i][j] = -1

    d = deque()
    d2 = deque()

    if mp[ch][cw]==None:
        mp[ch][cw]=0
        d.append((ch,cw))
    while len(d) > 0:
        y,x = d.popleft()
        d2.append((y,x))
        for dy,dx in ((1,0),(-1,0),(0,1),(0,-1)):
            ny=y+dy
            nx=x+dx
            if 0 <= ny < h and 0 <= nx < w and mp[ny][nx]==None:
                mp[ny][nx]=mp[y][x]
                d.append((ny,nx))
        if len(d)==0:
            while len(d2) > 0:
                y2,x2 = d2.popleft()
                for dy in range(-2,3):
                    for dx in range(-2,3):
                        ny=y2+dy
                        nx=x2+dx
                        if 0 <= ny < h and 0 <= nx < w and mp[ny][nx]==None:
                            mp[ny][nx]=mp[y][x]+1
                            d.append((ny,nx))
    if mp[dh][dw] == None:
        print(-1)
    else:
        print(mp[dh][dw])    
    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------