#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define INF  (1<<30)
#define INFL (1LL<<60)
#define MOD 1000000007
#define EPS ((ld)(1e-9))

#define sz(x) ((int)(x).size())
#define setz(x) memset(x, 0, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define rep(i, x)  for (int i = 0, _##i = (x); i < _##i; i++)
#define repp(i, s, e) for (int i = (s), _##i = (e); i < _##i; i++)
#define repr(i, s, e) for (int i = (s)-1, _##i = (e); i >= _##i; i--)
#define repi(i, x) for (auto &i : (x))


using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ld, ld> pdd;
typedef pair<ll, ll> pll;

template<typename T, typename V>
ostream &operator<<(ostream &os, const pair<T, V> pai) { 
    return os << '(' << pai.first << ' ' << pai.second << ')';
}

template<typename T>
ostream &operator<<(ostream &os, const vector<T> v) {
    cout << '[';
    rep(i, sz(v)) cout << v[i] << ",";
    cout << "]";
    return os;
}

template<typename T>
ostream &operator<<(ostream &os, const set<T> v) {
    cout << "{";
    for (auto p : v) cout << p << ",";
    cout << "}";
    return os;
}

template<typename T, typename V>
ostream &operator<<(ostream &os, const map<T, V> v) {
    cout << "{";
    for (auto p : v) cout << p << ",";
    cout << "}";
    return os;
}

#ifndef __SOULTCH
#define debug(...) 0
#define endl '\n'
#else
#define debug(...) cout << " [-] ", _dbg(#__VA_ARGS__, __VA_ARGS__)
template<class TH> void _dbg(const char *sdbg, TH h){ cout << sdbg << '=' << h << endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
    while(*sdbg != ',') cout << *sdbg++;
    cout << '=' << (h) << ','; 
    _dbg(sdbg+1, a...);
}
#endif

template<typename T> void get_max(T &a, T b) {a = max(a, b);}
template<typename T> void get_min(T &a, T b) {a = min(a, b);}

ll trade(ll in, pll t1, pll t2, pll t3) {
    vector<pll> cand;
    if (t1.fi < t1.se) cand.pb(t1);
    if (t2.fi < t2.se) cand.pb(t2);
    if (t3.fi < t3.se) cand.pb(t3);
    debug(cand);
    debug(in);
    if (sz(cand) == 0) return in;
    else if (sz(cand) == 1) {
        ll trades = in/cand[0].fi;
        return in+(cand[0].se-cand[0].fi)*trades;
    } else if (sz(cand) == 2) {
        ll mv = in;
        for (ll c1 = 0; c1*cand[0].fi <= in; c1++) {
            ll remain = in-cand[0].fi*c1;
            ll c2 = remain/cand[1].fi;
            assert(c1*cand[0].fi+c2*cand[1].fi <= in);
            get_max(mv, in+c1*(cand[0].se-cand[0].fi)+c2*(cand[1].se-cand[1].fi));
        }
        return mv;
    } else if (sz(cand) == 3) {
        ll mv = in;
        for (ll c1 = 0; c1*cand[0].fi <= in; c1++) {
            for (ll c2 = 0; c1*cand[0].fi+c2*cand[1].fi <= in; c2++) {
                ll remain = in-cand[0].fi*c1-cand[1].fi*c2;
                ll c3 = remain/cand[2].fi;
                assert(c1*cand[0].fi+c2*cand[1].fi+c3*cand[2].fi <= in);
                get_max(mv, in+c1*(cand[0].se-cand[0].fi)+c2*(cand[1].se-cand[1].fi)+c3*(cand[2].se-cand[2].fi));
            }
        }
        return mv;
    }
    assert(false);
}

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

int main(void) {
    iostream::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);

    cin >> N >> Ga >> Sa >> Ba >> Gb >> Sb >> Bb;
    ll t = trade(N, {Ga, Gb}, {Sa, Sb}, {Ba, Bb});
    t =    trade(t, {Gb, Ga}, {Sb, Sa}, {Bb, Ba});

    cout << t << endl;
}


Measured memory_bytes: 256