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)]
count_arr = []
maze.length.times do
  count_arr << Array.new(w+2,1000)
end

# 座標
bfs = [[ch, cw]]
count_arr[ch][cw] = 0

while bfs.length > 0 do
  bfs2 = []
  while bfs.length > 0 do
    y, x = bfs.shift
    count = count_arr[y][x]
    # 歩き
    [[-1, 0], [1, 0], [0, 1], [0, -1]].each do |(dy, dx)|
      tmp_y = y + dy
      tmp_x = x + dx
      next if count_arr[tmp_y][tmp_x] <= count
      if maze[tmp_y][tmp_x] == "."
        bfs << [tmp_y, tmp_x]
      else
        bfs2 << [y, x]
      end
      count_arr[tmp_y][tmp_x] = count
    end
  end
  while bfs2.length > 0 do
    y, x = bfs2.shift
    count = count_arr[y][x]
    # 魔法
    [
      [-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] == "#" || count_arr[tmp_y][tmp_x] <= count + 1
      count_arr[tmp_y][tmp_x] = count + 1
      bfs << [tmp_y, tmp_x]
    end
  end
end
result = count_arr[dh][dw]
if result == 1000
  puts -1
else
  puts result 
end
