# -*- coding: utf-8 -*-



from collections import deque



q = deque()



h, w = list(map(int, input().split()))

ch, cw = list(map(int, input().split()))

dh, dw = list(map(int, input().split()))



ch -= 1

cw -= 1

dh -= 1

dw -= 1



# mylist = [[]]

ban_list = [[-1 for j in range(w)] for i in range(h)]

cost_list = [[-1 for j in range(w)] for i in range(h)]



# print(mylist)



for i in range(h):

  # 文字列の入力

  s = eval(input())

  for j in range(len(s)):

    ban_list[i][j] = s[j]



ban_list[ch][cw] = 0

# cost_list[ch][cw] = 0



cost = 0

ph = ch

pw = cw



q.append([ph, pw])





# # 最後デバッグ

# for li in ban_list:

#   print(li)







loop = True

while loop:

  # print(len(q))

  ph, pw = q.popleft()

  cost = ban_list[ph][pw]



  # print(ph, pw)

  # print(cost)

  # 上

  if ph == 0:

    pass

  else:

    if ban_list[ph-1][pw] != "#" and ban_list[ph-1][pw] != cost:

      if ban_list[ph-1][pw] == "." or cost < ban_list[ph-1][pw]:

        ban_list[ph-1][pw] = cost

        # appendだとrandom_02.txtがWAになる

        q.appendleft([ph-1, pw])

    else:

      pass



  # 右

  if pw == w -1:

    pass

  else:

    if ban_list[ph][pw + 1] != "#" and ban_list[ph][pw + 1] != cost:

      if ban_list[ph][pw + 1] == "." or cost < ban_list[ph][pw + 1]:

        ban_list[ph][pw + 1] = cost

        q.appendleft([ph, pw+1])

    else:

      pass



  # 下

  if ph == h -1:

    pass

  else:

    if ban_list[ph+1][pw] != "#" and ban_list[ph+1][pw] != cost:

      if ban_list[ph+1][pw] == "." or cost < ban_list[ph+1][pw]:

        ban_list[ph+1][pw] = cost

        q.appendleft([ph+1, pw])

    else:

      pass



  # 左

  if pw == 0:

    pass

  else:

    if ban_list[ph][pw - 1] != "#" and ban_list[ph][pw - 1] != cost:

      if ban_list[ph][pw - 1] == "." or cost < ban_list[ph][pw - 1]:

        ban_list[ph][pw - 1] = cost

        q.appendleft([ph, pw-1])

    else:

      pass





  # ジャンプ--------------

  for i in range(-2, 3):

    if (ph + i < 0) or (h - 1 < ph + i):

      continue



    for j in range(-2, 3):

      if (pw + j < 0) or (w - 1 < pw + j) or (i==0 and j == 0) or (i==0 and j == -1) or (i==0 and j == 1) or (i==-1 and j == 0) or (i==1 and j == 0):

        continue

      if ban_list[ph + i][pw + j] == ".":

        # print(ph, pw)

        # print(i, j)

        # print(cost)

        ban_list[ph + i][pw + j] = cost + 1

        q.append([ph + i, pw + j])



  if len(q) == 0:

    loop = False





result = ban_list[dh][dw]

if result == ".":

  result = -1



# # 最後デバッグ

# for li in ban_list:

#   print(li)



print(result)

# -*- coding: utf-8 -*-



from collections import deque



q = deque()



h, w = list(map(int, input().split()))

ch, cw = list(map(int, input().split()))

dh, dw = list(map(int, input().split()))



ch -= 1

cw -= 1

dh -= 1

dw -= 1



ban_list = [[-999 for j in range(w)] for i in range(h)]





for i in range(h):

  # 文字列の入力

  s = eval(input())

  for j in range(len(s)):

    ban_list[i][j] = s[j]





ban_list[ch][cw] = 0



cost = 0

ph = ch

pw = cw



q.append([ph, pw])



# 上右下左

ido_h = [-1, 0, 1, 0]

ido_w = [0, 1, 0, -1]



