# frozen_string_literal: true

h, w = gets.split.map(&:to_i)
ch, cw = gets.split.map(&:to_i)
dh, dw = gets.split.map(&:to_i)
s = h.times.map { gets.chomp.chars }

INF = 10**9
d = Array.new(h) { Array.new(w) { INF } }
q = []

q.push([ch - 1, cw - 1])
d[ch - 1][cw - 1] = 0

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

p = q.shift
loop do
  xx, yy = p
  count = d[xx][yy]
  0.upto(3) do |i|
    x = xx + dx[i]
    y = yy + dy[i]
    next if x.negative? || x > h - 1 || y.negative? || w - 1 < y || d[x][y] <= count || s[x][y] != '.'

    q.push([x, y])
    d[x][y] = [count, d[x][y]].min
  end
  (-2).upto(2) do |i|
    (-2).upto(2) do |j|
      x = xx + i
      y = yy + j
      next if x.negative? || x > h - 1 || y.negative? || w - 1 < y || d[x][y] <= count || s[x][y] != '.'

      q.push([x, y])
      d[x][y] = [count + 1, d[x][y]].min
    end
  end
  break if q.empty?

  p = q.shift
end
ans = d[dh - 1][dw - 1]
puts ans == INF ? '-1' : ans
