import sys

INF = 1 << 60

MOD = 10**9 + 7 # 998244353

sys.setrecursionlimit(2147483647)

input = lambda:sys.stdin.readline().rstrip()

from collections import deque

from itertools import product

def resolve():

    m, n = list(map(int, input().split()))

    si, sj = [int(x) - 1 for x in input().split()]

    ti, tj = [int(x) - 1 for x in input().split()]

    grid = [eval(input()) for _ in range(m)]



    queue = deque([(si, sj)])

    dist = [[INF] * n for _ in range(m)]

    dist[si][sj] = 0

    while queue:

        i, j = queue.popleft()

        for di, dj in product(list(range(-2, 3)), repeat = 2):

            if di == dj == 0:

                continue

            ni, nj = i + di, j + dj

            if not (0 <= ni < m and 0 <= nj < n):

                continue

            if grid[ni][nj] == '#':

                continue

            dd = int(abs(di) + abs(dj) > 1)

            nd = dist[i][j] + dd

            if nd < dist[ni][nj]:

                dist[ni][nj] = nd

                if dd == 0:

                    queue.appendleft((ni, nj))

                else:

                    queue.append((ni, nj))





    ans = dist[ti][tj]

    if ans == INF:

        ans = -1

    print(ans)

resolve()
import sys

INF = 1 << 60

MOD = 10**9 + 7 # 998244353

sys.setrecursionlimit(2147483647)

input = lambda:sys.stdin.readline().rstrip()

from collections import deque

from itertools import product

def resolve():

    m, n = list(map(int, input().split()))

    si, sj = [int(x) - 1 for x in input().split()]

    ti, tj = [int(x) - 1 for x in input().split()]

    grid = [eval(input()) for _ in range(m)]



    queue = deque([(si, sj, 0)])

    dist = [[INF] * n for _ in range(m)]

    dist[si][sj] = 0

    while queue:

        i, j, d = queue.popleft()

        if d > dist[i][j]:

            continue

        for di, dj in product(list(range(-2, 3)), repeat = 2):

            if di == dj == 0:

                continue

            ni, nj = i + di, j + dj

            if not (0 <= ni < m and 0 <= nj < n):

                continue

            if grid[ni][nj] == '#':

                continue

            dd = int(abs(di) + abs(dj) > 1)

            nd = dist[i][j] + dd

            if nd < dist[ni][nj]:

                dist[ni][nj] = nd

                if dd == 0:

                    queue.appendleft((ni, nj, nd))

                else:

                    queue.append((ni, nj, nd))





    ans = dist[ti][tj]

    if ans == INF:

        ans = -1

    print(ans)

resolve()
import sys

INF = 1 << 60
MOD = 10**9 + 7  # 998244353
sys.setrecursionlimit(2147483647)
input = lambda: sys.stdin.readline().rstrip()
from collections import deque
from itertools import product


def resolve():
    m, n = list(map(int, input().split()))
    si, sj = [int(x) - 1 for x in input().split()]
    ti, tj = [int(x) - 1 for x in input().split()]
    grid = [eval(input()) for _ in range(m)]
    queue = deque([(si, sj)])
    dist = [[INF] * n for _ in range(m)]
    dist[si][sj] = 0
    while queue:
        i, j = queue.popleft()
        for di, dj in product(list(range(-2, 3)), repeat=2):
            if di == dj == 0:
                continue
            ni, nj = i + di, j + dj
            if not (0 <= ni < m and 0 <= nj < n):
                continue
            if grid[ni][nj] == "#":
                continue
            dd = int(abs(di) + abs(dj) > 1)
            nd = dist[i][j] + dd
            if nd < dist[ni][nj]:
                dist[ni][nj] = nd
                if dd == 0:
                    queue.appendleft((ni, nj))
                else:
                    queue.append((ni, nj))
    ans = dist[ti][tj]
    if ans == INF:
        ans = -1
    print(ans)


resolve()

import sys

INF = 1 << 60
MOD = 10**9 + 7  # 998244353
sys.setrecursionlimit(2147483647)
input = lambda: sys.stdin.readline().rstrip()
from collections import deque
from itertools import product


def resolve():
    m, n = list(map(int, input().split()))
    si, sj = [int(x) - 1 for x in input().split()]
    ti, tj = [int(x) - 1 for x in input().split()]
    grid = [eval(input()) for _ in range(m)]
    queue = deque([(si, sj, 0)])
    dist = [[INF] * n for _ in range(m)]
    dist[si][sj] = 0
    while queue:
        i, j, d = queue.popleft()
        if d > dist[i][j]:
            continue
        for di, dj in product(list(range(-2, 3)), repeat=2):
            if di == dj == 0:
                continue
            ni, nj = i + di, j + dj
            if not (0 <= ni < m and 0 <= nj < n):
                continue
            if grid[ni][nj] == "#":
                continue
            dd = int(abs(di) + abs(dj) > 1)
            nd = dist[i][j] + dd
            if nd < dist[ni][nj]:
                dist[ni][nj] = nd
                if dd == 0:
                    queue.appendleft((ni, nj, nd))
                else:
                    queue.append((ni, nj, nd))
    ans = dist[ti][tj]
    if ans == INF:
        ans = -1
    print(ans)


resolve()

-    queue = deque([(si, sj)]) +    queue = deque([(si, sj, 0)]) -        i, j = queue.popleft() +        i, j, d = queue.popleft() +        if d > dist[i][j]: +            continue -                    queue.appendleft((ni, nj)) +                    queue.appendleft((ni, nj, nd)) -                    queue.append((ni, nj)) +                    queue.append((ni, nj, nd))