while q:

  ph, pw = q.popleft()

  cost = ban_list[ph][pw]



  for i in range(4):

    nh = ph + ido_h[i]

    nw = pw + ido_w[i]



    if nh < 0 or h -1 < nh or nw < 0 or w -1 < nw:

      continue

    val = ban_list[nh][nw]

    if val == "#" or val == cost:

      continue

    if val == "." or cost < val:

      ban_list[nh][nw] = cost

      q.appendleft([nh, nw])







  # # 上

  # if ph == 0:

  #   pass

  # else:

  #   if ban_list[ph-1][pw] != "#" and ban_list[ph-1][pw] != cost:

  #     if ban_list[ph-1][pw] == "." or cost < ban_list[ph-1][pw]:

  #       ban_list[ph-1][pw] = cost

  #       # appendだとrandom_02.txtがWAになる

  #       q.appendleft([ph-1, pw])

  #   else:

  #     pass



  # # 右

  # if pw == w -1:

  #   pass

  # else:

  #   if ban_list[ph][pw + 1] != "#" and ban_list[ph][pw + 1] != cost:

  #     if ban_list[ph][pw + 1] == "." or cost < ban_list[ph][pw + 1]:

  #       ban_list[ph][pw + 1] = cost

  #       q.appendleft([ph, pw+1])

  #   else:

  #     pass



  # # 下

  # if ph == h -1:

  #   pass

  # else:

  #   if ban_list[ph+1][pw] != "#" and ban_list[ph+1][pw] != cost:

  #     if ban_list[ph+1][pw] == "." or cost < ban_list[ph+1][pw]:

  #       ban_list[ph+1][pw] = cost

  #       q.appendleft([ph+1, pw])

  #   else:

  #     pass



  # # 左

  # if pw == 0:

  #   pass

  # else:

  #   if ban_list[ph][pw - 1] != "#" and ban_list[ph][pw - 1] != cost:

  #     if ban_list[ph][pw - 1] == "." or cost < ban_list[ph][pw - 1]:

  #       ban_list[ph][pw - 1] = cost

  #       q.appendleft([ph, pw-1])

  #   else:

  #     pass





  # ジャンプ--------------

  for i in range(-2, 3):

    if (ph + i < 0) or (h - 1 < ph + i):

      continue



    for j in range(-2, 3):

      if (pw + j < 0) or (w - 1 < pw + j) or (i==0 and j == 0) or (i==0 and j == -1) or (i==0 and j == 1) or (i==-1 and j == 0) or (i==1 and j == 0):

        continue

      if ban_list[ph + i][pw + j] == ".":

        ban_list[ph + i][pw + j] = cost + 1

        q.append([ph + i, pw + j])





result = ban_list[dh][dw]

if result == ".":

  result = -1





print(result)

# -*- coding: utf-8 -*-
from collections import deque

q = deque()
h, w = list(map(int, input().split()))
ch, cw = list(map(int, input().split()))
dh, dw = list(map(int, input().split()))
ch -= 1
cw -= 1
dh -= 1
dw -= 1
# mylist = [[]]
ban_list = [[-1 for j in range(w)] for i in range(h)]
cost_list = [[-1 for j in range(w)] for i in range(h)]
# print(mylist)
for i in range(h):
    # 文字列の入力
    s = eval(input())
    for j in range(len(s)):
        ban_list[i][j] = s[j]
ban_list[ch][cw] = 0
# cost_list[ch][cw] = 0
cost = 0
ph = ch
pw = cw
q.append([ph, pw])
# # 最後デバッグ
# for li in ban_list:
#   print(li)
loop = True
while loop:
    # print(len(q))
    ph, pw = q.popleft()
    cost = ban_list[ph][pw]
    # print(ph, pw)
    # print(cost)
    # 上
    if ph == 0:
        pass
    else:
        if ban_list[ph - 1][pw] != "#" and ban_list[ph - 1][pw] != cost:
            if ban_list[ph - 1][pw] == "." or cost < ban_list[ph - 1][pw]:
                ban_list[ph - 1][pw] = cost
                # appendだとrandom_02.txtがWAになる
                q.appendleft([ph - 1, pw])
        else:
            pass
    # 右
    if pw == w - 1:
        pass
    else:
        if ban_list[ph][pw + 1] != "#" and ban_list[ph][pw + 1] != cost:
            if ban_list[ph][pw + 1] == "." or cost < ban_list[ph][pw + 1]:
                ban_list[ph][pw + 1] = cost
                q.appendleft([ph, pw + 1])
        else:
            pass
    # 下
    if ph == h - 1:
        pass
    else:
        if ban_list[ph + 1][pw] != "#" and ban_list[ph + 1][pw] != cost:
            if ban_list[ph + 1][pw] == "." or cost < ban_list[ph + 1][pw]:
                ban_list[ph + 1][pw] = cost
                q.appendleft([ph + 1, pw])
        else:
            pass
    # 左
    if pw == 0:
        pass
    else:
        if ban_list[ph][pw - 1] != "#" and ban_list[ph][pw - 1] != cost:
            if ban_list[ph][pw - 1] == "." or cost < ban_list[ph][pw - 1]:
                ban_list[ph][pw - 1] = cost
                q.appendleft([ph, pw - 1])
        else:
            pass
    # ジャンプ--------------
    for i in range(-2, 3):
        if (ph + i < 0) or (h - 1 < ph + i):
            continue
        for j in range(-2, 3):
            if (
                (pw + j < 0)
                or (w - 1 < pw + j)
                or (i == 0 and j == 0)
                or (i == 0 and j == -1)
                or (i == 0 and j == 1)
                or (i == -1 and j == 0)
                or (i == 1 and j == 0)
            ):
                continue
            if ban_list[ph + i][pw + j] == ".":
                # print(ph, pw)
                # print(i, j)
                # print(cost)
                ban_list[ph + i][pw + j] = cost + 1
                q.append([ph + i, pw + j])
    if len(q) == 0:
        loop = False
