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 - 2 .. y + 2).each do |ny|
    (x - 2 .. x + 2).each do |nx|
      next if ny < 0 || nx < 0 || ny >= H || nx >= W
      v = W * ny + nx
      md = (y - ny).abs + (x - nx).abs
      
      if md <= 1
        if MAP[ny][nx] == '.' && dist[v] > d
          dist[v] = d
          if !inq[v]
            inq[v] = true
            q.unshift(v)
          end
        end
        next
      end
      
      if MAP[ny][nx] == '.' && dist[v] > d + 1
        dist[v] = d + 1
        if !inq[v]
          inq[v] = true
          q << v
        end
      end
    end
  end
end

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