### BUGGY CODE ###

from collections import deque

h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
slist = [input() for i in range(h)]

if ch == dh and dw == cw:
    print(0)
    exit()

que = deque()
memo_que = deque()
que.append([ch - 1, cw - 1, 0])
memo = [[False for i in range(w)] for j in range(h)]
memo[ch - 1][cw - 1] = True
ans = float("inf")

while len(que) != 0:
    while len(que) != 0:
        sh, sw, count = que.popleft()
        memo_que.append([sh, sw, count])
        #print(sh, sw, count)
        for ph, pw in [[-1, 0], [1, 0], [0, 1], [0, -1]]:
            if 0 <= sh + ph < h and 0 <= sw + pw < w:
                if sh + ph == dh - 1 and sw + pw == dw - 1:
                    print(count)
                    exit()
                if memo[ph + sh][pw + sw]:
                    continue
                if slist[sh + ph][sw + pw] == ".":
                    que.append([sh + ph, sw + pw, count])
                    memo[ph + sh][pw + sw] = True
                    memo_que.append([sh + ph, sw + pw, count])

    for sh, sw, count in memo_que:
        #print("w", sh, sw, count)
        for ph in range(-2, 3):
            for pw in range(-2, 3):
                if [ph, pw] not in [[-1, 0], [1, 0], [0, 1], [0, -1], [0, 0]]:
                    if 0 <= sh + ph < h and 0 <= sw + pw < w:
                        if sh + ph == dh - 1 and sw + pw == dw - 1:
                            print(count + 1)
                            exit()
                        if memo[ph + sh][pw + sw]:
                            continue
                        if slist[sh + ph][sw + pw] == ".":
                            que.append([sh + ph, sw + pw, count + 1])
                            memo[ph + sh][pw + sw] = True


print(-1)

### FIXED CODE ###

from collections import deque

h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
slist = [input() for i in range(h)]

if ch == dh and dw == cw:
    print(0)
    exit()

que = deque()
memo_que = deque()
que.append([ch - 1, cw - 1, 0])
memo = [[False for i in range(w)] for j in range(h)]
memo[ch - 1][cw - 1] = True

while len(que) != 0:
    # 幅たん
    while len(que) != 0:
        sh, sw, count = que.popleft()
        memo_que.append([sh, sw, count])
        #print(sh, sw, count)
        for ph, pw in [[-1, 0], [1, 0], [0, 1], [0, -1]]:
            if 0 <= sh + ph < h and 0 <= sw + pw < w:
                if sh + ph == dh - 1 and sw + pw == dw - 1:
                    print(count)
                    exit()
                if memo[ph + sh][pw + sw]:
                    continue
                if slist[sh + ph][sw + pw] == ".":
                    que.append([sh + ph, sw + pw, count])
                    memo[ph + sh][pw + sw] = True
                    memo_que.append([sh + ph, sw + pw, count])

    for sh, sw, count in memo_que:
        #print("w", sh, sw, count)
        for ph in range(-2, 3):
            for pw in range(-2, 3):
                if 0 <= sh + ph < h and 0 <= sw + pw < w:
                    if sh + ph == dh - 1 and sw + pw == dw - 1:
                        print(count + 1)
                        exit()
                    if memo[ph + sh][pw + sw]:
                        continue
                    if slist[sh + ph][sw + pw] == ".":
                        que.append([sh + ph, sw + pw, count + 1])
                        memo[ph + sh][pw + sw] = True
    memo_que.clear()
print(-1)