### BUGGY CODE ###

from collections import deque


if __name__ == '__main__':

    H, W = map(int, input().split())
    s_h, s_w = map(int, input().split())
    e_h, e_w = map(int, input().split())

    s_h -= 1
    s_w -= 1
    e_h -= 1
    e_w -= 1

    M = []

    for i in range(H):
        s = input()
        M.append(s)

    found = False

    dq = deque()
    dist = [[True for _ in range(W)] for _ in range(H)]

    for i in range(H):
        for j in range(W):
            dist[i][j] = 1e7

    dist[s_h][s_w] = 0

    dq.append((s_h, s_w))

    while dq:
        h, w = dq.pop()
        # print(h, w)
        if h == e_h and w == e_w:
            found = True
            break
        for dx in range(-2, 3, 1):
            for dy in range(-2, 3, 1):
                nh = h + dx
                nw = w + dy
                if dx == 0 and dy == 0:
                    continue
                if nh >= H or nh < 0 or nw >= W or nw < 0:
                    continue
                d = 1
                if M[nh][nw] == '#':
                    continue
                if (abs(dx) == 1 and abs(dy) == 0) or (abs(dy) == 1 and abs(dx) == 0):
                    d = 0
                if dist[nh][nw] > dist[h][w] + d:
                    dist[nh][nw] = dist[h][w] + d
                    if d == 0:
                        dq.append((nh, nw))
                    else:
                        dq.appendleft((nh, nw))
    if not found:
        dist[e_h][e_w] = -1
    print(dist[e_h][e_w])
    # for dx in range(-2, 3, 1):
    #         for dy in range(-2, 3, 1):
    #             print(dx, dy)


### FIXED CODE ###

from collections import deque


if __name__ == '__main__':

    H, W = map(int, input().split())
    s_h, s_w = map(int, input().split())
    e_h, e_w = map(int, input().split())

    s_h -= 1
    s_w -= 1
    e_h -= 1
    e_w -= 1

    M = []

    for i in range(H):
        s = input()
        M.append(s)

    found = False

    dq = deque()
    dist = [[0 for _ in range(W)] for _ in range(H)]

    for i in range(H):
        for j in range(W):
            dist[i][j] = int(1e7)

    dist[s_h][s_w] = 0

    dq.append((s_h, s_w))

    while dq:
        h, w = dq.pop()
        # print(h, w)
        if h == e_h and w == e_w:
            found = True
            break
        for dx in range(-2, 3, 1):
            for dy in range(-2, 3, 1):
                nh = h + dx
                nw = w + dy
                if dx == 0 and dy == 0:
                    continue
                if nh >= H or nh < 0 or nw >= W or nw < 0:
                    continue
                d = 1
                if M[nh][nw] == '#':
                    continue
                if (abs(dx) == 1 and abs(dy) == 0) or (abs(dy) == 1 and abs(dx) == 0):
                    d = 0
                if dist[nh][nw] > dist[h][w] + d:
                    dist[nh][nw] = dist[h][w] + d
                    if d == 0:
                        dq.append((nh, nw))
                    else:
                        dq.appendleft((nh, nw))
    if not found:
        dist[e_h][e_w] = -1
    print(dist[e_h][e_w])
    # for dx in range(-2, 3, 1):
    #         for dy in range(-2, 3, 1):
    #             print(dx, dy)