H, W = gets.chomp.split.map(&:to_i)
Ch, Cw = gets.chomp.split.map(&:to_i)
Dh, Dw = gets.chomp.split.map(&:to_i)
S = []
H.times{ |i|
  S.push gets.chomp.each_char.to_a
}

$id_max = -1
$area_by_pos = {}
$area_by_id = {}

def area_search i, j, id=nil
  return if S[i][j] == "#"
  return if $area_by_pos[[i, j]]
  unless id
    $id_max += 1
    id = $id_max
  end
  $area_by_pos[[i, j]] = id
  $area_by_id[id] ||= []
  $area_by_id[id].push([i,j])
  if i > 1
    area_search i-1, j, id
  end
  if i < H - 1
    area_search i+1, j, id
  end
  if j > 1
    area_search i, j-1, id
  end
  if j < W - 1
    area_search i, j+1, id
  end
end

H.times{ |i|
  W.times{ |j|
    area_search i, j
  }
}

#p $area_by_pos
#p $area_by_id
#p ""

$area_to_area = []

def warpable from_area_id, to_area_id
  from_pos_list = $area_by_id[from_area_id]
  to_pos_list   = $area_by_id[to_area_id]
  from_pos_list.each{ |from_pos|
    to_pos_list.each{ |to_pos|
      if (from_pos[0] - to_pos[0]).abs <= 2 && (from_pos[1] - to_pos[1]).abs <= 2
        #p from_pos, to_pos
        #p ""
        return true
      end
    }
  }
  return false
end

$area_by_id.each_key{ |from_area_id|
  $area_by_id.each_key{ |to_area_id|
    next if from_area_id == to_area_id
    if warpable from_area_id, to_area_id
      $area_to_area.push([from_area_id, to_area_id])
    end
  }
}

#p $area_to_area
#p ""

start_area = $area_by_pos[[Ch-1,Cw-1]]
goal_area = $area_by_pos[[Dh-1,Dw-1]]

#p start_area, goal_area

def min_warp current_area, goal_area, start_area=nil
  if current_area == goal_area
    return 0
  end
  if current_area == start_area
    return -1
  end
  current_area_to_goal_area = []
  $area_to_area.select{|e| e[0] == current_area}.each{ |e|
    if e[1] == goal_area
      return 1
    else
      start_area ||= current_area
      current_area_to_goal_area.push (min_warp e[1], goal_area, start_area)
    end
  }
  if current_area_to_goal_area.all?{ |e| e == -1}
    return -1
  else
    return (current_area_to_goal_area.select{ |e| e > -1 }.min + 1)
  end
end

puts min_warp start_area, goal_area
