//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<ll>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 100010001;
const ll LINF = (ll)INF*INF*10;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    cin >> n;
    ll g[2][3];
    rep(i, 2) rep(j, 3) cin >> g[i][j];

    if(g[0][0] > g[1][0] ) {
        if(g[0][1] > g[1][1]) {
            if(g[0][2] > g[1][2]) {
                vi dp(n+1);
                rep(i, n+1) {
                    dp[i] = i;
                }
                rep(i, n+1-g[1][0]) {
                    chmax(dp[i+g[1][0]], dp[i]+g[0][0]);
                }
                rep(i, n+1-g[1][1]) {
                    chmax(dp[i+g[1][1]], dp[i]+g[0][1]);
                }
                rep(i, n+1-g[1][2]) {
                    chmax(dp[i+g[1][2]], dp[i]+g[0][2]);
                }
                cout << dp[n] << endl;
            } else {
                n = n/g[0][2]*g[1][2] + n%g[0][2];
                ll ma = 0;
                for(int i = 0; i <= n; i += g[1][1]) {
                    chmax(ma, i/g[1][1]*g[0][1] + (n-i)%g[1][0] + (n-i)/g[1][0]*g[0][0]);
                }
                cout << ma << endl;
            }
        } else {
            if(g[0][2] > g[1][2]) {
                n = n/g[0][1]*g[1][1] + n%g[0][1];
                ll ma = 0;
                for(int i = 0; i <= n; i += g[1][2]) {
                    chmax(ma, i/g[1][2]*g[0][2] + (n-i)%g[1][0] + (n-i)/g[1][0]*g[0][0]);
                }
                cout << ma << endl; 
            } else {
                vi dp(n+1);
                rep(i, n+1) {
                    dp[i] = i;
                }
                rep(i, n+1-g[0][1]) {
                    chmax(dp[i+g[0][1]], dp[i]+g[1][1]);
                }
                rep(i, n+1-g[0][2]) {
                    chmax(dp[i+g[0][2]], dp[i]+g[1][2]);
                }
                cout << dp[n]%g[1][0] + dp[n]/g[1][0]*g[0][0] << endl;
            }
        }
    } else {
        if(g[0][1] > g[1][1]) {
            if(g[0][2] > g[1][2]) {
                n = n/g[0][0]*g[1][0] + n%g[0][0];
                ll ma = 0;
                for(int i = 0; i <= n; i += g[1][1]) {
                    chmax(ma, i/g[1][1]*g[0][1] + (n-i)%g[1][2] + (n-i)/g[1][2]*g[0][2]);
                }
                cout << ma << endl;
            } else {
                vi dp(n+1);
                rep(i, n+1) {
                    dp[i] = i;
                }
                rep(i, n+1-g[0][0]) {
                    chmax(dp[i+g[0][0]], dp[i]+g[1][0]);
                }
                rep(i, n+1-g[0][2]) {
                    chmax(dp[i+g[0][2]], dp[i]+g[1][2]);
                }
                cout << dp[n]%g[1][1] + dp[n]/g[1][1]*g[0][1] << endl;
            }
        } else {
            if(g[0][2] > g[1][2]) {
                vi dp(n+1);
                rep(i, n+1) {
                    dp[i] = i;
                }
                rep(i, n+1-g[0][0]) {
                    chmax(dp[i+g[0][0]], dp[i]+g[1][0]);
                }
                rep(i, n+1-g[0][1]) {
                    chmax(dp[i+g[0][1]], dp[i]+g[1][1]);
                }
                cout << dp[n]%g[1][2] + dp[n]/g[1][2]*g[0][2] << endl;
            } else {
                vi dp(n+1);
                rep(i, n+1) {
                    dp[i] = i;
                }
                rep(i, n+1-g[0][0]) {
                    chmax(dp[i+g[0][0]], dp[i]+g[1][0]);
                }
                rep(i, n+1-g[0][1]) {
                    chmax(dp[i+g[0][1]], dp[i]+g[1][1]);
                }
                rep(i, n+1-g[0][2]) {
                    chmax(dp[i+g[0][2]], dp[i]+g[1][2]);
                }
                cout << dp[n] << endl;
            }
        }
    }
}

Measured memory_bytes: 3668