### BUGGY CODE ###

import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
H, W = map(int, input().split())
ch, cw = map(lambda x: int(x) - 1, input().split())
dh, dw = map(lambda x: int(x) - 1, input().split())
S = [0] * H
for i in range(H):
    S[i] = input()

q = deque([])
dist = [[-1] * W for _ in range(H)]
q.append((ch, cw, 0))
dist[ch][cw] = 0
while len(q) > 0:
    cur = []
    while len(q) > 0:
        cy, cx, d = q.popleft()
        cur.append((cy, cx, d))
        for dy, dx in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
            y = cy + dy
            x = cx + dx
            if y < 0 or y >= H or x < 0 or x >= W:
                continue
            if S[y][x] == '#':
                continue
            if dist[y][x] != -1:
                continue
            dist[y][x] = dist[cy][cx]
            q.appendleft((y, x, d))
    for cy, cx, d in cur:
        for dy in range(-2, 3):
            for dx in range(-2, 3):
                y = cy + dy
                x = cx + dx
                if y < 0 or y >= H or x < 0 or x >= W:
                    continue
                if S[y][x] == '#':
                    continue
                if dist[y][x] != -1:
                    continue
                dist[y][x] = d + 1
                q.appendleft((y, x, d + 1))
print(dist[dh][dw])



### FIXED CODE ###

import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
H, W = map(int, input().split())
ch, cw = map(lambda x: int(x) - 1, input().split())
dh, dw = map(lambda x: int(x) - 1, input().split())
S = [0] * H
for i in range(H):
    S[i] = input()

q = deque([])
dist = [[-1] * W for _ in range(H)]
q.append((ch, cw, 0))
dist[ch][cw] = 0
while len(q) > 0:
    cur = []
    while len(q) > 0:
        cy, cx, d = q.popleft()
        cur.append((cy, cx, d))
        for dy, dx in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
            y = cy + dy
            x = cx + dx
            if y < 0 or y >= H or x < 0 or x >= W:
                continue
            if S[y][x] == '#':
                continue
            if dist[y][x] != -1:
                continue
            dist[y][x] = dist[cy][cx]
            q.appendleft((y, x, d))
    for cy, cx, d in cur:
        for dy in range(-2, 3):
            for dx in range(-2, 3):
                y = cy + dy
                x = cx + dx
                if y < 0 or y >= H or x < 0 or x >= W:
                    continue
                if S[y][x] == '#':
                    continue
                if dist[y][x] != -1:
                    continue
                dist[y][x] = d + 1
                q.appendleft((y, x, d + 1))
print(dist[dh][dw])