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

map = []
H.times do |i|
  map.push(gets.chomp.chars)
end

H.times do |i|
  map[i] = ["#", "#"] + map[i] + ["#", "#"]
end

map = [["#"] * (W + 2), ["#"] * (W + 2)] + map + [["#"] * (W + 2), ["#"] * (W + 2)]

move = [[-1, 0], [1, 0], [0, -1], [0, 1]]
warp = [
  [-2, -2], [-2, -1], [-2, 0], [-2, 1], [-2, 2],
  [-1, -2], [-1, -1], [-1, 1], [-1, 2],
  [0, -2], [0, 2],
  [1, -2], [1, -1], [1, 1], [1, 2],
  [2, -2], [2, -1], [2, 0], [2, 1], [2, 2],
]

ch += 1
cw += 1
dh += 1
dw += 1

map[ch][cw] = 0
go = [[ch, cw]]

loop do
  (H * W).times do |i|
    break if go[i].nil?
    now_h, now_w = go[i]
    move.each do |h, w|
      if map[now_h + h][now_w + w] == "."
        go.push([now_h + h, now_w + w])
        map[now_h + h][now_w + w] = map[now_h][now_w]
      end
    end
  end

  warp_xy = []
  go.each do |i, j|
    warp.each do |wh, ww|
      if map[i + wh][j + ww] == "."
        map[i + wh][j + ww] = map[i][j] + 1
        warp_xy.push([i + wh, j + ww])
      end
    end
  end
  go = warp_xy

  return p map[dh][dw] if map[dh][dw] != "."
  return p (-1) if warp_xy.empty?
end
