import sys input = sys.stdin.readline N=int(input()) AB=[list(map(int,input().split())) for i in range(3)] X=[AB[i][1]/AB[i][0] for i in range(3)] MAX=max(X) ind=X.index(MAX) a,b=AB[ind] k=max(0,N-10**7)//a ANS=k*b N-=k*a ANS2=0 for i in range(N//AB[0][0]+1): rest=N-i*AB[0][0] MIN=0 MAX=rest//AB[1][0] while MAX-MIN>5: mid1=MIN+(MAX-MIN)//3 mid2=MIN+(MAX-MIN)//3*2 rest2=rest-mid1*AB[1][0] c1=i*AB[0][1]+mid1*AB[1][1]+(rest2//AB[2][0])*AB[2][1] rest2=rest-mid2*AB[1][0] c2=i*AB[0][1]+mid2*AB[1][1]+(rest2//AB[2][0])*AB[2][1] if c1>c2: MAX=mid2 else: MIN=mid1 for j in range(MIN,MAX+1): rest2=rest-j*AB[1][0] ANS2=max(ANS2,i*AB[0][1]+j*AB[1][1]+(rest2//AB[2][0])*AB[2][1]) print(ANS+ANS2)