N = int(input()) AB = [list(map(int, input().split())) for _ in range(3)] XAB = [[AB[i][1] / AB[i][0], AB[i][0], AB[i][1]] for i in range(len(AB))] XAB.sort() _, Ai, Bi = XAB[0] _, Aj, Bj = XAB[1] _, Ak, Bk = XAB[2] ans = 0 for i in range(2000): for j in range(2000): if N - Ai * i - Aj * j < 0: continue k = (N - (Ai * i + Ai * j)) // Ak ans = max(ans, Bi * i + Bj * j, Bk * k) print(ans)