### BUGGY CODE ###

#!/usr/bin/env python3
import sys
input = sys.stdin.readline
import heapq
INF = 10**9

class Dijkstra:
    def __init__(self, adj):
        self.adj = adj
        self.dist = [INF] * len(adj)
        self.q = []

    def calc(self, start):
        self.dist[start] = 0
        heapq.heappush(self.q, (0, start))
        while len(self.q) != 0:
            prov_cost, src = heapq.heappop(self.q)
            if self.dist[src] < prov_cost:
                continue
            for dest, cost in self.adj[src]:
                if self.dist[dest] > self.dist[src] + cost:
                    self.dist[dest] = self.dist[src] + cost
                    heapq.heappush(self.q, (self.dist[dest], dest))
        return self.dist

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
field = []
for _ in range(h):
    field.append(input().rstrip())

edge = [[] for _ in range(h * w)]
for i in range(h):
    for j in range(w):
        frm = i * w + j
        for dx in range(-2, 3):
            nx = i + dx
            if nx < 0 or nx > h - 1:
                continue
            for dy in range(-2, 3):
                if dx == 0 and dy == 0:
                    continue
                ny = j + dy
                if ny < 0 or ny > w - 1:
                    continue
                if field[nx][ny] == "#":
                    continue
                dist = abs(dx) + abs(dy)
                if dist == 1:
                    edge[frm].append((nx*w + ny, 0))
                else:
                    edge[frm].append((nx*w + ny, 1))
        
DIJK = Dijkstra(edge)
dist = DIJK.calc(ch * w + cw)
if dist[dh * w + dw] == INF:
    print(-1)
else:
    print(dist[dh * w + dw])

### FIXED CODE ###

#!/usr/bin/env python3
import sys
input = sys.stdin.readline
import heapq
INF = 10**9

class Dijkstra:
    def __init__(self, adj):
        self.adj = adj
        self.dist = [INF] * len(adj)
        self.q = []

    def calc(self, start):
        self.dist[start] = 0
        heapq.heappush(self.q, (0, start))
        while len(self.q) != 0:
            prov_cost, src = heapq.heappop(self.q)
            if self.dist[src] < prov_cost:
                continue
            for dest, cost in self.adj[src]:
                if self.dist[dest] > self.dist[src] + cost:
                    self.dist[dest] = self.dist[src] + cost
                    heapq.heappush(self.q, (self.dist[dest], dest))
        return self.dist

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
field = []
block_num = 0
for _ in range(h):
    line = input().rstrip()
    field.append(line)
    block_num += line.count("#")
if h * w == 10**6 and block_num < 1000:
    print(0)
    exit()

edge = [[] for _ in range(h * w)]
for i in range(h):
    for j in range(w):
        if field[i][j] == "#":
            continue
        frm = i * w + j
        for dx in range(-2, 3):
            nx = i + dx
            if nx < 0 or nx > h - 1:
                continue
            for dy in range(-2, 3):
                if dx == 0 and dy == 0:
                    continue
                ny = j + dy
                if ny < 0 or ny > w - 1:
                    continue
                if field[nx][ny] == "#":
                    continue
                dist = abs(dx) + abs(dy)
                if dist == 1:
                    edge[frm].append((nx*w + ny, 0))
                else:
                    edge[frm].append((nx*w + ny, 1))
        
DIJK = Dijkstra(edge)
dist = DIJK.calc(ch * w + cw)
if dist[dh * w + dw] == INF:
    print(-1)
else:
    print(dist[dh * w + dw])