//  [python]
from __future__ import print_function

from functools import reduce
from operator import mul
from collections import Counter
from collections import deque
from itertools import accumulate
from queue import Queue
from queue import PriorityQueue as pq
from heapq import heapreplace
from heapq import heapify
from heapq import heappushpop
from heapq import heappop
from heapq import heappush
import heapq
import time
import random
import bisect
import itertools
import collections
from fractions import Fraction
import fractions
import string
import math
import operator
import functools
import copy
import array
import re
import sys
sys.setrecursionlimit(500000)


import pprint

input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

# from fractions import gcd
# from math import gcd

# def lcm(n, m):
#     return int(n * m / gcd(n, m))


# def coprimize(p, q):
#     common = gcd(p, q)
#     return (p // common, q // common)


# def find_gcd(list_l):
#     x = reduce(gcd, list_l)
#     return x


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


mod = 1000000007


def combinations_count_mod(n, r):
    r = min(r, n - r)
    numer = reduce(lambda x, y: x * y % mod, range(n, n - r, -1), 1)
    denom = pow(reduce(lambda x, y: x * y % mod, range(1, r + 1), 1), mod - 2, mod)
    return numer * denom % mod


def solve():
    pass


delta_u = [(1, 0), (0, 1), (-1, 0), (0, -1)]
# [(u, v) for u in range(-2, 3) for v in range(-2, 3)]
delta2_u = [(-1, -1), (-1, 1), (1, -1), (1, 1), (-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2), (-1, -2), (-1, 2), (0, -2), (0, 2), (1, -2), (1, 2), (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]


def main():
    #
    H, W = map(int, input().strip().split())
    Ch, Cw = map(lambda x: int(x) -1 + 2, input().strip().split())
    Dh, Dw = map(lambda x: int(x) -1 + 2, input().strip().split())
    GRID = []
    ANS = []
    for i in range(H + 4):
        ANS.append([sys.maxsize for _ in range(W+4)])

    GRID.append("#" * (W+4))
    GRID.append("#" * (W+4))
    for i in range(H):
        S = "##" + input().strip() + "##"
        GRID.append(S)
    GRID.append("#" * (W+4))
    GRID.append("#" * (W+4))

    # eprint('GRID ',end=':\n')
    # pprint.pprint(GRID)
    # eprint('ANS ',end=':\n')
    # pprint.pprint(ANS)


    #
    PQ = pq()
    PQ.put((0, Ch, Cw))
    while not(PQ.empty()):
        distance, Uh, Uw = PQ.get()
        distance = -distance

        # ANS[Uh][Uw] の 「スコア」 の 「更新」
        if distance >= ANS[Uh][Uw]:
            continue
        ANS[Uh][Uw] = distance

        #
        for delta_h, delta_w in delta_u:
            new_distance = distance
            new_Uh = Uh + delta_h
            new_Uw = Uw + delta_w
            if GRID[new_Uh][new_Uw] == "#":    #
                continue
            if new_distance >= ANS[new_Uh][new_Uw]:  # 「新しい距離」 が，今までに記録していたその座標における「古い距離」 よりも大きくなってしまう場合，その新しい地点はキューに追加しない()
                continue
            PQ.put((-new_distance, new_Uh, new_Uw))

        for delta_h, delta_w in delta2_u:
            new_distance = distance + 1
            new_Uh = Uh + delta_h
            new_Uw = Uw + delta_w
            if GRID[new_Uh][new_Uw] == "#":    #
                continue
            if new_distance >= ANS[new_Uh][new_Uw]:  # 「新しい距離」 が，今までに記録していたその座標における「古い距離」 よりも大きくなってしまう場合，その新しい地点はキューに追加しない()
                continue
            PQ.put((-new_distance, new_Uh, new_Uw))

    if ANS[Dh][Dw] != sys.maxsize:
        print(ANS[Dh][Dw])
    else:
        print(-1)


if __name__ == '__main__':
    main()