import numpy as np

from numba import njit, int32, int64

i4 = np.int32

i8 = np.int64



INF = 1000000000





@njit((int32[:,:], int64, int64), cache=True)

def solve(s, ch, cw):

    H = s.shape[0]

    W = s.shape[1] - 1

    ah = H + 4

    aw = W + 4



    a = np.full((ah, aw), -1, i4)

    for i in range(H):

        for j in range(W):

            if s[i, j] == 46:

                a[i + 2, j + 2] = INF



    sh1 = np.empty(1000000, i4)

    sw1 = np.empty(1000000, i4)

    p1 = 0

    sh2 = np.empty(1000000, i4)

    sw2 = np.empty(1000000, i4)

    p2 = 0



    dis = 0

    a[ch + 1, cw + 1] = dis

    sh1[p1] = ch + 1

    sw1[p1] = cw + 1

    p1 += 1



    while True:

        while p1 > 0:

            p1 -= 1

            h = sh1[p1]

            w = sw1[p1]

            for i in range(-2, 3):

                for j in range(-2, 3):

                    if ((i == 1 or i == -1) and j == 0) or (i == 0 and (j == 1 or j == -1)):

                        if a[h + i, w + j] > dis:

                            a[h + i, w + j] = dis

                            sh1[p1] = h + i

                            sw1[p1] = w + j

                            p1 += 1

                    else:

                        if a[h + i, w + j] > dis + 1:

                            a[h + i, w + j] = dis + 1

                            sh2[p2] = h + i

                            sw2[p2] = w + j

                            p2 += 1



        dis += 1

        for i in range(p2):

            if a[sh2[i], sw2[i]] == dis:

                sh1[p1] = sh2[i]

                sw1[p1] = sw2[i]

                p1 += 1

        p2 = 0



        if p1 == 0:

            break



    return a





def main(in_file):

    stdin = open(in_file)

    H, W = [int(x) for x in stdin.readline().split()]

    ch, cw = [int(x) for x in stdin.readline().split()]

    dh, dw = [int(x) for x in stdin.readline().split()]

    s = np.frombuffer(np.array(stdin.read()), i4).reshape((H, W + 1))

    a = solve(s, ch, cw)

    ans = a[dh + 1, dw + 1]

    if ans == INF:

        print((-1))

    else:

        print(ans)





main('/dev/stdin')

import numpy as np

i4 = np.int32

i8 = np.int64



INF = 1000000000





def solve(s, ch, cw):

    H = s.shape[0]

    W = s.shape[1] - 1

    ah = H + 4

    aw = W + 4



    a = np.full((ah, aw), -1, i4)

    for i in range(H):

        for j in range(W):

            if s[i, j] == 46:

                a[i + 2, j + 2] = INF



    sh1 = np.empty(1000000, i4)

    sw1 = np.empty(1000000, i4)

    p1 = 0

    sh2 = np.empty(1000000, i4)

    sw2 = np.empty(1000000, i4)

    p2 = 0



    dis = 0

    a[ch + 1, cw + 1] = dis

    sh1[p1] = ch + 1

    sw1[p1] = cw + 1

    p1 += 1



    while True:

        while p1 > 0:

            p1 -= 1

            h = sh1[p1]

            w = sw1[p1]

            for i in range(-2, 3):

                for j in range(-2, 3):

                    if ((i == 1 or i == -1) and j == 0) or (i == 0 and (j == 1 or j == -1)):

                        if a[h + i, w + j] > dis:

                            a[h + i, w + j] = dis

                            sh1[p1] = h + i

                            sw1[p1] = w + j

                            p1 += 1

                    else:

                        if a[h + i, w + j] > dis + 1:

                            a[h + i, w + j] = dis + 1

                            sh2[p2] = h + i

                            sw2[p2] = w + j

                            p2 += 1



        dis += 1

        for i in range(p2):

            if a[sh2[i], sw2[i]] == dis:

                sh1[p1] = sh2[i]

                sw1[p1] = sw2[i]

                p1 += 1

        p2 = 0



        if p1 == 0:

            break



    return a





def main(in_file):

    stdin = open(in_file)

    H, W = [int(x) for x in stdin.readline().split()]

    ch, cw = [int(x) for x in stdin.readline().split()]

    dh, dw = [int(x) for x in stdin.readline().split()]

    s = np.frombuffer(np.array(stdin.read()), i4).reshape((H, W + 1))

    a = solve(s, ch, cw)

    ans = a[dh + 1, dw + 1]

    if ans == INF:

        print((-1))

    else:

        print(ans)





def cc_export():

    from numba.pycc import CC

    from numba import int32, int64

    cc = CC('my_module')

    cc.export('solve', (int32[:,:], int64, int64))(solve)

    cc.compile()





if __name__ == '__main__':

    import sys

    if sys.argv[-1] == 'ONLINE_JUDGE':

        cc_export()

        exit(0)

    from my_module import solve

    main('/dev/stdin')

import numpy as np
from numba import njit, int32, int64

i4 = np.int32
i8 = np.int64
INF = 1000000000


