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,-1)}
queue = [[c[0]-1,c[1]-1]]
queue2 = []
cnt = 0
while !queue.empty? || !queue2.empty?
  if queue.empty?
    queue,queue2 = queue2,queue
    cnt += 1
  end
  v = queue[0]
  queue.shift
  for i in 0..24
    v0 = v[0]+(i/5)-2
    v1 = v[1]+(i%5)-2
    if (((i/5)-2).abs == 2 || ((i%5)-2).abs == 2 || (((i/5)-2).abs == 1 && ((i%5)-2).abs == 1)) && (0 <= v0 && v0 < h && 0 <= v1 && v1 < w)
      if visited[v0][v1] == -1 && s[v0][v1] == "."
        visited[v0][v1] = cnt+1
        queue2 << [v0,v1]
      end
    elsif ((i/5)-2 != 0 || (i%5)-2 != 0) && (0 <= v0 && v0 < h && 0 <= v1 && v1 < w)
      if (visited[v0][v1] == -1 || visited[v0][v1] > cnt) && s[v0][v1] == "."
        visited[v0][v1] = cnt
        queue << [v0,v1]
      end
    end
  end
end
puts visited[d[0]-1][d[1]-1]