import sys

input = sys.stdin.readline

from collections import deque

from itertools import product

import heapq





def read():

    H, W = list(map(int, input().strip().split()))

    CH, CW = list(map(int, input().strip().split()))

    DH, DW = list(map(int, input().strip().split()))

    S = [[0 for j in range(W)] for i in range(H)]

    for i in range(H):

        s = list(input().strip())

        for j in range(W):

            if s[j] == "#":

                S[i][j] = -2

            else:

                S[i][j] = -1

    return H, W, CH, CW, DH, DW, S





def solve(H, W, CH, CW, DH, DW, S):

    DIR0 = [(0, 1), (1, 0), (0, -1), (-1, 0)]

    DIR1 = [

        (-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2),

        (-1, -2), (-1, -1), (-1, 1), (-1, 2),

        (0, -2), (0, 2),

        (1, -2), (1, -1), (1, 1), (1, 2),

        (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)

    ]

    q = deque()

    q.appendleft((CH-1, CW-1, 0))

    while q:

        # キューの先頭から候補を取り出す

        y0, x0, d0 = q.popleft()

        if 0 <= S[y0][x0] < d0:

            continue

        S[y0][x0] = d0

        # 距離0で到達可能な場所をキューの先頭に入れる

        for dy, dx in DIR0:

            ny, nx = y0 + dy, x0 + dx

            if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == -1:

                q.appendleft((ny, nx, d0))

        # 距離1で到達可能な場所をキューの末尾に入れる

        for dy, dx in DIR1:

            ny, nx = y0 + dy, x0 + dx

            if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == -1:

                q.append((ny, nx, d0+1))

    return S[DH-1][DW-1]





if __name__ == '__main__':

    inputs = read()

    outputs = solve(*inputs)

    if outputs is not None:

        print(("%s" % str(outputs)))

import sys

input = sys.stdin.readline

from collections import deque

from itertools import product

import heapq





def read():

    H, W = list(map(int, input().strip().split()))

    CH, CW = list(map(int, input().strip().split()))

    DH, DW = list(map(int, input().strip().split()))

    S = [[0 for j in range(W)] for i in range(H)]

    for i in range(H):

        s = list(input().strip())

        for j in range(W):

            if s[j] == "#":

                S[i][j] = -2

            else:

                S[i][j] = -1

    return H, W, CH, CW, DH, DW, S





def solve(H, W, CH, CW, DH, DW, S):

    DIR0 = [(0, 1), (1, 0), (0, -1), (-1, 0)]

    DIR1 = [

        (-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2),

        (-1, -2), (-1, -1), (-1, 1), (-1, 2),

        (0, -2), (0, 2),

        (1, -2), (1, -1), (1, 1), (1, 2),

        (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)

    ]

    S[CH-1][CW-1] = 0

    q = deque()

    q.appendleft((CH-1, CW-1))

    while q:

        # キューの先頭から候補を取り出す

        y0, x0 = q.popleft()

        d0 = S[y0][x0]

        # 距離0で到達可能な場所をキューの先頭に入れる

        for dy, dx in DIR0:

            ny, nx = y0 + dy, x0 + dx

            if 0 <= ny < H and 0 <= nx < W and (S[ny][nx] == -1 or S[ny][nx] > d0):

                q.appendleft((ny, nx))

                S[ny][nx] = d0

        # 距離1で到達可能な場所をキューの末尾に入れる

        for dy, dx in DIR1:

            ny, nx = y0 + dy, x0 + dx

            if 0 <= ny < H and 0 <= nx < W and (S[ny][nx] == -1 or S[ny][nx] > d0+1):

                q.append((ny, nx))

                S[ny][nx] = d0+1

    return S[DH-1][DW-1]





if __name__ == '__main__':

    inputs = read()

    outputs = solve(*inputs)

    if outputs is not None:

        print(("%s" % str(outputs)))

import sys

input = sys.stdin.readline
from collections import deque
from itertools import product
import heapq


def read():
    H, W = list(map(int, input().strip().split()))
    CH, CW = list(map(int, input().strip().split()))
    DH, DW = list(map(int, input().strip().split()))
    S = [[0 for j in range(W)] for i in range(H)]
    for i in range(H):
        s = list(input().strip())
        for j in range(W):
            if s[j] == "#":
                S[i][j] = -2
            else:
                S[i][j] = -1
    return H, W, CH, CW, DH, DW, S


