H,W = gets.split.map(&:to_i)
SY,SX = gets.split.map { _1.to_i - 1 }
GY,GX = gets.split.map { _1.to_i - 1 }
S = readlines.map { _1.chomp.chars }

B = "#"
R = "."

MV = [[1,0],[-1,0],[0,1],[0,-1]]
WP = [*-2..2].product([*-2..2]) - [*MV, [0,0]]

dist = Array.new(H) { [-1]*W }
que = [SY,SX]
dist[SY][SX] = 0
mem = Array.new(H) { []*W }
until que.empty?
  break if dist[GY][GX] != -1

  que2 = []
  until que.empty?
    y,x = que.pop(2)
    d = dist[y][x]

    blk = false
    MV.each do |dy,dx|
      yy,xx = y+dy,x+dx
      if 0 <= yy && yy < H && 0 <= xx && xx < W
        if S[yy][xx] == R && (dist[yy][xx] < 0 || dist[yy][xx] > d)
          dist[yy][xx] = d
          mem[yy][xx] = true
          que.push(yy,xx)
        else
          blk = S[yy][xx] == B
        end
      end
    end
    que2.push(y,x) if blk
  end
  
  que2.each_slice(2) do |y,x|
    WP.each do |dy,dx|
      yy,xx = y+dy,x+dx
      if 0 <= yy && yy < H && 0 <= xx && xx < W && !mem[yy][xx] && S[yy][xx] == R && (dist[yy][xx] < 0 || dist[yy][xx] > d + 1)
        dist[yy][xx] = d + 1
        que.push(yy,xx)
      end
    end
  end

end

puts dist[GY][GX]