H, W = list(map(int, input().split()))

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

gh, gw =  list(map(int, input().split()))



f = [[s=="." for s in eval(input())] for h in range(H)]

ans = [[-1 for w in range(W)]for h in range(H)]



nextQ = [(ch-1)*1000 + cw-1]

ans[ch-1][cw-1] = 0

while len(nextQ)>0:

  Q = list(nextQ)

  nextQ = set()

  visited = set()

  while len(Q):

    nh = Q.pop(0)

    nw = nh%1000

    nh = nh//1000

    for dh in range(0-min(nh,2), 3):

      if nh+dh>=H:

        break

      for dw in range(0-min(nw,2), 3):

        if nw + dw>=W:

          break

        if dh==0 and dw==0:

          continue

        if dh*dw==0 and (abs(dh)==1 or abs(dw)==1):

          if ans[nh+dh][nw+dw]==-1:

            if f[nh+dh][nw+dw]:

              Q.append((nh+dh)*1000 + nw+dw)

              visited.add((nh+dh)*1000 + nw+dw)

              ans[nh+dh][nw+dw]=ans[nh][nw]

            else:

              ans[nh+dh][nw+dw]=-2

          elif ans[nh+dh][nw+dw]>ans[nh][nw]:

            Q.append((nh+dh)*1000 + nw+dw)

            visited.add((nh+dh)*1000 + nw+dw)

            ans[nh+dh][nw+dw]=ans[nh][nw]

        else:

          if ans[nh+dh][nw+dw]==-1:

            if f[nh+dh][nw+dw]:

              nextQ.add((nh+dh)*1000 + nw+dw)

              ans[nh+dh][nw+dw]=ans[nh][nw]+1

            else:

              ans[nh+dh][nw+dw]=-2

          elif ans[nh+dh][nw+dw]>ans[nh][nw]:

            nextQ.add((nh+dh)*1000 + nw+dw)

            ans[nh+dh][nw+dw]=ans[nh][nw]+1

  nextQ -= visited

print((ans[gh-1][gw-1]))

      

H, W = list(map(int, input().split()))

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

gh, gw =  list(map(int, input().split()))



f = [[s=="." for s in eval(input())] for h in range(H)]

ans = [[-1 for w in range(W)]for h in range(H)]



nextQ = [(ch-1)*1000 + cw-1]

ans[ch-1][cw-1] = 0

while len(nextQ)>0:

  Q = list(nextQ)

  nextQ = set()

  visited = set()

  while len(Q):

    nh = Q.pop(0)

    nw = nh%1000

    nh = nh//1000

    for dh in range(0-min(nh,2), 3):

      if nh+dh>=H:

        break

      for dw in range(0-min(nw,2), 3):

        if nw + dw>=W:

          break

        if dh==0 and dw==0:

          continue

        if dh*dw==0 and (abs(dh)==1 or abs(dw)==1):

          if ans[nh+dh][nw+dw]==-1:

            if f[nh+dh][nw+dw]:

              Q.append((nh+dh)*1000 + nw+dw)

              visited.add((nh+dh)*1000 + nw+dw)

              ans[nh+dh][nw+dw]=ans[nh][nw]

            else:

              ans[nh+dh][nw+dw]=-2

          elif ans[nh+dh][nw+dw]>ans[nh][nw]:

            Q.append((nh+dh)*1000 + nw+dw)

            nextQ -= {(nh+dh)*1000 + nw+dw}

            ans[nh+dh][nw+dw]=ans[nh][nw]

        else:

          if ans[nh+dh][nw+dw]==-1:

            if f[nh+dh][nw+dw]:

              nextQ.add((nh+dh)*1000 + nw+dw)

              ans[nh+dh][nw+dw]=ans[nh][nw]+1

            else:

              ans[nh+dh][nw+dw]=-2

print((ans[gh-1][gw-1]))

      

