n = int(input()) [[a1, b1], [a2, b2], [a3, b3]] = [list(map(int, input().split())) for _ in range(3)] amin, bmin = min(a1, a2, a3), min(b1, b2, b3) assert n > 0 and amin > 0 and bmin > 0 if a2 * b1 < a1 * b2: a1, b1, a2, b2 = a2, b2, a1, b1 if a3 * b1 < a1 * b3: a1, b1, a3, b3 = a3, b3, a1, b1 w = max(n // a1 - a2 - a3, 0) m = n - w * a1 + 1 dp = [-1 << 60] * 2048 r = 0 for i in range(m): dp[i & 2047] = r = max( dp[(i - a1) & 2047] + b1, dp[(i - a2) & 2047] + b2, dp[(i - a3) & 2047] + b3, r, ) print(r + w * b1)