import sys

range = xrange

input = raw_input



n = int(eval(input()))

A = [int(x) for x in input().split()]

B = [int(x) for x in input().split()]





DP = [0]*(n+1)

DP[n] = n

for j in range(3):

    sell = A[j]

    get_back = B[j]

    if get_back > sell:

        for i in reversed(list(range(sell,len(DP)))):

            val1 = DP[i - sell]

            val2 = DP[i] + get_back - sell

            if val2 > val1:

                DP[i-sell] = val2

n = max(DP)

A,B = B,A



DP = [0]*(n+1)

DP[n] = n

for j in range(3):

    sell = A[j]

    get_back = B[j]

    if get_back > sell:

        for i in reversed(list(range(sell,len(DP)))):

            val1 = DP[i - sell]

            val2 = DP[i] + get_back - sell

            if val2 > val1:

                DP[i-sell] = val2



print((max(DP)))
import sys

range = xrange

input = raw_input



n = int(eval(input()))

A = [int(x) for x in input().split()]

B = [int(x) for x in input().split()]



DP = [0]*(n+1)

DP[n] = n

for j in range(3):

    sell = A[j]

    extra = B[j] - sell

    if extra > 0:

        for i in reversed(list(range(sell,len(DP)))):

            if DP[i] + extra > DP[i - sell]:

                DP[i-sell] = DP[i] + extra

n = max(DP)

A,B = B,A

 

DP = [0]*(n+1)

DP[n] = n

for j in range(3):

    sell = A[j]

    extra = B[j] - sell

    if extra > 0:

        for i in reversed(list(range(sell,len(DP)))):

            if DP[i] + extra > DP[i - sell]:

                DP[i-sell] = DP[i] + extra

 

print((max(DP)))
import sys

range = xrange
input = raw_input
n = int(eval(input()))
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
DP = [0] * (n + 1)
DP[n] = n
for j in range(3):
    sell = A[j]
    get_back = B[j]
    if get_back > sell:
        for i in reversed(list(range(sell, len(DP)))):
            val1 = DP[i - sell]
            val2 = DP[i] + get_back - sell
            if val2 > val1:
                DP[i - sell] = val2
n = max(DP)
A, B = B, A
DP = [0] * (n + 1)
DP[n] = n
for j in range(3):
    sell = A[j]
    get_back = B[j]
    if get_back > sell:
        for i in reversed(list(range(sell, len(DP)))):
            val1 = DP[i - sell]
            val2 = DP[i] + get_back - sell
            if val2 > val1:
                DP[i - sell] = val2
print((max(DP)))

import sys

range = xrange
input = raw_input
n = int(eval(input()))
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
DP = [0] * (n + 1)
DP[n] = n
for j in range(3):
    sell = A[j]
    extra = B[j] - sell
    if extra > 0:
        for i in reversed(list(range(sell, len(DP)))):
            if DP[i] + extra > DP[i - sell]:
                DP[i - sell] = DP[i] + extra
n = max(DP)
A, B = B, A
DP = [0] * (n + 1)
DP[n] = n
for j in range(3):
    sell = A[j]
    extra = B[j] - sell
    if extra > 0:
        for i in reversed(list(range(sell, len(DP)))):
            if DP[i] + extra > DP[i - sell]:
                DP[i - sell] = DP[i] + extra
print((max(DP)))

-    get_back = B[j] -    if get_back > sell: +    extra = B[j] - sell +    if extra > 0: -            val1 = DP[i - sell] -            val2 = DP[i] + get_back - sell -            if val2 > val1: -                DP[i - sell] = val2 +            if DP[i] + extra > DP[i - sell]: +                DP[i - sell] = DP[i] + extra -    get_back = B[j] -    if get_back > sell: +    extra = B[j] - sell +    if extra > 0: -            val1 = DP[i - sell] -            val2 = DP[i] + get_back - sell -            if val2 > val1: -                DP[i - sell] = val2 +            if DP[i] + extra > DP[i - sell]: +                DP[i - sell] = DP[i] + extra