H,W = gets.split.map(&:to_i)
SY,SX = gets.split.map { _1.to_i - 1 }
GY,GX = gets.split.map { _1.to_i - 1 }
S = readlines.map { _1.chomp.chars }

D = [[1,0],[-1,0],[0,1],[0,-1]]

dist = Array.new(H) { [-1]*W }
que = []
que.prepend([SY,SX])
dist[SY][SX] = 0
while !que.empty?
  
  y,x = que.shift

  D.each do |dy,dx|
    yy,xx = y+dy,x+dx
    next if yy < 0 || xx < 0
    next if S.dig(yy,xx) != "."
    next if 0 <= dist[yy][xx] && dist[yy][xx] <= dist[y][x]
    dist[yy][xx] = dist[y][x]
    que.prepend([yy,xx])
  end
  
  (-2..2).each do |dy|
    (-2..2).each do |dx|
      yy,xx = y+dy,x+dx
      next if yy < 0 || xx < 0
      next if S.dig(yy,xx) != "."
      next if 0 <= dist[yy][xx] && dist[yy][xx] <= dist[y][x]
      dist[yy][xx] = dist[y][x] + 1
      que.push([yy,xx])
    end
  end
  
end

puts dist[GY][GX]
