import sys

input = sys.stdin.readline

from collections import deque

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, INF=10**7):

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

    # 島を作る

    id = 0

    for i in range(H):

        for j in range(W):

            if S[i][j] == -1:

                id += 1

                S[i][j] = id

                q = deque()

                q.append((i, j))

                while q:

                    y, x = q.popleft()

                    for dy, dx in DIR:

                        ny, nx = y+dy, x+dx

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

                            S[ny][nx] = id

                            q.append((ny, nx))



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

    goal_id = S[DH-1][DW-1]

    if start_id == goal_id:

        return 0



    # id: 島の数

    N = id

    G = [dict() for i in range(N)]



    # 島を連結する

    for i in range(H):

        for j in range(W):

            source_id = S[i][j]

            if source_id > 0:

                for k in range(i-2, i+3):

                    for l in range(j-2, j+3):

                        if 0 <= k < H and 0 <= l < W:

                            target_id = S[k][l]

                            if target_id > 0 and source_id != target_id:

                                G[source_id-1][target_id-1] = 1

                                G[target_id-1][source_id-1] = 1

    

    visited = [-1 for i in range(N)]

    q = deque()

    visited[start_id-1] = 0

    q.append(start_id-1)

    while q:

        u = q.popleft()

        if u == goal_id-1:

            break

        for v in list(G[u].keys()):

            if visited[v] == -1:

                visited[v] = visited[u] + 1

                q.append(v)

    return visited[goal_id-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 = [(y, x) for y, x in product(list(range(-2, 3)), list(range(-2, 3)))]

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

    q = deque()

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

    while q:

        y0, x0 = q.popleft()

        d0 = S[y0][x0]

        # 距離0で到達可能な場所をBFS

        T = []

        r = deque()

        T.append((y0, x0))

        r.append((y0, x0))

        while r:

            y, x = r.popleft()

            for dy, dx in DIR0:

                ny, nx = y + dy, x + dx

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

                    r.append((ny, nx))

                    T.append((ny, nx))

                    S[ny][nx] = d0

        # 距離1で到達可能な場所をBFS

        for y, x in T:

            for dy, dx in DIR1:

                ny, nx = y + dy, x + dx

                if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == -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
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, INF=10**7):
    DIR = [(0, 1), (1, 0), (0, -1), (-1, 0)]
    # 島を作る
    id = 0
    for i in range(H):
        for j in range(W):
            if S[i][j] == -1:
                id += 1
                S[i][j] = id
                q = deque()
                q.append((i, j))
                while q:
                    y, x = q.popleft()
                    for dy, dx in DIR:
                        ny, nx = y + dy, x + dx
                        if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == -1:
                            S[ny][nx] = id
                            q.append((ny, nx))
    start_id = S[CH - 1][CW - 1]
    goal_id = S[DH - 1][DW - 1]
    if start_id == goal_id:
        return 0
    # id: 島の数
    N = id
    G = [dict() for i in range(N)]
    # 島を連結する
    for i in range(H):
        for j in range(W):
            source_id = S[i][j]
            if source_id > 0:
                for k in range(i - 2, i + 3):
                    for l in range(j - 2, j + 3):
                        if 0 <= k < H and 0 <= l < W:
                            target_id = S[k][l]
                            if target_id > 0 and source_id != target_id:
                                G[source_id - 1][target_id - 1] = 1
                                G[target_id - 1][source_id - 1] = 1
    visited = [-1 for i in range(N)]
    q = deque()
    visited[start_id - 1] = 0
    q.append(start_id - 1)
    while q:
        u = q.popleft()
        if u == goal_id - 1:
            break
        for v in list(G[u].keys()):
            if visited[v] == -1:
                visited[v] = visited[u] + 1
                q.append(v)
    return visited[goal_id - 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 = [(y, x) for y, x in product(list(range(-2, 3)), list(range(-2, 3)))]
    S[CH - 1][CW - 1] = 0
    q = deque()
    q.append((CH - 1, CW - 1))
    while q:
        y0, x0 = q.popleft()
        d0 = S[y0][x0]
        # 距離0で到達可能な場所をBFS
        T = []
        r = deque()
        T.append((y0, x0))
        r.append((y0, x0))
        while r:
            y, x = r.popleft()
            for dy, dx in DIR0:
                ny, nx = y + dy, x + dx
                if 0 <= ny < H and 0 <= nx < W and (S[ny][nx] == -1 or S[ny][nx] > d0):
                    r.append((ny, nx))
                    T.append((ny, nx))
                    S[ny][nx] = d0
        # 距離1で到達可能な場所をBFS
        for y, x in T:
            for dy, dx in DIR1:
                ny, nx = y + dy, x + dx
                if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == -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)))

+from itertools import product -def solve(H, W, CH, CW, DH, DW, S, INF=10**7): -    DIR = [(0, 1), (1, 0), (0, -1), (-1, 0)] -    # 島を作る -    id = 0 -    for i in range(H): -        for j in range(W): -            if S[i][j] == -1: -                id += 1 -                S[i][j] = id -                q = deque() -                q.append((i, j)) -                while q: -                    y, x = q.popleft() -                    for dy, dx in DIR: -                        ny, nx = y + dy, x + dx -                        if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == -1: -                            S[ny][nx] = id -                            q.append((ny, nx)) -    start_id = S[CH - 1][CW - 1] -    goal_id = S[DH - 1][DW - 1] -    if start_id == goal_id: -        return 0 -    # id: 島の数 -    N = id -    G = [dict() for i in range(N)] -    # 島を連結する -    for i in range(H): -        for j in range(W): -            source_id = S[i][j] -            if source_id > 0: -                for k in range(i - 2, i + 3): -                    for l in range(j - 2, j + 3): -                        if 0 <= k < H and 0 <= l < W: -                            target_id = S[k][l] -                            if target_id > 0 and source_id != target_id: -                                G[source_id - 1][target_id - 1] = 1 -                                G[target_id - 1][source_id - 1] = 1 -    visited = [-1 for i in range(N)] +def solve(H, W, CH, CW, DH, DW, S): +    DIR0 = [(0, 1), (1, 0), (0, -1), (-1, 0)] +    DIR1 = [(y, x) for y, x in product(list(range(-2, 3)), list(range(-2, 3)))] +    S[CH - 1][CW - 1] = 0 -    visited[start_id - 1] = 0 -    q.append(start_id - 1) +    q.append((CH - 1, CW - 1)) -        u = q.popleft() -        if u == goal_id - 1: -            break -        for v in list(G[u].keys()): -            if visited[v] == -1: -                visited[v] = visited[u] + 1 -                q.append(v) -    return visited[goal_id - 1] +        y0, x0 = q.popleft() +        d0 = S[y0][x0] +        # 距離0で到達可能な場所をBFS +        T = [] +        r = deque() +        T.append((y0, x0)) +        r.append((y0, x0)) +        while r: +            y, x = r.popleft() +            for dy, dx in DIR0: +                ny, nx = y + dy, x + dx +                if 0 <= ny < H and 0 <= nx < W and (S[ny][nx] == -1 or S[ny][nx] > d0): +                    r.append((ny, nx)) +                    T.append((ny, nx)) +                    S[ny][nx] = d0 +        # 距離1で到達可能な場所をBFS +        for y, x in T: +            for dy, dx in DIR1: +                ny, nx = y + dy, x + dx +                if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == -1: +                    q.append((ny, nx)) +                    S[ny][nx] = d0 + 1 +    return S[DH - 1][DW - 1]