use std::{
    collections::HashSet,
    io::{read_to_string, stdin},
};

const ROW_MAX: usize = 12;

fn array_sum(a: &[usize; ROW_MAX + 1], b: &[usize; ROW_MAX + 1]) -> [usize; ROW_MAX + 1] {
    let mut c = [0; ROW_MAX + 1];
    for i in 0..ROW_MAX + 1 {
        c[i] = a[i] + b[i];
    }
    c
}

fn consume_check(groups_remain: &[usize; ROW_MAX + 1], cx: &[usize; ROW_MAX + 1]) -> bool {
    groups_remain.iter().zip(cx.iter()).all(|(g, c)| g >= c)
}

fn consume(groups_remain: &mut [usize; ROW_MAX + 1], cx: &[usize; ROW_MAX + 1]) {
    for i in 0..ROW_MAX + 1 {
        groups_remain[i] -= cx[i];
    }
}

fn restore(groups_remain: &mut [usize; ROW_MAX + 1], cx: &[usize; ROW_MAX + 1]) {
    for i in 0..ROW_MAX + 1 {
        groups_remain[i] += cx[i];
    }
}

fn check_empty(groups_remain: &[usize; ROW_MAX + 1]) -> bool {
    groups_remain.iter().all(|&g| g == 0)
}

fn main() {
    let temp = read_to_string(stdin()).unwrap();
    let mut iter = temp
        .split_ascii_whitespace()
        .map(|x| x.parse::<usize>().unwrap());
    let n = iter.next().unwrap();
    let mut groups = [0; ROW_MAX + 1];
    for i in 1..=n {
        groups[i] = iter.next().unwrap();
    }

    let mut cands: Vec<HashSet<[usize; ROW_MAX + 1]>> = vec![HashSet::new(); ROW_MAX + 1];
    cands[0].insert([0; ROW_MAX + 1]);
    cands[1].insert([0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
    cands[2].insert([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);

    for i in 3..=ROW_MAX {
        let mut arr = [0; ROW_MAX + 1];
        arr[i] = 1;
        cands[i].insert(arr);

        for j in i / 2..i - 1 {
            let k = i - 1 - j;
            let mut v = vec![];
            for can1 in cands[j].iter() {
                for can2 in cands[k].iter() {
                    v.push(array_sum(can1, can2));
                }
            }
            for a in v {
                cands[i].insert(a);
            }
        }
    }

    let mut deq = vec![];
    deq.push((0, 1, [0usize; ROW_MAX + 1]));

    let mut res = 13;
    while !deq.is_empty() {
        let (type_id, level, delta) = deq.pop().unwrap();

        if type_id == 0 {
            consume(&mut groups, &delta);
            if check_empty(&groups) {
                res = res.min(level - 1);
                continue;
            }
            if level >= 13 {
                continue;
            }
            let mut flag = false;
            for i in (1..=level).rev() {
                let v = cands[i]
                    .iter()
                    .filter(|c| consume_check(&groups, c))
                    .collect::<Vec<_>>();

                if v.is_empty() {
                    continue;
                }
                flag = true;
                for &e in v {
                    deq.push((1, level + 1, e));
                    deq.push((0, level + 1, e));
                }
            }
            if !flag {
                for &e in cands[0].iter() {
                    deq.push((1, level + 1, e));
                    deq.push((0, level + 1, e));
                }
            }
        } else {
            restore(&mut groups, &delta);
        }
    }
    if res >= 13 {
        println!("{}", -1);
    } else {
        println!("{}", res);
    }
}
