from collections import defaultdict

from collections import deque

from collections import Counter

import math



def readInt():

	return int(input())

def readInts():

	return list(map(int, input().split()))

def readChar():

	return input()

def readChars():

	return input().split()



def p(arr):

	print("_______________________")

	for i in arr:

		for j in i:

			print(j,end=" ")

		print()



h,w = readInts()

ch,cw = readInts()

dh,dw = readInts()

s = [["#" for i in range(w+2)]]+[["#"]+ list(input())+["#"] for i in range(h)]+[["#" for i in range(w+2)]]



q = deque()

visited = [[-1]*(w+2) for i in range(h+2)]

count = (w+2)*(h+2)

for i in range(h+2):

	for j in range(w+2):

		if s[i][j]=="#":

			visited[i][j] = float("inf")

			count -= 1



from pprint import pprint

visited[ch][cw] = 0



q = {(ch,cw)}

set_list = set()

cost = 0

while count:

	while len(q) !=0:

		x,y = q.pop()

		set_list.add((x,y))

		visited[x][y] = cost

		count-=1

		if s[x-1][y]=="." and visited[x-1][y]==-1:

			q.add((x-1,y))

		if s[x][y-1]=="." and visited[x][y-1]==-1:

			q.add((x,y-1))

		if s[x+1][y]=="." and visited[x+1][y]==-1:

			q.add((x+1,y))

		if s[x][y+1]=="." and visited[x][y+1]==-1:

			q.add((x,y+1))



	if len(set_list)==0:

		break



	cost+=1

	while len(set_list) != 0:

		x,y = set_list.pop()

		for dx in range(-2,3):

			for dy in range(-2,3):

				x2 = x+dx

				y2 = y+dy

				if visited[min(x2,h+1)][min(y2,w+1)]!=-1: continue

				q.add((x2,y2))



print(visited[dh][dw])

from collections import defaultdict

from collections import deque

from collections import Counter

import math



def readInt():

	return int(eval(input()))

def readInts():

	return list(map(int, input().split()))

def readChar():

	return eval(input())

def readChars():

	return input().split()



h,w = readInts()

ch,cw = readInts()

dh,dw = readInts()

s = ["#"*(w+2)]+["#"+eval(input())+"#" for i in range(h)]+["#"*(w+2)]

visited = [[-2]*(w+2) for i in range(h+2)]

for i in range(h+2):

	for j in range(w+2):

		if s[i][j]==".":

			visited[i][j] = -1



delta1 = [[0,1],[1,0],[-1,0],[0,-1]]



q = {(ch,cw)}

set_list = set()

cost = 0

while 1:

	if len(q)==0:

		break

	while len(q) !=0:

		x,y = q.pop()

		set_list.add((x,y))

		visited[x][y] = cost

		for dx,dy in delta1:

			x2 = x+dx

			y2 = y+dy

			if visited[x2][y2]==-1:

				q.add((x2,y2))

	cost+=1

	while len(set_list) != 0:

		x,y = set_list.pop()

		for dx in range(-2,3):

			for dy in range(-2,3):

				x2 = x+dx

				y2 = y+dy

				if visited[min(x2,h+1)][min(y2,w+1)]!=-1: continue

				q.add((x2,y2))



print((visited[dh][dw]))
from collections import defaultdict
from collections import deque
from collections import Counter
import math


def readInt():
    return int(input())


def readInts():
    return list(map(int, input().split()))


def readChar():
    return input()


def readChars():
    return input().split()


def p(arr):
    print("_______________________")
    for i in arr:
        for j in i:
            print(j, end=" ")
        print()


h, w = readInts()
ch, cw = readInts()
dh, dw = readInts()
s = (
    [["#" for i in range(w + 2)]]
    + [["#"] + list(input()) + ["#"] for i in range(h)]
    + [["#" for i in range(w + 2)]]
)
q = deque()
visited = [[-1] * (w + 2) for i in range(h + 2)]
count = (w + 2) * (h + 2)
for i in range(h + 2):
    for j in range(w + 2):
        if s[i][j] == "#":
            visited[i][j] = float("inf")
            count -= 1
from pprint import pprint

