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

while !d[dh-1][dw-1] && !que.empty?
    x, y, z, mode = que.shift
    next if d[x][y]
    d[x][y] = z
    
    if mode == 0
        ([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, (xx - x) + (yy - y) * 2])
                else
                    que << [xx, yy, z+1, 0]
                end
            end
        end
    else
        que.unshift([x+1, y, z, 1]) if x < h-1 && s[x+1][y] == ?. && !d[x+1][y]
        que.unshift([x-1, y, z, -1]) if x > 0 && s[x-1][y] == ?. && !d[x-1][y]
        que.unshift([x, y+1, z, 2]) if y < w-1 && s[x][y+1] == ?. && !d[x][y+1]
        que.unshift([x, y-1, z, -2]) if y > 0 && s[x][y-1] == ?. && !d[x][y-1]

        case mode
        when 1
            next if x + 2 >= h
            ([y-2,0].max .. [y+2,w-1].min).each do |yy|
                que << [x+2, yy, z+1, 0] if s[x+2][yy] == ?. && !d[x+2][yy]
            end
        when -1
            next if x - 2 < 0
            ([y-2,0].max .. [y+2,w-1].min).each do |yy|
                que << [x-2, yy, z+1, 0] if s[x-2][yy] == ?. && !d[x-2][yy]
            end
        when 2
            next if y + 2 >= w
            ([x-2,0].max .. [x+2,h-1].min).each do |xx|
                que << [xx, y+2, z+1, 0] if s[xx][y+2] == ?. && !d[xx][y+2]
            end
        when -2
            next if y - 2 < 0
            ([x-2,0].max .. [x+2,h-1].min).each do |xx|
                que << [xx, y-2, z+1, 0] if s[xx][y-2] == ?. && !d[xx][y-2]
            end
        end
    end
end

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