import numpy as np n = int(input()) [[a1, b1], [a2, b2], [a3, b3]] = [list(map(int, input().split())) for _ in range(3)] assert n > 0 and a1 > 0 and a2 > 0 and a3 > 0 and b1 > 0 and b2 > 0 and b3 > 0 assert n < (1 << 64) and n // min(a1, a2, a3) * max(b1, b2, b3) < (1 << 64) if a2 * b1 < a1 * b2: a1, b1, a2, b2 = a2, b2, a1, b1 if a3 * b1 < a1 * b3: a1, b1, a3, b3 = a3, b3, a1, b1 w = max(n // a1 - (a2 + a3), 0) print( max( int(np.amax(((s - j * a3) // a1) * b1 + i * b2 + j * b3)) for i, j, s in zip( range(a1), map(lambda s: np.arange(0, min(a1, s // a3 + 1), dtype=np.uint64), range(n - w * a1, -1, -a2)), range(n - w * a1, -1, -a2), ) ) + w * b1 )