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

    ([x-2,0].max .. [x+2,h-1].min).each do |xx|
        ([y-2,0].max .. [y+2,w-1].min).each do |yy|
            zz = (x - xx).abs + (y - yy).abs

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

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