### BUGGY CODE ###

import math
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8
from numba import jit
import collections
import bisect
from collections import deque
from copy import copy, deepcopy
import time

dx = [1,0,-1,0]
dy = [0,1,0,-1]

def main():
    H,W = map(int,input().split())
    Ch,Cw = map(int,input().split())
    Ch+=-1
    Cw+=-1
    Dh,Dw = map(int,input().split())
    Dh+=-1
    Dw+=-1
    S = [None for i in range(H)]
    
    for i in range(H):
        S[i] = input()
        
    ans=0
    visited = [[False for i in range(W)] for j in range(H)]
    walk_deque = deque()
    walk_deque.append((Ch,Cw))
    visited[Ch][Cw]=True
    while(1):
        new_visited = []
        while(len(walk_deque) != 0):
            Ph,Pw = walk_deque.popleft()
            new_visited.append([Ph,Pw])
            #print(Ph,Pw)
            if Ph==Dh and Pw==Dw:
                print(ans)
                return
            for i in range(4):
                tmph = Ph+dy[i]
                tmpw = Pw+dx[i]
                if tmph >= 0 and tmph <=H-1 and tmpw >= 0 and tmpw <=W-1:
                    if S[tmph][tmpw]=='.' and not visited[tmph][tmpw]:
                        walk_deque.append((tmph,tmpw))
                        visited[tmph][tmpw]=True

        #print("warp")
        for h,w in new_visited:
            for x in [-2,-1,0,1,2]:
                for y in [-2,-1,0,1,2]:
                    tmph = h+y
                    tmpw = w+x
                    if tmph >= 0 and tmph <=H-1 and tmpw >= 0 and tmpw <=W-1:
                        if S[tmph][tmpw]=='.' and not visited[tmph][tmpw]:
                            walk_deque.append((h+y,w+x))
                            visited[h+y][w+x]=True
        ans+=1
        if len(walk_deque)==0:
            print(-1)
            return
        
if __name__ == '__main__':
    main()

### FIXED CODE ###

import math
#import numpy as np
#import numba
#from numba import njit, b1, i4, i8, f8
#from numba import jit
import collections
import bisect
from collections import deque
from copy import copy, deepcopy
import time

dx = [1,0,-1,0]
dy = [0,1,0,-1]

def main():
    H,W = map(int,input().split())
    Ch,Cw = map(int,input().split())
    Ch+=-1
    Cw+=-1
    Dh,Dw = map(int,input().split())
    Dh+=-1
    Dw+=-1
    S = [None for i in range(H)]
    
    for i in range(H):
        S[i] = input()
        
    ans=0
    visited = [[False for i in range(W)] for j in range(H)]
    walk_deque = deque()
    walk_deque.append((Ch,Cw))
    visited[Ch][Cw]=True
    while(1):
        new_visited = []
        while(len(walk_deque) != 0):
            Ph,Pw = walk_deque.popleft()
            new_visited.append([Ph,Pw])
            #print(Ph,Pw)
            if Ph==Dh and Pw==Dw:
                print(ans)
                return
            for i in range(4):
                tmph = Ph+dy[i]
                tmpw = Pw+dx[i]
                if tmph >= 0 and tmph <=H-1 and tmpw >= 0 and tmpw <=W-1:
                    if S[tmph][tmpw]=='.' and not visited[tmph][tmpw]:
                        walk_deque.append((tmph,tmpw))
                        visited[tmph][tmpw]=True

        #print("warp")
        for h,w in new_visited:
            for x in [-2,-1,0,1,2]:
                for y in [-2,-1,0,1,2]:
                    tmph = h+y
                    tmpw = w+x
                    if tmph >= 0 and tmph <=H-1 and tmpw >= 0 and tmpw <=W-1:
                        if S[tmph][tmpw]=='.' and not visited[tmph][tmpw]:
                            walk_deque.append((h+y,w+x))
                            visited[h+y][w+x]=True
        ans+=1
        if len(walk_deque)==0:
            print(-1)
            return
        
if __name__ == '__main__':
    main()