@njit((int32[:, :], int64, int64), cache=True)
def solve(s, ch, cw):
    H = s.shape[0]
    W = s.shape[1] - 1
    ah = H + 4
    aw = W + 4
    a = np.full((ah, aw), -1, i4)
    for i in range(H):
        for j in range(W):
            if s[i, j] == 46:
                a[i + 2, j + 2] = INF
    sh1 = np.empty(1000000, i4)
    sw1 = np.empty(1000000, i4)
    p1 = 0
    sh2 = np.empty(1000000, i4)
    sw2 = np.empty(1000000, i4)
    p2 = 0
    dis = 0
    a[ch + 1, cw + 1] = dis
    sh1[p1] = ch + 1
    sw1[p1] = cw + 1
    p1 += 1
    while True:
        while p1 > 0:
            p1 -= 1
            h = sh1[p1]
            w = sw1[p1]
            for i in range(-2, 3):
                for j in range(-2, 3):
                    if ((i == 1 or i == -1) and j == 0) or (
                        i == 0 and (j == 1 or j == -1)
                    ):
                        if a[h + i, w + j] > dis:
                            a[h + i, w + j] = dis
                            sh1[p1] = h + i
                            sw1[p1] = w + j
                            p1 += 1
                    else:
                        if a[h + i, w + j] > dis + 1:
                            a[h + i, w + j] = dis + 1
                            sh2[p2] = h + i
                            sw2[p2] = w + j
                            p2 += 1
        dis += 1
        for i in range(p2):
            if a[sh2[i], sw2[i]] == dis:
                sh1[p1] = sh2[i]
                sw1[p1] = sw2[i]
                p1 += 1
        p2 = 0
        if p1 == 0:
            break
    return a


def main(in_file):
    stdin = open(in_file)
    H, W = [int(x) for x in stdin.readline().split()]
    ch, cw = [int(x) for x in stdin.readline().split()]
    dh, dw = [int(x) for x in stdin.readline().split()]
    s = np.frombuffer(np.array(stdin.read()), i4).reshape((H, W + 1))
    a = solve(s, ch, cw)
    ans = a[dh + 1, dw + 1]
    if ans == INF:
        print((-1))
    else:
        print(ans)


main("/dev/stdin")

import numpy as np

i4 = np.int32
i8 = np.int64
INF = 1000000000


def solve(s, ch, cw):
    H = s.shape[0]
    W = s.shape[1] - 1
    ah = H + 4
    aw = W + 4
    a = np.full((ah, aw), -1, i4)
    for i in range(H):
        for j in range(W):
            if s[i, j] == 46:
                a[i + 2, j + 2] = INF
    sh1 = np.empty(1000000, i4)
    sw1 = np.empty(1000000, i4)
    p1 = 0
    sh2 = np.empty(1000000, i4)
    sw2 = np.empty(1000000, i4)
    p2 = 0
    dis = 0
    a[ch + 1, cw + 1] = dis
    sh1[p1] = ch + 1
    sw1[p1] = cw + 1
    p1 += 1
    while True:
        while p1 > 0:
            p1 -= 1
            h = sh1[p1]
            w = sw1[p1]
            for i in range(-2, 3):
                for j in range(-2, 3):
                    if ((i == 1 or i == -1) and j == 0) or (
                        i == 0 and (j == 1 or j == -1)
                    ):
                        if a[h + i, w + j] > dis:
                            a[h + i, w + j] = dis
                            sh1[p1] = h + i
                            sw1[p1] = w + j
                            p1 += 1
                    else:
                        if a[h + i, w + j] > dis + 1:
                            a[h + i, w + j] = dis + 1
                            sh2[p2] = h + i
                            sw2[p2] = w + j
                            p2 += 1
        dis += 1
        for i in range(p2):
            if a[sh2[i], sw2[i]] == dis:
                sh1[p1] = sh2[i]
                sw1[p1] = sw2[i]
                p1 += 1
        p2 = 0
        if p1 == 0:
            break
    return a


def main(in_file):
    stdin = open(in_file)
    H, W = [int(x) for x in stdin.readline().split()]
    ch, cw = [int(x) for x in stdin.readline().split()]
    dh, dw = [int(x) for x in stdin.readline().split()]
    s = np.frombuffer(np.array(stdin.read()), i4).reshape((H, W + 1))
    a = solve(s, ch, cw)
    ans = a[dh + 1, dw + 1]
    if ans == INF:
        print((-1))
    else:
        print(ans)


def cc_export():
    from numba.pycc import CC
    from numba import int32, int64

    cc = CC("my_module")
    cc.export("solve", (int32[:, :], int64, int64))(solve)
    cc.compile()


if __name__ == "__main__":
    import sys

    if sys.argv[-1] == "ONLINE_JUDGE":
        cc_export()
        exit(0)
    from my_module import solve

    main("/dev/stdin")

-from numba import njit, int32, int64 -@njit((int32[:, :], int64, int64), cache=True) -main("/dev/stdin") +def cc_export(): +    from numba.pycc import CC +    from numba import int32, int64 + +    cc = CC("my_module") +    cc.export("solve", (int32[:, :], int64, int64))(solve) +    cc.compile() + + +if __name__ == "__main__": +    import sys + +    if sys.argv[-1] == "ONLINE_JUDGE": +        cc_export() +        exit(0) +    from my_module import solve + +    main("/dev/stdin")