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) }
reached[SH-1][SW-1] = 0

count = 0

while true
  que = []

  1.upto(H) do |h|
    1.upto(W) do |w|
      que.push([h,w]) if reached[h-1][w-1] == count
    end
  end

  if que.empty?
    break
  end

  while !que.empty?
    h,w=que.shift
    next if s[h-1][w-1] == '#'
    if h == GH && w == GW
      p count
      exit
    end
    reached[h-1][w-1] = count

    [[1,0],[0,-1],[-1,0],[0,1]].each do |nh,nw|
      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] &.<= count

      que.push([next_h,next_w])
    end
  end

  1.upto(H) do |h|
    1.upto(W) do |w|
      que.push([h,w]) if reached[h-1][w-1] == count
    end
  end

  while !que.empty?
    h,w=que.shift

    [-2,-1,0,1,2].each do |nh|
      [-2,-1,0,1,2].each do |nw|
        next if nh == 0 && nw == 0
        next if [[1,0],[0,-1],[-1,0],[0,1]].include?([nh,nw])

        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] &.<= count

        reached[h-1][w-1] = count + 1
      end
    end
  end

  count += 1
end

p -1