result = ban_list[dh][dw]
if result == ".":
    result = -1
# # 最後デバッグ
# for li in ban_list:
#   print(li)
print(result)

# -*- coding: utf-8 -*-
from collections import deque

q = deque()
h, w = list(map(int, input().split()))
ch, cw = list(map(int, input().split()))
dh, dw = list(map(int, input().split()))
ch -= 1
cw -= 1
dh -= 1
dw -= 1
ban_list = [[-999 for j in range(w)] for i in range(h)]
for i in range(h):
    # 文字列の入力
    s = eval(input())
    for j in range(len(s)):
        ban_list[i][j] = s[j]
ban_list[ch][cw] = 0
cost = 0
ph = ch
pw = cw
q.append([ph, pw])
# 上右下左
ido_h = [-1, 0, 1, 0]
ido_w = [0, 1, 0, -1]
while q:
    ph, pw = q.popleft()
    cost = ban_list[ph][pw]
    for i in range(4):
        nh = ph + ido_h[i]
        nw = pw + ido_w[i]
        if nh < 0 or h - 1 < nh or nw < 0 or w - 1 < nw:
            continue
        val = ban_list[nh][nw]
        if val == "#" or val == cost:
            continue
        if val == "." or cost < val:
            ban_list[nh][nw] = cost
            q.appendleft([nh, nw])
    # # 上
    # if ph == 0:
    #   pass
    # else:
    #   if ban_list[ph-1][pw] != "#" and ban_list[ph-1][pw] != cost:
    #     if ban_list[ph-1][pw] == "." or cost < ban_list[ph-1][pw]:
    #       ban_list[ph-1][pw] = cost
    #       # appendだとrandom_02.txtがWAになる
    #       q.appendleft([ph-1, pw])
    #   else:
    #     pass
    # # 右
    # if pw == w -1:
    #   pass
    # else:
    #   if ban_list[ph][pw + 1] != "#" and ban_list[ph][pw + 1] != cost:
    #     if ban_list[ph][pw + 1] == "." or cost < ban_list[ph][pw + 1]:
    #       ban_list[ph][pw + 1] = cost
    #       q.appendleft([ph, pw+1])
    #   else:
    #     pass
    # # 下
    # if ph == h -1:
    #   pass
    # else:
    #   if ban_list[ph+1][pw] != "#" and ban_list[ph+1][pw] != cost:
    #     if ban_list[ph+1][pw] == "." or cost < ban_list[ph+1][pw]:
    #       ban_list[ph+1][pw] = cost
    #       q.appendleft([ph+1, pw])
    #   else:
    #     pass
    # # 左
    # if pw == 0:
    #   pass
    # else:
    #   if ban_list[ph][pw - 1] != "#" and ban_list[ph][pw - 1] != cost:
    #     if ban_list[ph][pw - 1] == "." or cost < ban_list[ph][pw - 1]:
    #       ban_list[ph][pw - 1] = cost
    #       q.appendleft([ph, pw-1])
    #   else:
    #     pass
    # ジャンプ--------------
    for i in range(-2, 3):
        if (ph + i < 0) or (h - 1 < ph + i):
            continue
        for j in range(-2, 3):
            if (
                (pw + j < 0)
                or (w - 1 < pw + j)
                or (i == 0 and j == 0)
                or (i == 0 and j == -1)
                or (i == 0 and j == 1)
                or (i == -1 and j == 0)
                or (i == 1 and j == 0)
            ):
                continue
            if ban_list[ph + i][pw + j] == ".":
                ban_list[ph + i][pw + j] = cost + 1
                q.append([ph + i, pw + j])
