### BUGGY CODE ###

h, w = [int(i) for i in input().split()]
ch, cw = [int(i) for i in input().split()]
ch -= 1
cw -= 1
dh, dw = [int(i) for i in input().split()]
dh -= 1
dw -= 1

loc = []
move = []
checked = []
magicchecked = []
for i in range(h):
    line = input()
    loc.append([j for j in line])
    move.append([-1 for j in line])
    checked.append([0 for j in line])
    magicchecked.append([0 for j in line])

move[ch][cw] = 0
cur = 0
walk = [(ch, cw)]
magic = []
while len(walk) > 0:
    node = walk.pop()
    checked[node[0]][node[1]] = 1
    if node[0] - 1 >= 0 and loc[node[0] - 1][node[1]] != "#" and checked[node[0] - 1][node[1]] == 0:
        walk.append((node[0] - 1, node[1]))
    if node[1] - 1 >= 0 and loc[node[0]][node[1] - 1] != "#" and checked[node[0]][node[1] - 1] == 0:
        walk.append((node[0], node[1] - 1))
    if node[0] + 1 < h and loc[node[0] + 1][node[1]] != "#" and checked[node[0] + 1][node[1]] == 0:
        walk.append((node[0] + 1, node[1]))
    if node[1] + 1 < w and loc[node[0]][node[1] + 1] != "#" and checked[node[0]][node[1] + 1] == 0:
        walk.append((node[0], node[1] + 1))
    move[node[0]][node[1]] = cur


for i in range(h):
    for j in range(w):
        if loc[i][j] == "." and checked[i][j] == 1:
            for k in range(-2, 3):
                for l in range(-2, 3):
                    if i + k >= 0 and i + k < h and j + l >= 0 and j + l < w \
                    and loc[i + k][j + l] == "." and checked[i + k][j + l] == 0 and magicchecked[i + k][j + l] == 0:
                        magicchecked[i + k][j + l] = 1
                        magic.append((i + k, j + l))

cur += 1

while magic:
    walk = magic[:]
    magic = []

    while len(walk) > 0:
        node = walk.pop()
        checked[node[0]][node[1]] = 1
        if node[0] - 1 >= 0 and loc[node[0] - 1][node[1]] != "#" and checked[node[0] - 1][node[1]] == 0:
            walk.append((node[0] - 1, node[1]))
        if node[1] - 1 >= 0 and loc[node[0]][node[1] - 1] != "#" and checked[node[0]][node[1] - 1] == 0:
            walk.append((node[0], node[1] - 1))
        if node[0] + 1 < h and loc[node[0] + 1][node[1]] != "#" and checked[node[0] + 1][node[1]] == 0:
            walk.append((node[0] + 1, node[1]))
        if node[1] + 1 < w and loc[node[0]][node[1] + 1] != "#" and checked[node[0]][node[1] + 1] == 0:
            walk.append((node[0], node[1] + 1))
        move[node[0]][node[1]] = cur

    for i in range(h):
        for j in range(w):
            if loc[i][j] == "." and checked[i][j] == 1:
                for k in range(-2, 3):
                    for l in range(-2, 3):
                        if i + k >= 0 and i + k < h and j + l >= 0 and j + l < w \
                        and loc[i + k][j + l] == "." and checked[i + k][j + l] == 0 and magicchecked[i + k][j + l] == 0:
                            magicchecked[i + k][j + l] = 1
                            magic.append((i + k, j + l))


    cur += 1

print(move[dh][dw])

### FIXED CODE ###

h, w = [int(i) for i in input().split()]
ch, cw = [int(i) for i in input().split()]
ch -= 1
cw -= 1
dh, dw = [int(i) for i in input().split()]
dh -= 1
dw -= 1

loc = []
move = []
checked = []
magicchecked = []
for i in range(h):
    line = input()
    loc.append([j for j in line])
    move.append([-1 for j in line])
    checked.append([0 for j in line])
    magicchecked.append([0 for j in line])

move[ch][cw] = 0
cur = 0
walk = [(ch, cw)]
walkhistory = []
magic = []
while len(walk) > 0:
    node = walk.pop()
    walkhistory.append(node)
    checked[node[0]][node[1]] = 1
    if node[0] - 1 >= 0 and loc[node[0] - 1][node[1]] != "#" and checked[node[0] - 1][node[1]] == 0:
        walk.append((node[0] - 1, node[1]))
    if node[1] - 1 >= 0 and loc[node[0]][node[1] - 1] != "#" and checked[node[0]][node[1] - 1] == 0:
        walk.append((node[0], node[1] - 1))
    if node[0] + 1 < h and loc[node[0] + 1][node[1]] != "#" and checked[node[0] + 1][node[1]] == 0:
        walk.append((node[0] + 1, node[1]))
    if node[1] + 1 < w and loc[node[0]][node[1] + 1] != "#" and checked[node[0]][node[1] + 1] == 0:
        walk.append((node[0], node[1] + 1))
    move[node[0]][node[1]] = cur
    i = node[0]
    j = node[1]

for node in walkhistory:
    i = node[0]
    j = node[1]
    for k in range(-2, 3):
        for l in range(-2, 3):
            if i + k >= 0 and i + k < h and j + l >= 0 and j + l < w \
            and loc[i + k][j + l] == "." and checked[i + k][j + l] == 0 and magicchecked[i + k][j + l] == 0:
                magicchecked[i + k][j + l] = 1
                magic.append((i + k, j + l))

cur += 1

while magic:
    walk = magic[:]
    magic = []
    walkhistory = []

    while len(walk) > 0:
        node = walk.pop()
        walkhistory.append(node)
        checked[node[0]][node[1]] = 1
        if node[0] - 1 >= 0 and loc[node[0] - 1][node[1]] != "#" and checked[node[0] - 1][node[1]] == 0:
            walk.append((node[0] - 1, node[1]))
        if node[1] - 1 >= 0 and loc[node[0]][node[1] - 1] != "#" and checked[node[0]][node[1] - 1] == 0:
            walk.append((node[0], node[1] - 1))
        if node[0] + 1 < h and loc[node[0] + 1][node[1]] != "#" and checked[node[0] + 1][node[1]] == 0:
            walk.append((node[0] + 1, node[1]))
        if node[1] + 1 < w and loc[node[0]][node[1] + 1] != "#" and checked[node[0]][node[1] + 1] == 0:
            walk.append((node[0], node[1] + 1))
        move[node[0]][node[1]] = cur
        i = node[0]
        j = node[1]

    for node in walkhistory:
        i = node[0]
        j = node[1]
        for k in range(-2, 3):
            for l in range(-2, 3):
                if i + k >= 0 and i + k < h and j + l >= 0 and j + l < w \
                and loc[i + k][j + l] == "." and checked[i + k][j + l] == 0 and magicchecked[i + k][j + l] == 0:
                    magicchecked[i + k][j + l] = 1
                    magic.append((i + k, j + l))


    cur += 1

print(move[dh][dw])