N = int(input()) AB = [list(map(int, input().split())) for _ in range(3)] INF = 1<<60 top, bottom = 0, 1 a, b = -1, -1 for A, B in AB: if bottom*B > top*A: top, bottom = B, A a, b = A, B ans = 0 if 10**7 < N: rem = N-10**7 n = (rem+a-1)//a ans = b*n N -= n*a dp = [-INF]*(N+1) dp[0] = 0 for i in range(N+1): for A, B in AB: if i+A <= N: dp[i+A] = max(dp[i+A], dp[i]+B) ans += max(dp) print(ans)