h,w = gets.chomp.split(" ").map(&:to_i)
c = gets.chomp.split(" ").map(&:to_i)
d = gets.chomp.split(" ").map(&:to_i)
s = Array.new(h){Array.new}
h.times do |i|
  s[i] = gets.chomp.split("")
end
visited = Array.new(h){Array.new(w,-2)}
visited[c[0]-1][c[1]-1] = 0
queue = [[c[0]-1,c[1]-1]]
queue2 = []
cnt = 0
map1 = [[-1,0],[1,0],[0,1],[0,-1]]
map2 = [[-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]]

while !queue.empty? || !queue2.empty?
  if queue.empty?
    queue,queue2 = queue2,queue
    cnt += 1
  end
  v = queue[0]
  queue.shift
  if visited[v[0]][v[1]] == cnt-1
    next
  end
  map1.each do |x|
    v0 = v[0]+x[0]
    v1 = v[1]+x[1]
    if 0 <= v0 && v0 < h && 0 <= v1 && v1 < w
      if (visited[v0][v1] == -2 || visited[v0][v1] > cnt) && s[v0][v1] == "."
        visited[v0][v1] = cnt
        queue << [v0,v1]
      end
    end
  end
  map2.each do |x|
    v0 = v[0]+x[0]
    v1 = v[1]+x[1]
    if 0 <= v0 && v0 < h && 0 <= v1 && v1 < w
      if visited[v0][v1] == -2 && s[v0][v1] == "."
        visited[v0][v1] = cnt+1
        queue2 << [v0,v1]
      end
    end
  end
end
if visited[d[0]-1][d[1]-1] == -2
  puts -1
else
  puts visited[d[0]-1][d[1]-1]
end