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]]

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

    t.each do |x,y|
        next if d[x][y]
        d[x][y] = i

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

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

    t = u
end

p d[dh-1][dw-1] or -1