import sys input = sys.stdin.readline N = int(input()) A1, B1 = map(int, input().split()) A2, B2 = map(int, input().split()) A3, B3 = map(int, input().split()) D = 0 for x1 in range(min(A2, A3)+1): for x2 in range(min(A1, A3)+1): remN = N-A1*x1-A2*x2 if remN < 0: break D = max(D, B1*x1+B2*x2+B3*(remN//A3)) for x3 in range(min(A2, A1)+1): for x2 in range(min(A1, A3)+1): remN = N-A3*x3-A2*x2 if remN < 0: break D = max(D, B1*(remN//A1)+B2*x2+B3*x3) for x3 in range(min(A2, A1)+1): for x1 in range(min(A2, A3)+1): remN = N-A3*x3-A1*x1 if remN < 0: break D = max(D, B1*x1+B2*(remN//A2)+B3*x3) print(D)