H,W = gets.chomp.split.map(&:to_i)

SH,SW = gets.chomp.split.map(&:to_i)
GH,GW = gets.chomp.split.map(&:to_i)

s = []
H.times do |h|
  s[h] = gets.chomp.split('')
end

reached = Array.new(H) { Array.new(W, nil) }

que = [[SH,SW,0]]
while !que.empty?
  h,w,num=que.shift
  next if s[h-1][w-1] == '#'
  next if reached[h-1][w-1] &.<= num
  reached[h-1][w-1] = num
  if h == GH && w == GW
    p num
    exit
  end
  [-2,-1,0,1,2].each do |nh|
    [-2,-1,0,1,2].each do |nw|
      next if nh == 0 && nw == 0
      next_h = h+nh
      next_w = w+nw
      next if next_h < 1 || next_h > H
      next if next_w < 1 || next_w > W
      next if s[next_h-1][next_w-1] == '#'
      next if reached[next_h-1][next_w-1] &.<= num
      if nh.abs < 2 && nw.abs < 2
        que.push([next_h,next_w,num])
      else
        que.push([next_h,next_w,num+1])
      end
    end
  end
end

p -1
