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).flat_map do |i|
    (-2..2).map {|j| i.abs + j.abs > 1 ? [i, j] : nil}
end.compact

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
    f = false
    
    move.each do |i,j|
        next if !(x+i).between?(0,h-1) || !(y+j).between?(0,w-1) || d[x+i][y+j]
        if s[x+i][y+j] == ?#
            f = true
            next
        end
        que.unshift([x+i, y+j, z])
    end

    f and warp.each do |i,j|
        next if !(x+i).between?(0,h-1) || !(y+j).between?(0,w-1) || s[x+i][y+j] == ?# || d[x+i][y+j]
        que << [x+i, y+j, z+1]
    end
end

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