import math N = int(input()) AB = [tuple(map(int, input().split())) for i in range(3)] ans = 0 g = 1 for A, B in AB: g = g*A//math.gcd(g, A) for A, B in AB: ans = max(ans, N//g*((g//A)*B)) N %= g g=AB[1][0]*AB[2][0]//math.gcd(AB[1][0],AB[2][0]) h=max([(g//A)*B for A,B in AB[1:]]) tmp_max=0 for i in range(N+1): if AB[0][0]*i>N: break tmp=AB[0][1]*i m=N-AB[0][0]*i tmp+=(m//g)*h m%=g tmp_max_=0 for j in range(m+1): if AB[1][0]*j>m: break tmp_=AB[1][1]*j o=m-j*AB[1][0] tmp_+=AB[2][1]*(o//AB[2][0]) tmp_max_=max(tmp_max_,tmp_) tmp+=tmp_max_ tmp_max=max(tmp,tmp_max) print(ans+tmp_max)