result = ban_list[dh][dw]
if result == ".":
    result = -1
print(result)

-# mylist = [[]] -ban_list = [[-1 for j in range(w)] for i in range(h)] -cost_list = [[-1 for j in range(w)] for i in range(h)] -# print(mylist) +ban_list = [[-999 for j in range(w)] for i in range(h)] -# cost_list[ch][cw] = 0 -# # 最後デバッグ -# for li in ban_list: -#   print(li) -loop = True -while loop: -    # print(len(q)) +# 上右下左 +ido_h = [-1, 0, 1, 0] +ido_w = [0, 1, 0, -1] +while q: -    # print(ph, pw) -    # print(cost) -    # 上 -    if ph == 0: -        pass -    else: -        if ban_list[ph - 1][pw] != "#" and ban_list[ph - 1][pw] != cost: -            if ban_list[ph - 1][pw] == "." or cost < ban_list[ph - 1][pw]: -                ban_list[ph - 1][pw] = cost -                # appendだとrandom_02.txtがWAになる -                q.appendleft([ph - 1, pw]) -        else: -            pass -    # 右 -    if pw == w - 1: -        pass -    else: -        if ban_list[ph][pw + 1] != "#" and ban_list[ph][pw + 1] != cost: -            if ban_list[ph][pw + 1] == "." or cost < ban_list[ph][pw + 1]: -                ban_list[ph][pw + 1] = cost -                q.appendleft([ph, pw + 1]) -        else: -            pass -    # 下 -    if ph == h - 1: -        pass -    else: -        if ban_list[ph + 1][pw] != "#" and ban_list[ph + 1][pw] != cost: -            if ban_list[ph + 1][pw] == "." or cost < ban_list[ph + 1][pw]: -                ban_list[ph + 1][pw] = cost -                q.appendleft([ph + 1, pw]) -        else: -            pass -    # 左 -    if pw == 0: -        pass -    else: -        if ban_list[ph][pw - 1] != "#" and ban_list[ph][pw - 1] != cost: -            if ban_list[ph][pw - 1] == "." or cost < ban_list[ph][pw - 1]: -                ban_list[ph][pw - 1] = cost -                q.appendleft([ph, pw - 1]) -        else: -            pass +    for i in range(4): +        nh = ph + ido_h[i] +        nw = pw + ido_w[i] +        if nh < 0 or h - 1 < nh or nw < 0 or w - 1 < nw: +            continue +        val = ban_list[nh][nw] +        if val == "#" or val == cost: +            continue +        if val == "." or cost < val: +            ban_list[nh][nw] = cost +            q.appendleft([nh, nw]) +    # # 上 +    # if ph == 0: +    #   pass +    # else: +    #   if ban_list[ph-1][pw] != "#" and ban_list[ph-1][pw] != cost: +    #     if ban_list[ph-1][pw] == "." or cost < ban_list[ph-1][pw]: +    #       ban_list[ph-1][pw] = cost +    #       # appendだとrandom_02.txtがWAになる +    #       q.appendleft([ph-1, pw]) +    #   else: +    #     pass +    # # 右 +    # if pw == w -1: +    #   pass +    # else: +    #   if ban_list[ph][pw + 1] != "#" and ban_list[ph][pw + 1] != cost: +    #     if ban_list[ph][pw + 1] == "." or cost < ban_list[ph][pw + 1]: +    #       ban_list[ph][pw + 1] = cost +    #       q.appendleft([ph, pw+1]) +    #   else: +    #     pass +    # # 下 +    # if ph == h -1: +    #   pass +    # else: +    #   if ban_list[ph+1][pw] != "#" and ban_list[ph+1][pw] != cost: +    #     if ban_list[ph+1][pw] == "." or cost < ban_list[ph+1][pw]: +    #       ban_list[ph+1][pw] = cost +    #       q.appendleft([ph+1, pw]) +    #   else: +    #     pass +    # # 左 +    # if pw == 0: +    #   pass +    # else: +    #   if ban_list[ph][pw - 1] != "#" and ban_list[ph][pw - 1] != cost: +    #     if ban_list[ph][pw - 1] == "." or cost < ban_list[ph][pw - 1]: +    #       ban_list[ph][pw - 1] = cost +    #       q.appendleft([ph, pw-1]) +    #   else: +    #     pass -                # print(ph, pw) -                # print(i, j) -                # print(cost) -    if len(q) == 0: -        loop = False -# # 最後デバッグ -# for li in ban_list: -#   print(li)