h, w = gets.chomp.split.map(&:to_i)
ch, cw = gets.chomp.split.map {|x| x.to_i - 1 }
dh, dw = gets.chomp.split.map {|x| x.to_i - 1 }
s = Array.new(h) { gets.chomp }

q = [[ch, cw]]
dp = Array.new(h) { Array.new(w, h * w) }
dp[ch][cw] = 0

d = [
  [-1, 0], [1, 0], [0, -1], [0, 1],
  [-2, 0], [-1, 1], [0, 2], [1, 1], [2, 0], [1, -1], [0, -2], [-1, -1],
  [-2, -1], [-1, -2], [-2, 1], [-1, 2], [2, 1], [1, 2], [2, -1], [1, -2],
  [-2, -2], [-2, 2], [2, -2], [2, 2]
]

while !q.empty?
  x, y = q.shift
  break if x == dh && y == dw

  d.each do |dx, dy|
    nx = x + dx
    ny = y + dy
    next if nx < 0 || nx >= h
    next if ny < 0 || ny >= w
    next if s[nx][ny] == "#"
    next if dp[nx][ny] <= dp[x][y]

    if dx.abs + dy.abs <= 1
      dp[nx][ny] = dp[x][y]
      q.unshift([nx, ny])
    elsif dp[nx][ny] > dp[x][y] + 1
      dp[nx][ny] = dp[x][y] + 1
      q.push([nx, ny])
    end
  end
end
puts (dp[dh][dw] == h * w)? "-1" : dp[dh][dw]