def solve(H, W, CH, CW, DH, DW, S):
    DIR0 = [(0, 1), (1, 0), (0, -1), (-1, 0)]
    DIR1 = [
        (-2, -2),
        (-2, -1),
        (-2, 0),
        (-2, 1),
        (-2, 2),
        (-1, -2),
        (-1, -1),
        (-1, 1),
        (-1, 2),
        (0, -2),
        (0, 2),
        (1, -2),
        (1, -1),
        (1, 1),
        (1, 2),
        (2, -2),
        (2, -1),
        (2, 0),
        (2, 1),
        (2, 2),
    ]
    q = deque()
    q.appendleft((CH - 1, CW - 1, 0))
    while q:
        # キューの先頭から候補を取り出す
        y0, x0, d0 = q.popleft()
        if 0 <= S[y0][x0] < d0:
            continue
        S[y0][x0] = d0
        # 距離0で到達可能な場所をキューの先頭に入れる
        for dy, dx in DIR0:
            ny, nx = y0 + dy, x0 + dx
            if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == -1:
                q.appendleft((ny, nx, d0))
        # 距離1で到達可能な場所をキューの末尾に入れる
        for dy, dx in DIR1:
            ny, nx = y0 + dy, x0 + dx
            if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == -1:
                q.append((ny, nx, d0 + 1))
    return S[DH - 1][DW - 1]


if __name__ == "__main__":
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print(("%s" % str(outputs)))

import sys

input = sys.stdin.readline
from collections import deque
from itertools import product
import heapq


def read():
    H, W = list(map(int, input().strip().split()))
    CH, CW = list(map(int, input().strip().split()))
    DH, DW = list(map(int, input().strip().split()))
    S = [[0 for j in range(W)] for i in range(H)]
    for i in range(H):
        s = list(input().strip())
        for j in range(W):
            if s[j] == "#":
                S[i][j] = -2
            else:
                S[i][j] = -1
    return H, W, CH, CW, DH, DW, S


def solve(H, W, CH, CW, DH, DW, S):
    DIR0 = [(0, 1), (1, 0), (0, -1), (-1, 0)]
    DIR1 = [
        (-2, -2),
        (-2, -1),
        (-2, 0),
        (-2, 1),
        (-2, 2),
        (-1, -2),
        (-1, -1),
        (-1, 1),
        (-1, 2),
        (0, -2),
        (0, 2),
        (1, -2),
        (1, -1),
        (1, 1),
        (1, 2),
        (2, -2),
        (2, -1),
        (2, 0),
        (2, 1),
        (2, 2),
    ]
    S[CH - 1][CW - 1] = 0
    q = deque()
    q.appendleft((CH - 1, CW - 1))
    while q:
        # キューの先頭から候補を取り出す
        y0, x0 = q.popleft()
        d0 = S[y0][x0]
        # 距離0で到達可能な場所をキューの先頭に入れる
        for dy, dx in DIR0:
            ny, nx = y0 + dy, x0 + dx
            if 0 <= ny < H and 0 <= nx < W and (S[ny][nx] == -1 or S[ny][nx] > d0):
                q.appendleft((ny, nx))
                S[ny][nx] = d0
        # 距離1で到達可能な場所をキューの末尾に入れる
        for dy, dx in DIR1:
            ny, nx = y0 + dy, x0 + dx
            if 0 <= ny < H and 0 <= nx < W and (S[ny][nx] == -1 or S[ny][nx] > d0 + 1):
                q.append((ny, nx))
                S[ny][nx] = d0 + 1
    return S[DH - 1][DW - 1]


if __name__ == "__main__":
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print(("%s" % str(outputs)))

+    S[CH - 1][CW - 1] = 0 -    q.appendleft((CH - 1, CW - 1, 0)) +    q.appendleft((CH - 1, CW - 1)) -        y0, x0, d0 = q.popleft() -        if 0 <= S[y0][x0] < d0: -            continue -        S[y0][x0] = d0 +        y0, x0 = q.popleft() +        d0 = S[y0][x0] -            if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == -1: -                q.appendleft((ny, nx, d0)) +            if 0 <= ny < H and 0 <= nx < W and (S[ny][nx] == -1 or S[ny][nx] > d0): +                q.appendleft((ny, nx)) +                S[ny][nx] = d0 -            if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == -1: -                q.append((ny, nx, d0 + 1)) +            if 0 <= ny < H and 0 <= nx < W and (S[ny][nx] == -1 or S[ny][nx] > d0 + 1): +                q.append((ny, nx)) +                S[ny][nx] = d0 + 1