#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll

#define REP(i,n) for(int i=0;i<n;++i)
#define ALL(name) name.begin(),name.end()
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#  define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#  define DBG(fmt, ...)
#endif

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 1000000007;
#define MAX_N 100010
#define SUBMIT

ll N;
ll Ga, Sa, Ba, Gb, Sb, Bb;

// B, S, B のうち bool が true のものだけチェック
// どんぐりを最初 n こもっているときの、最大化可能数
// isAtoB が true なら A -> B をチェック
ll check(bool isG, bool isS, bool isB, ll n, bool isAtoB) {
    int checkCnt = static_cast<ll>(isG) + static_cast<ll>(isS) + static_cast<ll>(isB);
    ll Gx = Ga, Gy = Gb;
    ll Sx = Sa, Sy = Sb;
    ll Bx = Ba, By = Bb;
    if(!isAtoB) {
        swap(Gx, Gy);
        swap(Sx, Sy);
        swap(Bx, By);
    }

    ll ret = n;
    if(checkCnt == 1) {
        for(ll i = 0; i <= n; ++i) {
            ll cand = n - i;
            ll a = Gx, b = Gy;
            if(isS) { a = Sx; b = Sy; }
            else if(isB) { a = Bx; b = By; }
            cand += max(i, (i / a) * b + (i % a));
            ret = max(ret, cand);
        }
    }
    else if(checkCnt == 2) {
        for(ll i = 0; i <= n; ++i) {
            ll j = n - i;
            ll k = n - i - j;
            ll cand = 0;

            if(isG && isS) {
                cand += max(i, (i / Gx) * Gy + (i % Gx));
                cand += max(j, (j / Sx) * Sy + (j % Sx));
                cand += max(k, (k / Bx) * By + (k % Bx));
            }
            else if(isS && isB) {
                cand += max(k, (k / Gx) * Gy + (k % Gx));
                cand += max(j, (j / Sx) * Sy + (j % Sx));
                cand += max(i, (i / Bx) * By + (i % Bx));
            }
            else if(isG && isB){
                cand += max(j, (j / Gx) * Gy + (j % Gx));
                cand += max(k, (k / Sx) * Sy + (k % Sx));
                cand += max(i, (i / Bx) * By + (i % Bx));
            }

            ret = max(ret, cand);
        }
    }
    else if(checkCnt == 3) {
        for(ll i = 0; i <= n; ++i) {
            for(ll j = 0; j <= (n - i); ++j) {
                ll k = n - i - j;
                ll cand = 0;

                if(isG && isS) {
                    cand += max(i, (i / Gx) * Gy + (i % Gx));
                    cand += max(j, (j / Sx) * Sy + (j % Sx));
                    cand += max(k, (k / Bx) * By + (k % Bx));
                }
                else if(isS && isB) {
                    cand += max(k, (k / Gx) * Gy + (k % Gx));
                    cand += max(j, (j / Sx) * Sy + (j % Sx));
                    cand += max(i, (i / Bx) * By + (i % Bx));
                }
                else if(isG && isB){
                    cand += max(j, (j / Gx) * Gy + (j % Gx));
                    cand += max(k, (k / Sx) * Sy + (k % Sx));
                    cand += max(i, (i / Bx) * By + (i % Bx));
                }

                ret = max(ret, cand);
            }
        }
    }
    return ret;
}

ll solve() {
    // A -> B でチェックするべきかどうか
    bool isG = (Ga <= Gb), isS = (Sa <= Sb), isB = (Ba <= Bb);
    ll ans = check(isG, isS, isB, N, true);

    // A -> B をチェックしなかったものだけ B -> A をチェック
    ans = check(!isG, !isS, !isB, ans, false);

    return ans;
}

ll blute_force() {
    ll ret = N;
    ll a_b = 0;
    // A -> B
    for(ll g = 0; g <= N; ++g) {
        for(ll s = 0; s <= (N - g); ++s) {
            for(ll b = 0; b <= (N - g - s); ++b) {
                ll cand = N - g - s - b;
                cand += (g / Ga) * Gb;
                cand += (s / Sa) * Sb;
                cand += (b / Ba) * Bb;
                a_b = max(a_b, cand);
            }
        }
    }
    // B -> A
    for(ll g = 0; g <= a_b; ++g) {
        for(ll s = 0; s <= (a_b - g); ++s) {
            for(ll b = 0; b <= (a_b - g - s); ++b) {
                ll cand = a_b - g - s - b;
                cand += (g / Gb) * Ga;
                cand += (s / Sb) * Sa;
                cand += (b / Bb) * Ba;
                ret = max(ret, cand);
            }
        }
    }
    return ret;
}

signed main() {
#ifdef SUBMIT
    cin.tie(0); cout.tie(0);
    ios::sync_with_stdio(false);

    cin >> N >> Ga >> Sa >> Ba >> Gb >> Sb >> Bb;
    printf("%lld\n", solve());
    //printf("%lld\n", blute_force());
#else
    random_device r;
    mt19937 mt(r());
    uniform_int_distribution<> dist(1, 30);

    REP(loop, 100) {
        N = dist(mt);
        Ga = dist(mt);
        Sa = dist(mt);
        Ba = dist(mt);
        Gb = dist(mt);
        Sb = dist(mt);
        Bb = dist(mt);
        ll cand = solve();
        ll ans = blute_force();
        if(cand != ans) {
            printf("N: %lld Ga: %lld Sa: %lld Ba: %lld Gb: %lld Sb: %lld Bb: %lld\n",
                    N, Ga, Sa, Ba, Gb, Sb, Bb);
            printf("cand: %lld ans: %lld\n", cand, ans);
            return 0;
        }
    }
    printf("OK\n");
#endif

    return 0;
}


Measured memory_bytes: 256