n = int(input()) AB = [list(map(int,input().split())) for i in range(3)] ans = 0 ma = max([a for a,b in AB]) for i in range(3): AB = AB[1:] + [AB[0]] a1,b1 = AB[0] a2,b2 = AB[1] a3,b3 = AB[2] for x in range(ma+1): if x*a1 > n: break for y in range(ma+1): if x*a1 + y*a2 > n: break count = b1*x + b2*y left = n - x*a1 - y*a2 count += left//a3*b3 ans = max(ans,count) print(ans)