use std::io::*;
use std::str::FromStr;

fn main() {
    let n: usize = read();
    let (ga, sa, ba): (usize, usize, usize) = (read(), read(), read());
    let (gb, sb, bb): (usize, usize, usize) = (read(), read(), read());

    let mut ans = n;
    if ga <= gb {
        if sa <= sb {
            if ba <= bb {    // A:g,s,b
                let mut i = 0;
                while i * ga <= n {
                    let mut j = 0;
                    while i*ga + j*sa <= n {
                        let mut d = n - i*ga - j*sa;
                        let mut k = d / ba;
                        d -= k * ba;
                        d += i * gb + j * sb + k * bb;
                        ans = std::cmp::max(ans, d);
                        j += 1;
                    }
                    i += 1;
                }
            } else {    // A:g,s    B:b
                let mut i = 0;
                while i * ga <= n {
                    let m = n - i*ga;
                    let j = m / sa;
                    let mut d = n - i*ga - j*sa + i * gb + j * sb;
                    let mut k = d / bb;
                    d -= k * bb;
                    d += k * ba;
                    ans = std::cmp::max(ans, d);
                    i += 1;
                }
            }
        } else {
            if ba <= bb {    // A:g,b    B:s
                let mut i = 0;
                while i * ga <= n {
                    let m = n - i*ga;
                    let j = m / ba;
                    let mut d = n - i*ga - j*ba + i*gb + j*bb;
                    let mut k = d / sb;
                    d -= k * sb;
                    d += k * sa;
                    ans = std::cmp::max(ans, d);
                    i += 1;
                }
            } else {    // A:g  B:g,s
                let i = n / ga;
                let m = n - i*ga + i*gb;
                let mut j = 0;
                while j*sb <= m {
                    let mut d = m - j*sb;
                    let mut k = d / bb;
                    d -= k * bb;
                    d += j*sa + k*ba;
                    ans = std::cmp::max(ans, d);
                    j += 1;
                }
            }
        }
    } else {
        if sa <= sb {
            if ba <= bb {    // A:s,b    B:g
                let mut i = 0;
                while i * sa <= n {
                    let m = n - i * sa;
                    let j = m / ba;
                    let mut d = n - i*sa - j*ba + i*sb + j*bb;
                    let mut k = d / gb;
                    d -= k * gb;
                    d += k * ga;
                    ans = std::cmp::max(ans, d);
                    i += 1;
                }
            } else {    // A:s  B:g,b
                let i = n / sa;
                let m = n - i * sa + i * sb;
                let mut j = 0;
                while j*gb <= m {
                    let mut d = m - j*gb;
                    let mut k = d / bb;
                    d -= k * bb;
                    d += j * ga + k * ba;
                    ans = std::cmp::max(ans, d);
                    j += 1;
                }
            }
        } else {
            if ba <= bb {    // A:b  B:s,g
                let i = n / ba;
                let m = n - i*ba + i*bb;
                let mut j = 0;
                while j*gb <= m {
                    let mut d = m - j*gb;
                    let mut k = d / sb;
                    d -= k * sb;
                    d += j * ga + k * sa;
                    ans = std::cmp::max(ans, d);
                    j += 1;
                }
            } else {    // B:g,s,b
                let mut i = 0;
                while i * gb <= n {
                    let mut j = 0;
                    while i*gb + j*sb <= n {
                        let mut d = n - i*gb - j*sb;
                        let mut k = d / bb;
                        d -= k * bb;
                        d += i*ga + j*sa + k*ba;
                        ans = std::cmp::max(ans, d);
                        j += 1;
                    }
                    i += 1;
                }
            }
        }
    }
    println!("{}", ans);
}

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}


Measured memory_bytes: 4352