require 'set'
h, w = gets.split.map(&:to_i)
ch, cw = gets.split.map(&:to_i)
ch -= 1
cw -= 1
dh, dw = gets.split.map(&:to_i)
dh -= 1
dw -= 1
s = Array.new(h)
h.times do |i|
  s[i] = gets.chop.chars
end

q = [[ch, cw]]
cost = [[[ch, cw]]]
current_cost = 0
checked = Set.new
checked << [ch, cw]
loop do
  # 同コスト内のノード幅優先探索
  break if q.empty?
  until q.empty?
    nh, nw = q.shift

    if [nh, nw] == [dh, dw]
      puts current_cost
      exit 0
    end

    if nh != 0 && s[nh-1][nw] == '.' && !checked.include?([nh-1, nw])
      cost[current_cost] << [nh-1, nw]
      checked << [nh-1, nw]
      q << [nh-1, nw]
    end
    if nh != h-1 && s[nh+1][nw] == '.' && !checked.include?([nh+1, nw])
      cost[current_cost] << [nh+1, nw]
      checked << [nh+1, nw]
      q << [nh+1, nw]
    end
    if nw != 0 && s[nh][nw-1] == '.' && !checked.include?([nh, nw-1])
      cost[current_cost] << [nh, nw-1]
      checked << [nh, nw-1]
      q << [nh, nw-1]
    end
    if nh != w-1 && s[nh][nw+1] == '.' && !checked.include?([nh, nw+1])
      cost[current_cost] << [nh, nw+1]
      checked << [nh, nw+1]
      q << [nh, nw+1]
    end
  end
  cost[current_cost+1] = []

  # 次のコストのノードの発見とキューイング
  cost[current_cost].each do |eh, ew|
    min_eh = eh - 2 < 0 ? 0 : eh - 2
    max_eh = eh + 2 > h - 1 ? h - 1 : eh + 2
    min_ew = ew - 2 < 0 ? 0 : ew - 2
    max_ew = ew + 2 > w - 1 ? w - 1 : ew + 2
    (min_eh..max_eh).each do |th|
      (min_ew..max_ew).each do |tw|
        next if s[th][tw] != '.' || checked.include?([th, tw])

        if [th, tw] == [dh, dw]
          puts current_cost + 1
          exit 0
        end

        cost[current_cost+1] << [th, tw]
        checked << [th, tw]
        q << [th, tw]
      end
    end
  end
  current_cost += 1
end
puts -1