#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#ifndef bhartiya
    #pragma GCC optimize("Ofast")
    #pragma GCC optimize("unroll-loops")
    // #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif


typedef long long ll;
typedef pair<ll, ll> pll;
typedef gp_hash_table<long long, long long> umap;
typedef tree<int, null_type, less<int>, rb_tree_tag,
		tree_order_statistics_node_update> oset;
// not that imp
typedef pair<pll, ll> plll;
typedef vector<ll> vl;
typedef vector<pll> vll;

#define inf 200000000000000ll
#define mod 1000000007ll
#define eps 1e-7
#define PI 3.1415926535897932385
// #define PI acos(-1)

#define pb push_back
#define bitc  __builtin_popcountll
#define mp make_pair
#define ff first
#define ss second
#define all(ar) ar.begin(), ar.end()

#define fr(i,a,b) for (ll i = (a), _b = (b); i <= _b; i++)
#define rep(i,n) for (ll i = 0, _n = (n); i < _n; i++)
#define repr(i,n) for (ll i = n - 1; i >= 0; i--)
#define frr(i,a,b) for (ll i = (a), _b = (b); i >= _b; i--)
#define foreach(it,ar) for (auto it = ar.begin(); it != ar.end(); it++)
#define fill(ar,val) memset(ar, val, sizeof(ar))

#ifdef bhartiya
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif

mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
inline bool equals(double a, double b) {return fabs(a - b) < 1e-9;}
ll gcd(ll a, ll b){ return b==0 ? a : gcd(b, a%b); }

const ll maxn = 500005;
ll n;
set<pll> balls;



int main() 
{ 
	ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0), cout.precision(10); //cout<<fixed;
    cin.exceptions(cin.failbit);
    #ifdef bhartiya
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int t = 1;
    // cin>>t;
    while(t--){
        cin>>n;
        ll x,y;
        ll a,b;
        ll p,q;
        cin>>x>>a>>p;
        cin>>y>>b>>q;
        ll ans = n;
        for(ll i=0;i<=n;i+=x){
            for(ll j=0; j<=n-i; j+=a){
                ll val = 0;
                val += i/x*y;
                val += j/a*b;
                val += max(n-i-j, (n-i-j)/p*q + (n-i-j)%p);
                ans = max(ans, val);
            }
        }
        n = ans;
        ll cnt = 0;
        if(y<x) cnt++;
        if(b<a) cnt++;
        if(q<p) cnt++;
        trace(cnt);
        if(cnt == 3){
            for(ll i=0;i<=n;i+=y){
                for(ll j=0; j<=n-i; j+=b){
                    ll val = 0;
                    val += i/y*x;
                    val += j/b*a;
                    val += max(n-i-j, (n-i-j)/q*p + (n-i-j)%q);
                    ans = max(ans, val);
                }
            }
        }
        else if(cnt == 2){
            if(q<p && !(y<x)){
                swap(x,p);
                swap(q,y);
            }
            else if(q<p && !(b<a)){
                swap(a,p);
                swap(q,b);
            }
            for(ll i=0;i<=n;i+=y){
                ll val = 0;
                val += i/y*x;
                val += max(n-i, (n-i)/b*a + (n-i)%b);
                ans = max(ans, val);
            }
        }
        else if(cnt == 1){
            ll val = 0;
            if(q<p){
                val += n/q*p + n%q;
            }
            else if(b<a){
                val += n/b*a + n%b;
            }
            else if(y<x){
                val += n/y*x + n%y;
            }
            ans = max(ans, val);
        }
        cout<<ans;
    }
    end_routine();
}

Measured memory_bytes: 256