h, w = gets.split.map(&:to_i)
ch, cw = gets.split.map(&:to_i)
max = h * w
ch += 1
cw += 1
dh, dw = gets.split.map(&:to_i)
dh += 1
dw += 1
s = Array.new(h+4) { Array.new(w+4, nil) }
h.times do |i|
  s[i+2] = [nil, nil, *gets.chop.chars.map{_1 == '#' ? nil : max}, nil, nil]
end

q = [[ch, cw, 0]]
seek = []
s[ch][cw] = 0

loop do
  # 同コスト内のノード幅優先探索
  break if q.empty?
  until q.empty?
    nh, nw, cc = q.shift

    if nh == dh && nw == dw
      puts cc
      exit 0
    end

    ts = s[nh-1][nw]
    if ts && ts > cc
      q.push [nh-1, nw, cc]
      s[nh-1][nw] = cc
    end
    ts = s[nh+1][nw]
    if ts && ts > cc
      q.push [nh+1, nw, cc]
      s[nh+1][nw] = cc
    end
    ts = s[nh][nw-1]
    if ts && ts > cc
      q.push [nh, nw-1, cc]
      s[nh][nw-1] = cc
    end
    ts = s[nh][nw+1]
    if ts && ts > cc
      q.push [nh, nw+1, cc]
      s[nh][nw+1] = cc
    end

    seek.push [nh, nw, cc+1]
  end

  # 次のコストのノードの発見とキューイング
  seek.each do |eh, ew, ec|
    (eh-2..eh+2).each do |th|
      (ew-2..ew+2).each do |tw|
        es = s[th][tw]
        next unless es
        next if es < ec

        if th == dh && tw == dw
          puts ec
          exit 0
        end

        q.push [th, tw, ec]
        s[th][tw] = ec
      end
    end
  end
end
puts -1