visited[ch][cw] = 0
q = {(ch, cw)}
set_list = set()
cost = 0
while count:
    while len(q) != 0:
        x, y = q.pop()
        set_list.add((x, y))
        visited[x][y] = cost
        count -= 1
        if s[x - 1][y] == "." and visited[x - 1][y] == -1:
            q.add((x - 1, y))
        if s[x][y - 1] == "." and visited[x][y - 1] == -1:
            q.add((x, y - 1))
        if s[x + 1][y] == "." and visited[x + 1][y] == -1:
            q.add((x + 1, y))
        if s[x][y + 1] == "." and visited[x][y + 1] == -1:
            q.add((x, y + 1))
    if len(set_list) == 0:
        break
    cost += 1
    while len(set_list) != 0:
        x, y = set_list.pop()
        for dx in range(-2, 3):
            for dy in range(-2, 3):
                x2 = x + dx
                y2 = y + dy
                if visited[min(x2, h + 1)][min(y2, w + 1)] != -1:
                    continue
                q.add((x2, y2))
print(visited[dh][dw])

from collections import defaultdict
from collections import deque
from collections import Counter
import math


def readInt():
    return int(eval(input()))


def readInts():
    return list(map(int, input().split()))


def readChar():
    return eval(input())


def readChars():
    return input().split()


h, w = readInts()
ch, cw = readInts()
dh, dw = readInts()
s = ["#" * (w + 2)] + ["#" + eval(input()) + "#" for i in range(h)] + ["#" * (w + 2)]
visited = [[-2] * (w + 2) for i in range(h + 2)]
for i in range(h + 2):
    for j in range(w + 2):
        if s[i][j] == ".":
            visited[i][j] = -1
delta1 = [[0, 1], [1, 0], [-1, 0], [0, -1]]
q = {(ch, cw)}
set_list = set()
cost = 0
while 1:
    if len(q) == 0:
        break
    while len(q) != 0:
        x, y = q.pop()
        set_list.add((x, y))
        visited[x][y] = cost
        for dx, dy in delta1:
            x2 = x + dx
            y2 = y + dy
            if visited[x2][y2] == -1:
                q.add((x2, y2))
    cost += 1
    while len(set_list) != 0:
        x, y = set_list.pop()
        for dx in range(-2, 3):
            for dy in range(-2, 3):
                x2 = x + dx
                y2 = y + dy
                if visited[min(x2, h + 1)][min(y2, w + 1)] != -1:
                    continue
                q.add((x2, y2))
print((visited[dh][dw]))

-    return int(input()) +    return int(eval(input())) -    return input() +    return eval(input()) -def p(arr): -    print("_______________________") -    for i in arr: -        for j in i: -            print(j, end=" ") -        print() - - -s = ( -    [["#" for i in range(w + 2)]] -    + [["#"] + list(input()) + ["#"] for i in range(h)] -    + [["#" for i in range(w + 2)]] -) -q = deque() -visited = [[-1] * (w + 2) for i in range(h + 2)] -count = (w + 2) * (h + 2) +s = ["#" * (w + 2)] + ["#" + eval(input()) + "#" for i in range(h)] + ["#" * (w + 2)] +visited = [[-2] * (w + 2) for i in range(h + 2)] -        if s[i][j] == "#": -            visited[i][j] = float("inf") -            count -= 1 -from pprint import pprint - -visited[ch][cw] = 0 +        if s[i][j] == ".": +            visited[i][j] = -1 +delta1 = [[0, 1], [1, 0], [-1, 0], [0, -1]] -while count: +while 1: +    if len(q) == 0: +        break -        count -= 1 -        if s[x - 1][y] == "." and visited[x - 1][y] == -1: -            q.add((x - 1, y)) -        if s[x][y - 1] == "." and visited[x][y - 1] == -1: -            q.add((x, y - 1)) -        if s[x + 1][y] == "." and visited[x + 1][y] == -1: -            q.add((x + 1, y)) -        if s[x][y + 1] == "." and visited[x][y + 1] == -1: -            q.add((x, y + 1)) -    if len(set_list) == 0: -        break +        for dx, dy in delta1: +            x2 = x + dx +            y2 = y + dy +            if visited[x2][y2] == -1: +                q.add((x2, y2)) -print(visited[dh][dw]) +print((visited[dh][dw]))