#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input!{
        n: usize,
        mut aa: [i64;n],
        mut b: [i64;n],
    }
    let mut f = true;
    for i in 0..n {
        if aa[i] > b[i] {
            f = false;
        }
    }
    // a <= b
    let mut doub:Vec<i64> = b.clone();
    let mut a = aa.clone();
    doub.push(b[0]);
    doub.push(b[1]);
    a.push(aa[0]);
    a.push(aa[1]);
    // println!("{:?}", doub);
    let mut count = 0;
    let mut ans_f_true = false;
    let mut p = false;
    let mut q = false;
    loop {
        // minus check
        for i in 0..n {
            if a[i] > doub[i] {
                f = false;
            }
        }
        if f==false {
            q = true;
            break;
        }
        let g = count;
        // downgrade
        let d = doub.clone();
        for i in 1..n+1 {
            let r:i64 = (d[i] - a[i]) / (d[i-1] + d[i+1]);
            let l:i64 = d[i] - r*(d[i-1] + d[i+1]);
            if r>0 {
                count+=r;
                if i==n {
                    doub[0] = l;
                }
                if i==1 {
                    doub[n+1] = l;
                }
                doub[i] = l;
            }
        }
        if g==count {
            p = true;
            break;
        }
        // println!("{:?}", doub);
        // equal check
        let mut ans_f = true;
        for i in 0..n {
            if a[i] != doub[i] {
                ans_f = false;
            }
        }
        if ans_f {
            ans_f_true = true;
            break;
        }
    }
    // if p {
    //     println!("double eq");
    // }
    // if q {
    //     println!("down");
    // }
    if ans_f_true {
        println!("{}", count);
    }else{
        println!("-1");
    }
}

Measured memory_bytes: 16636