h, w = gets.chomp.split(' ').map { |e| e.to_i + 4 }
c = gets.chomp.split(' ').map { |e| e.to_i + 1 }
d = gets.chomp.split(' ').map { |e| e.to_i + 1 }
s = h.times.map { |i| 2 <= i && i < h - 2 ? "###{gets.chomp}##" : '#' * w }

co = h.times.map { w.times.map { -1 } }
v = h.times.map { |i| w.times.map { |j| s[i][j] == '#' } }

qa = [c]
v[c[0]][c[1]] = true

b = 0
while !(qa.empty?)
  qb = []

  while !(qa.empty?)
    y, x = qa.shift

    co[y][x] = b
    qb.push([y, x])

    [[-1, 0], [0, 1], [1, 0], [0, -1]].each do |dy, dx|
      next if v[y + dy][x + dx]
      qa.push([y + dy, x + dx])
      qb.push([y + dy, x + dx])
      v[y + dy][x + dx] = true
    end
  end

  qb.each do |y, x|
    (-2..2).to_a.product((-2..2).to_a).each do |dy, dx|
      next if v[y + dy][x + dx]
      qa.push([y + dy, x + dx])
      v[y + dy][x + dx] = true
    end
  end

  b += 1
end

puts co[d[0]][d[1]]
