let l = require("fs").readFileSync("/dev/stdin", "utf8").trim().split("\n"); let n = BigInt(l[0]); let [a0, b0] = l[1].split(" ").map((s) => BigInt(s)); let [a1, b1] = l[2].split(" ").map((s) => BigInt(s)); let [a2, b2] = l[3].split(" ").map((s) => BigInt(s)); function co_solve(n, a0, b0, a1, b1, a2, b2) { let r = 0n; for (let i = 0n; i < a0; i += 1n) { let d = n - i * a1; if (d < 0n) { break; } for (let j = 0n; j < a0; j += 1n) { let t = d / a0 * b0 + i * b1 + j * b2; if (r < t) { r = t; } d -= a2; if (d < 0n) { break; } } } return r; } let r1 = co_solve(n, a0, b0, a1, b1, a2, b2); let r2 = co_solve(n, a1, b1, a2, b2, a0, b0); let r3 = co_solve(n, a2, b2, a0, b0, a1, b1); let r = r1; if (r < r2) { r = r2; } if (r < r3) { r = r3; } console.log(r.toString());