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 solve(n, ap, bp, aq, bq, ar, br) { let r = 0n, imax = ap * bq, jmax = ap * br; for (let i = 0n, di = n; i < imax && di >= 0n; i += bq, di -= aq) { for (let j = 0n, dj = di; j < jmax && dj >= 0n; j += br, dj -= ar) { let t = dj / ap * bp + i + j; r = (r < t) ? t : r; } } return r; } let r1 = solve(n, a0, b0, a1, b1, a2, b2); let r2 = solve(n, a1, b1, a2, b2, a0, b0); let r3 = solve(n, a2, b2, a0, b0, a1, b1); let r = (r1 > r2 && r1 > r3) ? r1 : (r2 > r3) ? r2 : r3; console.log(r.toString());