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 -2..2
    for j in -2..2
      if (i == -2 || i == 2 || j == -2 || j == 2 || (i.abs == 1 && j.abs == 1)) && (0 <= v[0]+i && v[0]+i < h && 0 <= v[1]+j && v[1]+j < w)
        if visited[v[0]+i][v[1]+j] == -1 && s[v[0]+i][v[1]+j] == "."
          visited[v[0]+i][v[1]+j] = cnt+1
          queue2 << [v[0]+i,v[1]+j]
        end
      elsif (i != 0 || j != 0) && (0 <= v[0]+i && v[0]+i < h && 0 <= v[1]+j && v[1]+j < w)
        if (visited[v[0]+i][v[1]+j] == -1 || visited[v[0]+i][v[1]+j] > cnt) && s[v[0]+i][v[1]+j] == "."
          visited[v[0]+i][v[1]+j] = cnt
          queue << [v[0]+i,v[1]+j]
        end
      end
    end
  end
end
puts visited[d[0]-1][d[1]-1]