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}

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

while !d[dh-1][dw-1] && !que.empty?
    x, y, z = que.shift
    next if d[x][y]
    d[x][y] = z

    (-2..2).each do |i|
        xx = x + i
        next if !xx.between?(0, h-1)

        (-2..2).each do |j|
            yy = y + j
            zz = i.abs + j.abs

            if !yy.between?(0, w-1) || s[xx][yy] == ?# || d[xx][yy]
                next
            elsif zz == 1
                que.unshift([xx, yy, z])
            elsif zz != 0
                que << [xx, yy, z+1]
            end
        end
    end
end

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