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 p = min(m, max(a1, a2, a3)) dp = [0] * m r = 0 for i in range(p): dp[i] = r = max( dp[i - a1] + b1 if i >= a1 else 0, dp[i - a2] + b2 if i >= a2 else 0, dp[i - a3] + b3 if i >= a3 else 0, r, ) for i in range(p, m): dp[i] = r = max( dp[i - a1] + b1, dp[i - a2] + b2, dp[i - a3] + b3, r, ) print(r + w * b1)