#include<bits/stdc++.h>

#define int long long
using namespace std;
#define rep(i, n) for(int i=0;i<(n);++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

int N;
int GA, SA, BA;
int GB, SB, BB;
int dp1[5][5050];

int DP1() {
    vector<pii> vw;
    if (GB > GA)vw.push_back(pii(GB - GA, GA));
    if (SB > SA)vw.push_back(pii(SB - SA, SA));
    if (BB > BA)vw.push_back(pii(BB - BA, BA));
    if (vw.size() == 0)return N;
    rep(i, vw.size()) {
        int v = vw[i].first;
        int w = vw[i].second;
        for (int j = 0; j <= N; ++j) {
            if (j < w) {
                dp1[i + 1][j] = dp1[i][j];
            } else {
                dp1[i + 1][j] = max(dp1[i][j], dp1[i + 1][j - w] + v);
            }
        }
    }
    return dp1[vw.size()][N] + N;
}

int DP2() {
    if (GB < GA) {
        //assert(false);
        //SB<SA,BB<BA
        //2で何もしていないはず
        vector<pii> vw;
        //dp1は綺麗なので再利用
        vw.push_back(pii(GA - GB, GB));
        vw.push_back(pii(SA - SB, SB));
        vw.push_back(pii(BA - BB, BB));
        rep(i, vw.size()) {
            int v = vw[i].first;
            int w = vw[i].second;
            for (int j = 0; j <= N; ++j) {
                if (j < w) {
                    dp1[i + 1][j] = dp1[i][j];
                } else {
                    dp1[i + 1][j] = max(dp1[i][j], dp1[i + 1][j - w] + v);
                }
            }
        }
        return dp1[3][N] + N;
    } else if (SB < SA) {
        //GB>GA,SB<SA,BB<BA
        //BA-BB>=SA-SB
        if (N / BB < N / SB) {
            {
                int ans = 0;
                //BB>2
                for (int b = N / BB; b >= 0; --b) {
                    int s = (N - b * BB) / SB;
                    int tmp = 0;
                    tmp -= b * BB + s * SB;
                    tmp += b * BA + s * SA;
                    ans = max(ans, tmp);
                }
                return N + ans;
            }
        } else if (N / BB > N / SB) {
            {
                int ans = 0;
                for (int s = N / SB; s >= 0; --s) {
                    int b = (N - s * SB) / BB;
                    int tmp = 0;
                    tmp -= b * BB + s * SB;
                    tmp += b * BA + s * SA;
                    ans = max(ans, tmp);
                }
                return N + ans;
            }
        } else {
            if (BB * (SA - SB) >= SB * (BA - BB)) {
                //銀に変える
                int s = N / SB;
                N -= s * SB;
                N += s * SA;
                return N;
            } else {
                //銅に変える
                int b = N / BB;
                N -= b * BB;
                N += b * BA;
                return N;
            }
        }
    } else if (BB < BA) {
        //GB>GA,SB>SA,BB<BA
        //変えられるだけ銅に変える
        int b = N / BB;
        N -= b * BB;
        N += b * BA;
        return N;
    } else {
        return N;
    }
}

signed main() {
    cin >> N;
    cin >> GA >> SA >> BA;
    cin >> GB >> SB >> BB;
    //
    if (GB - GA < SB - SA) {
        swap(GB, SB);
        swap(GA, SA);
    }
    if (SB - SA < BB - BA) {
        swap(SB, BB);
        swap(SA, BA);
    }
    if (GB - GA < SB - SA) {
        swap(GB, SB);
        swap(GA, SA);
    }
    //GB-GA>=SB-SA>=BB-BA
    N = DP1();
    int N2 = DP2();
    cout << N2 << endl;
    return 0;
}


Measured memory_bytes: 384