import sys

input = sys.stdin.readline



def F(N,A,B):

    # n 個のどんぐりを持っている

    # a個がbの価値に変換できる

    AB = [(x,y) for x,y in zip(A,B) if x < y]

    dp = [0] * (N+1)

    for n in range(N+1):

        x = n

        for a,b in AB:

            if n >= a:

                y = dp[n-a] + b

                if x < y:

                    x = y

        dp[n] = x

    return dp[N]



N = int(eval(input()))

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

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



answer = F(F(N,A,B),B,A)

print(answer)
import sys

input = sys.stdin.readline



def F(N,A,B):

    AB = list(zip(A,B))

    AB = [(a,b) for a,b in AB if a < b] # 有用なやつだけ

    AB.sort(key = lambda x: x[1]/x[0], reverse = True)

    if len(AB) == 0:

        return N

    if len(AB) == 1:

        a,b = AB[0]

        x = N//a

        return N + (b-a) * x

    if len(AB) == 2:

        (a,b),(c,d) = AB

        # 2種類目の硬価はa回未満しか使わない：ac個はa優先なので

        return max(N + (N - i * c) // a * (b-a) + i * (d-c) for i in range(a))

    # 効率の良いものから並んでいる

    dp = [0] * (N+1)

    for n in range(N+1):

        x = n

        for a,b in AB:

            if n >= a:

                y = dp[n-a] + b

                if x < y:

                    x = y

        dp[n] = x

    return dp[N]



N = int(eval(input()))

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

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



answer = F(F(N,A,B),B,A)

print(answer)





AB = [(1,2),(3,4)]

(a,b),(c,d) = AB

import sys

input = sys.stdin.readline


def F(N, A, B):
    # n 個のどんぐりを持っている
    # a個がbの価値に変換できる
    AB = [(x, y) for x, y in zip(A, B) if x < y]
    dp = [0] * (N + 1)
    for n in range(N + 1):
        x = n
        for a, b in AB:
            if n >= a:
                y = dp[n - a] + b
                if x < y:
                    x = y
        dp[n] = x
    return dp[N]


N = int(eval(input()))
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
answer = F(F(N, A, B), B, A)
print(answer)

import sys

input = sys.stdin.readline


def F(N, A, B):
    AB = list(zip(A, B))
    AB = [(a, b) for a, b in AB if a < b]  # 有用なやつだけ
    AB.sort(key=lambda x: x[1] / x[0], reverse=True)
    if len(AB) == 0:
        return N
    if len(AB) == 1:
        a, b = AB[0]
        x = N // a
        return N + (b - a) * x
    if len(AB) == 2:
        (a, b), (c, d) = AB
        # 2種類目の硬価はa回未満しか使わない：ac個はa優先なので
        return max(N + (N - i * c) // a * (b - a) + i * (d - c) for i in range(a))
    # 効率の良いものから並んでいる
    dp = [0] * (N + 1)
    for n in range(N + 1):
        x = n
        for a, b in AB:
            if n >= a:
                y = dp[n - a] + b
                if x < y:
                    x = y
        dp[n] = x
    return dp[N]


N = int(eval(input()))
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
answer = F(F(N, A, B), B, A)
print(answer)
AB = [(1, 2), (3, 4)]
(a, b), (c, d) = AB

-    # n 個のどんぐりを持っている -    # a個がbの価値に変換できる -    AB = [(x, y) for x, y in zip(A, B) if x < y] +    AB = list(zip(A, B)) +    AB = [(a, b) for a, b in AB if a < b]  # 有用なやつだけ +    AB.sort(key=lambda x: x[1] / x[0], reverse=True) +    if len(AB) == 0: +        return N +    if len(AB) == 1: +        a, b = AB[0] +        x = N // a +        return N + (b - a) * x +    if len(AB) == 2: +        (a, b), (c, d) = AB +        # 2種類目の硬価はa回未満しか使わない：ac個はa優先なので +        return max(N + (N - i * c) // a * (b - a) + i * (d - c) for i in range(a)) +    # 効率の良いものから並んでいる +AB = [(1, 2), (3, 4)] +(a, b), (c, d) = AB