h,w = gets.chomp.split(" ").map(&:to_i)
ch,cw = gets.chomp.split(" ").map(&:to_i)
dh,dw = gets.chomp.split(" ").map(&:to_i)

maze = ["#" * (w + 2)] + h.times.map{"#" + gets.chomp + "#"} + ["#" * (w + 2)]

sy = ch
sx = cw
gy = dh
gx = dw
result = -1
# 座標, 回数
bfs = [[sy, sx, 0]]
bfs2 = [[sy, sx, 0]]
maze[sy][sx] = "#"

while true do
  while bfs.length > 0 do
    y, x, num = bfs.shift
    if y == gy && x == gx
      result = num
      break
    end
    # 歩き
    [[-1, 0], [1, 0], [0, 1], [0, -1]].each do |(dy, dx)|
      tmp_y = y + dy
      tmp_x = x + dx
      next if maze[tmp_y][tmp_x] == "#"
      maze[tmp_y][tmp_x] = "#"
      bfs << [tmp_y, tmp_x, num]
      bfs2 << [tmp_y, tmp_x, num]
    end
  end
  flag = false
  while bfs2.length > 0 do
    y, x, num = bfs2.shift
    if y == gy && x == gx
      result = num
      break
    end
    # 魔法
    [
      [-2, -2], [-2, -1], [-2, 0], [-2, 1], [-2, 2],
      [-1, -2], [-1, 2], [0, -2], [0, 2], [1, -2], [1, 2],
      [2, -2], [2, -1], [2, 0], [2, 1], [2, 2]
    ].each do |(dy, dx)|
      tmp_y = y + dy
      tmp_x = x + dx
      next if maze[tmp_y].nil? || maze[tmp_y][tmp_x].nil? || maze[tmp_y][tmp_x] == "#"
      maze[tmp_y][tmp_x] = "#"
      bfs << [tmp_y, tmp_x, num+1]
      flag = true
      break
    end
    break if flag
  end
  break if bfs.length == 0 && bfs2.length == 0
end
puts result 
