fun main(args: Array<String>) {
    val n = readLine()!!.toLong()
    val (ga, sa, ba) = readLine()!!.split(" ").map { it.toInt() }
    val (gb, sb, bb) = readLine()!!.split(" ").map { it.toInt() }
    val n2 = solve(n, ga, sa, ba, gb, sb, bb)
    val n3 = solve(n2, gb, sb, bb, ga, sa, ba)
    println(n3)
}

const val M = 100000

fun solve(n: Long, ga: Int, sa: Int, ba: Int, gb: Int, sb: Int, bb: Int): Long {
    val nInt = n.toInt()
    val dp = LongArray(M)
    for (i in 1..nInt) {
        var value = i.toLong()
        if (i >= ga && ga < gb) {
            value = Math.max(value, dp[(i - ga + M) % M] + gb)
        }
        if (i >= sa && sa < sb) {
            value = Math.max(value, dp[(i - sa + M) % M] + sb)
        }
        if (i >= ba && ba < bb) {
            value = Math.max(value, dp[(i - ba + M) % M] + bb)
        }
        dp[i % M] = value
    }
    return dp[nInt % M]
}
