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}

move = [[1,0], [-1,0], [0,1], [0,-1]]
warp = (-2..2).to_a.product((-2..2).to_a) - move - [[0,0]]

init = 1 << 30
d = Array.new(h) {Array.new(w, init)}
t = [[ch-1, cw-1]]
d[ch-1][cw-1] = 0
(0..).each do |k|
    break if t.empty? || d[dh-1][dw-1] < init
    u = []

    while (x,y = t.pop)
        move.each do |i,j|
            xx = x + i
            yy = y + j
            if xx.between?(0, h-1) && yy.between?(0, w-1) && s[xx][yy] == ?. && d[xx][yy] > k
                d[xx][yy] = k
                t << [xx, yy]
            end
        end

        warp.each do |i,j|
            xx = x + i
            yy = y + j
            if xx.between?(0, h-1) && yy.between?(0, w-1) && s[xx][yy] == ?. && d[xx][yy] > k + 1
                d[xx][yy] = k + 1
                u << [xx, yy]
            end
        end
    end

    t = u
end

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