import java.io.PrintWriter
val pw = PrintWriter(System.out)
fun main(args: Array<String>) {
    func()
    pw.flush()
}
fun print(v: Any) { pw.print(v) }
fun println(v: Any) { pw.println(v) }

fun func() {
    val N = nextLong()
    val (gA, sA, bA) = listOfInt()
    val (gB, sB, bB) = listOfInt()
    val n = f(N, gA, sA, bA, gB, sB, bB)
    val ans = f(n, gB, sB, bB, gA, sA, bA)
    println(ans)
}

const val M = 100000

fun f(n: Long, ga: Int, sa: Int, ba: Int, gb: Int, sb: Int, bb: Int): Long {
    val dp = LongArray(M)
    for (i in 1..n.toInt()) {
        var v = i.toLong()
        if (i >= ga && ga < gb) {
            v = Math.max(v, dp[(i - ga + M) % M] + gb)
        }
        if (i >= sa && sa < sb) {
            v = Math.max(v, dp[(i - sa + M) % M] + sb)
        }
        if (i >= ba && ba < bb) {
            v = Math.max(v, dp[(i - ba + M) % M] + bb)
        }
        dp[i % M] = v
    }
    return dp[n.toInt() % M]
}
fun next() = readLine()!!
fun nextLong() = next().toLong()
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map { it.toInt() }
