h, w = gets.split.map(&:to_i)
ch, cw = gets.split.map(&:to_i)
dh,dw = gets.split.map(&:to_i)
s = h.times.map {gets.chomp + "##"} + ["#" * (w+4), "#" * (w+4)]

move = [[1,0], [-1,0], [0,1], [0,-1]]
warp = (-2..2).flat_map do |i|
    (-2..2).map {|j| i.abs + j.abs > 1 ? [i, j] : nil}
end.compact

d = Array.new(h+2) {Array.new(w+2)}
que = [[ch-1, cw-1, 0]]

while (x,y,z = que.shift)
    next if d[x][y]
    d[x][y] = z
    
    move.each do |i,j|
        next if d[x+i][y+j] || s[x+i][y+j] == ?#
        que.unshift([x+i, y+j, z])
    end

    warp.each do |i,j|
        que << [x+i, y+j, z+1] if s[x+i][y+j] == ?. && !d[x+i][y+j]
    end
end

p d[dh-1][dw-1] ? d[dh-1][dw-1] : -1
