#include<bits/stdc++.h>
using namespace std;

#define lint long long
#define P pair<int, int>
#define LLP pair<long long, long long>
#define REP(i, x, n) for(int i = (x), i##_len = int(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = int(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = int(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = int(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = int(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())

const int IINF = (1 << 30) - 1;
const long long LLINF = 1LL << 61;
const long long MOD = 1000000007LL;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

long long gcd(long long _x, long long _y){
    long long _r = _x % _y;
    while(_r != 0LL){
        _x = _y;
        _y = _r;
        _r = _x % _y;
    }
    return _y;
}

inline long long lcm(long long _x, long long _y){
    return _x / gcd(_x, _y) * _y;
}

template<typename T>
bool chmax(T &_a, T _b){
    if(_a < _b){
        _a = _b;
        return true;
    }else{
        return false;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    lint n, ga, sa, ba, gb, sb, bb;
    cin >> n >> ga >> sa >> ba >> gb >> sb >> bb;

    lint ans = n;
    // a to b
    for(lint gold = 0 ; gold <= n ; ++gold){
        for(lint silver = 0 ; gold + silver <= n ; ++silver){
            lint bronze = n - (gold + silver);
            lint cnt = 0;
            if(ga < gb){
                cnt += gold / ga * gb;
                cnt += gold % ga;
            }else{
                cnt += gold;
            }
            if(sa < sb){
                cnt += silver / sa * sb;
                cnt += silver % sa;
            }else{
                cnt += silver;
            }
            if(ba < bb){
                cnt += bronze / ba * bb;
                cnt += bronze % ba;
            }else{
                cnt += bronze;
            }
            ans = max(ans, cnt);
        }
    }

    // b to a
    if(ans == n){
        for(lint gold = 0 ; gold <= n ; ++gold){
            for(lint silver = 0 ; gold + silver <= n ; ++silver){
                lint bronze = n - (gold + silver);
                lint cnt = 0;
                if(gb < ga){
                    cnt += gold / gb * ga;
                    cnt += gold % gb;
                }else{
                    cnt += gold;
                }
                if(sb < sa){
                    cnt += silver / sb * sa;
                    cnt += silver % sb;
                }else{
                    cnt += silver;
                }
                if(bb < ba){
                    cnt += bronze / bb * ba;
                    cnt += bronze % bb;
                }else{
                    cnt += bronze;
                }
                ans = max(ans, cnt);
            }
        }
    }else{
        lint l = 1;
        if(gb < ga){
            l = lcm(l, gb);
        }
        if(sb < sa){
            l = lcm(l, sb);
        }
        if(bb < ba){
            l = lcm(l, bb);
        }

        vector<lint> dp(l + 1, 0LL);
        rep(i, l){
            chmax(dp[i + 1], dp[i]);
            if(i + gb <= l){
                chmax(dp[i + gb], dp[i] + (ga - gb));
            }
            if(i + sb <= l){
                chmax(dp[i + sb], dp[i] + (sa - sb));
            }
            if(i + bb <= l){
                chmax(dp[i + bb], dp[i] + (ba - bb));
            }
        }

        lint tmp = 0;
        for(lint i = 1 ; i <= l ; ++i){
            chmax(tmp, (ans / i) * dp[i] + dp[ans % i]);
        }
        ans += tmp;
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 18432