INF = 10_000_000
H, W = gets.split.map(&:to_i)
sy, sx = gets.split.map(&:to_i)
ty, tx = gets.split.map(&:to_i)
MAP = H.times.map{ gets.chomp }

S = W * (sy - 1) + sx - 1
T = W * (ty - 1) + tx - 1

inq = Array.new(H * W)
dist = Array.new(H * W, INF)

q = [S]
inq[S] = true
dist[S] = 0

until q.empty?
  u = q.shift
  d = dist[u]
  inq[u] = false
  
  y,x = u.divmod(W)
  y_range = (y - 2 .. y + 2).to_a
  x_range = (x - 2 .. x + 2).to_a
  
  y_range.product(x_range) do |y_, x_|
    next if y_ < 0 || x_ < 0 || y_ >= H || x_ >= W
    v = W * y_ + x_

    d_ = (y - y_).abs + (x - x_).abs > 1 ? d + 1 : d
    next if MAP[y_][x_] == '#' || dist[v] <= d_

    dist[v] = d_
    if !inq[v]
      if d_ > d
        q.push(v)
      else
        q.unshift(v)
      end
      inq[v] = true
    end
  end
end

if dist[T] < INF
  puts dist[T]
else
  puts -1
end