H, W = list(map(int, input().split()))
ch, cw = list(map(int, input().split()))
gh, gw = list(map(int, input().split()))
f = [[s == "." for s in eval(input())] for h in range(H)]
ans = [[-1 for w in range(W)] for h in range(H)]
nextQ = [(ch - 1) * 1000 + cw - 1]
ans[ch - 1][cw - 1] = 0
while len(nextQ) > 0:
    Q = list(nextQ)
    nextQ = set()
    visited = set()
    while len(Q):
        nh = Q.pop(0)
        nw = nh % 1000
        nh = nh // 1000
        for dh in range(0 - min(nh, 2), 3):
            if nh + dh >= H:
                break
            for dw in range(0 - min(nw, 2), 3):
                if nw + dw >= W:
                    break
                if dh == 0 and dw == 0:
                    continue
                if dh * dw == 0 and (abs(dh) == 1 or abs(dw) == 1):
                    if ans[nh + dh][nw + dw] == -1:
                        if f[nh + dh][nw + dw]:
                            Q.append((nh + dh) * 1000 + nw + dw)
                            visited.add((nh + dh) * 1000 + nw + dw)
                            ans[nh + dh][nw + dw] = ans[nh][nw]
                        else:
                            ans[nh + dh][nw + dw] = -2
                    elif ans[nh + dh][nw + dw] > ans[nh][nw]:
                        Q.append((nh + dh) * 1000 + nw + dw)
                        visited.add((nh + dh) * 1000 + nw + dw)
                        ans[nh + dh][nw + dw] = ans[nh][nw]
                else:
                    if ans[nh + dh][nw + dw] == -1:
                        if f[nh + dh][nw + dw]:
                            nextQ.add((nh + dh) * 1000 + nw + dw)
                            ans[nh + dh][nw + dw] = ans[nh][nw] + 1
                        else:
                            ans[nh + dh][nw + dw] = -2
                    elif ans[nh + dh][nw + dw] > ans[nh][nw]:
                        nextQ.add((nh + dh) * 1000 + nw + dw)
                        ans[nh + dh][nw + dw] = ans[nh][nw] + 1
    nextQ -= visited
print((ans[gh - 1][gw - 1]))

H, W = list(map(int, input().split()))
ch, cw = list(map(int, input().split()))
gh, gw = list(map(int, input().split()))
f = [[s == "." for s in eval(input())] for h in range(H)]
ans = [[-1 for w in range(W)] for h in range(H)]
nextQ = [(ch - 1) * 1000 + cw - 1]
ans[ch - 1][cw - 1] = 0
while len(nextQ) > 0:
    Q = list(nextQ)
    nextQ = set()
    visited = set()
    while len(Q):
        nh = Q.pop(0)
        nw = nh % 1000
        nh = nh // 1000
        for dh in range(0 - min(nh, 2), 3):
            if nh + dh >= H:
                break
            for dw in range(0 - min(nw, 2), 3):
                if nw + dw >= W:
                    break
                if dh == 0 and dw == 0:
                    continue
                if dh * dw == 0 and (abs(dh) == 1 or abs(dw) == 1):
                    if ans[nh + dh][nw + dw] == -1:
                        if f[nh + dh][nw + dw]:
                            Q.append((nh + dh) * 1000 + nw + dw)
                            visited.add((nh + dh) * 1000 + nw + dw)
                            ans[nh + dh][nw + dw] = ans[nh][nw]
                        else:
                            ans[nh + dh][nw + dw] = -2
                    elif ans[nh + dh][nw + dw] > ans[nh][nw]:
                        Q.append((nh + dh) * 1000 + nw + dw)
                        nextQ -= {(nh + dh) * 1000 + nw + dw}
                        ans[nh + dh][nw + dw] = ans[nh][nw]
                else:
                    if ans[nh + dh][nw + dw] == -1:
                        if f[nh + dh][nw + dw]:
                            nextQ.add((nh + dh) * 1000 + nw + dw)
                            ans[nh + dh][nw + dw] = ans[nh][nw] + 1
                        else:
                            ans[nh + dh][nw + dw] = -2
print((ans[gh - 1][gw - 1]))

-                        visited.add((nh + dh) * 1000 + nw + dw) +                        nextQ -= {(nh + dh) * 1000 + nw + dw} -                    elif ans[nh + dh][nw + dw] > ans[nh][nw]: -                        nextQ.add((nh + dh) * 1000 + nw + dw) -                        ans[nh + dh][nw + dw] = ans[nh][nw] + 1 -    nextQ -= visited