from fractions import gcd

from functools import reduce

inpl = lambda: list(map(int,input().split()))

def lcm(a,b):

    return a*b//gcd(a,b)

def lcm_list(numbers):

    return reduce(lcm, numbers)



N = int(eval(input()))

rate = []

rate.append(inpl())

rate.append(inpl())

AB = []

BA = []

for i in range(3):

    rA, rB = rate[0][i], rate[1][i]

    if rB > rA:

        AB.append((rA, rB))

    elif rA > rB:

        BA.append((rB, rA))

for l in [AB, BA]:

    l.sort(key=lambda x: -x[1]/x[0])



def buy_and_sell(N, cost_ret):

    if len(cost_ret) == 0:

        return N

    else:

        Nunit = lcm_list([ x[0] for x in cost_ret ])

        b0, s0 = cost_ret[0]

        q = N // b0

        n = max(0, q - 2*Nunit//b0 + 1)

        Ndp = N - n*b0

        dp = [0]*(Ndp+1)

        w = [0]*(len(cost_ret)+1)

        for i in range(1,Ndp+1):

            k = 1

            for cost, ret in cost_ret:

                if i >= cost:

                    w[k] = dp[i-cost] + ret

                    k += 1

            dp[i] = max(dp[i-1]+1, max(w))

        return s0*n + dp[-1]



M = buy_and_sell(N, AB)

L = buy_and_sell(M, BA)

print(L)
from fractions import gcd

from functools import reduce

inpl = lambda: list(map(int,input().split()))

def lcm(a,b):

    return a*b//gcd(a,b)

def lcm_list(numbers):

    return reduce(lcm, numbers)



N = int(eval(input()))

rate = []

rate.append(inpl())

rate.append(inpl())

AB = []

BA = []

for i in range(3):

    rA, rB = rate[0][i], rate[1][i]

    if rB > rA:

        AB.append((rA, rB))

    elif rA > rB:

        BA.append((rB, rA))

for l in [AB, BA]:

    l.sort(key=lambda x: -x[1]/x[0])



def buy_and_sell(N, cost_ret):

    if len(cost_ret) == 0:

        return N

    else:

        Nunit = lcm_list([ x[0] for x in cost_ret ])

        b0, s0 = cost_ret[0]

        q = N // b0

        n = max(0, q - (len(cost_ret)-1)*Nunit//b0)

        Ndp = N - n*b0

        dp = [0]*(Ndp+1)

        w = [0]*(len(cost_ret)+1)

        for i in range(1,Ndp+1):

            k = 1

            for cost, ret in cost_ret:

                if i >= cost:

                    w[k] = dp[i-cost] + ret

                    k += 1

            dp[i] = max(dp[i-1]+1, max(w))

        return s0*n + dp[-1]



M = buy_and_sell(N, AB)

L = buy_and_sell(M, BA)

print(L)
from fractions import gcd
from functools import reduce

inpl = lambda: list(map(int, input().split()))


def lcm(a, b):
    return a * b // gcd(a, b)


def lcm_list(numbers):
    return reduce(lcm, numbers)


N = int(eval(input()))
rate = []
rate.append(inpl())
rate.append(inpl())
AB = []
BA = []
for i in range(3):
    rA, rB = rate[0][i], rate[1][i]
    if rB > rA:
        AB.append((rA, rB))
    elif rA > rB:
        BA.append((rB, rA))
for l in [AB, BA]:
    l.sort(key=lambda x: -x[1] / x[0])


def buy_and_sell(N, cost_ret):
    if len(cost_ret) == 0:
        return N
    else:
        Nunit = lcm_list([x[0] for x in cost_ret])
        b0, s0 = cost_ret[0]
        q = N // b0
        n = max(0, q - 2 * Nunit // b0 + 1)
        Ndp = N - n * b0
        dp = [0] * (Ndp + 1)
        w = [0] * (len(cost_ret) + 1)
        for i in range(1, Ndp + 1):
            k = 1
            for cost, ret in cost_ret:
                if i >= cost:
                    w[k] = dp[i - cost] + ret
                    k += 1
            dp[i] = max(dp[i - 1] + 1, max(w))
        return s0 * n + dp[-1]


M = buy_and_sell(N, AB)
L = buy_and_sell(M, BA)
print(L)

from fractions import gcd
from functools import reduce

inpl = lambda: list(map(int, input().split()))


def lcm(a, b):
    return a * b // gcd(a, b)


def lcm_list(numbers):
    return reduce(lcm, numbers)


N = int(eval(input()))
rate = []
rate.append(inpl())
rate.append(inpl())
AB = []
BA = []
for i in range(3):
    rA, rB = rate[0][i], rate[1][i]
    if rB > rA:
        AB.append((rA, rB))
    elif rA > rB:
        BA.append((rB, rA))
for l in [AB, BA]:
    l.sort(key=lambda x: -x[1] / x[0])


def buy_and_sell(N, cost_ret):
    if len(cost_ret) == 0:
        return N
    else:
        Nunit = lcm_list([x[0] for x in cost_ret])
        b0, s0 = cost_ret[0]
        q = N // b0
        n = max(0, q - (len(cost_ret) - 1) * Nunit // b0)
        Ndp = N - n * b0
        dp = [0] * (Ndp + 1)
        w = [0] * (len(cost_ret) + 1)
        for i in range(1, Ndp + 1):
            k = 1
            for cost, ret in cost_ret:
                if i >= cost:
                    w[k] = dp[i - cost] + ret
                    k += 1
            dp[i] = max(dp[i - 1] + 1, max(w))
        return s0 * n + dp[-1]


M = buy_and_sell(N, AB)
L = buy_and_sell(M, BA)
print(L)

-        n = max(0, q - 2 * Nunit // b0 + 1) +        n = max(0, q - (len(cost_ret) - 1) * Nunit // b0)