#include<bits/stdc++.h>
//ios::sync_with_stdio(false);
//cin.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<pii,int> ppii;
typedef pair<int,pii> pipi;
typedef pair<ll,ll> pll;
typedef pair<pll,ll> plpl;
typedef tuple<ll,ll,ll> tl;
ll mod=1000000007;
ll mod2=998244353;
ll inf=1000000000000000000;
double pi=2*acos(0);
#define rep(i,m,n) for(int i=m;i<n;i++)
#define rrep(i,n,m) for(int i=n;i>=m;i--)
ll lmax(ll a,ll b){
    if(a<b)return b;
    else return a;
}
ll lmin(ll a,ll b){
    if(a<b)return a;
    else return b;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n;cin>>n;
    ll ga,gb,sa,sb,ba,bb;
    cin>>ga>>sa>>ba>>gb>>sb>>bb;
    int ca=0,cb=0;
    if(ga<gb)cb++;
    else ca++;
    if(sa<sb)cb++;
    else ca++;
    if(ba<bb)cb++;
    else ca++;
    if(ca==0){
        ll dp[3][n+1];
        memset(dp,0,sizeof dp);
        rep(i,0,n/ga+1){
            dp[0][n-i*ga]=n+(gb-ga)*i;
            
        }
        rep(i,0,n+1){
            for(int j=0;;j++){
                if(i-j*sa<0)break;
                dp[1][i-j*sa]=max(dp[1][i-j*sa],dp[0][i]+(sb-sa)*j);
            }
        }
        ll ans=0;
        rep(i,0,n+1){
            for(int j=0;;j++){
                if(i-j*ba<0)break;
                dp[2][i-j*ba]=max(dp[2][i-j*ba],dp[1][i]+(bb-ba)*j);
                ans=max(ans,dp[2][i-j*ba]);
            }
        }
        cout<<ans<<endl;
    }
    else if(ca==1){
        ll ma=n;
        vector<ll> v,w;
        ll a,b;
        if(ga<gb){
            v.push_back(ga);
            w.push_back(gb);
        }
        else{
            a=ga;
            b=gb;
        }
        if(sa<sb){
            v.push_back(sa);
            w.push_back(sb);
        }
        else{
            a=sa;
            b=sb;
        }
        if(ba<bb){
            v.push_back(ba);
            w.push_back(bb);
        }
        else{
            a=ba;
            b=bb;
        }
        rep(i,0,n+1){
            ma=max(ma,(i/v[0])*w[0]+i%v[0]+((n-i)/v[1])*w[1]+(n-i)%v[1]);
        }
        cout<<(ma/b)*a+ma%b<<endl;
    }
    else if(ca==2){
        vector<ll> v,w;
        ll a,b;
        if(ga>=gb){
            v.push_back(ga);
            w.push_back(gb);
        }
        else{
            a=ga;
            b=gb;
        }
        if(sa>=sb){
            v.push_back(sa);
            w.push_back(sb);
        }
        else{
            a=sa;
            b=sb;
        }
        if(ba>=bb){
            v.push_back(ba);
            w.push_back(bb);
        }
        else{
            a=ba;
            b=bb;
        }
        n=(n/a)*b+n%a;
        ll ma=n;
        rep(i,0,n+1){
            ma=max(ma,(i/w[0])*v[0]+i%w[0]+((n-i)/w[1])*v[1]+(n-i)%w[1]);
        }
        cout<<ma<<endl;
    }
    else if(ca==3){
        ll dp[3][n+1];
        memset(dp,0,sizeof dp);
        rep(i,0,n/gb+1){
            dp[0][n-i*gb]=n+(ga-gb)*i;
            
        }
        rep(i,0,n+1){
            for(int j=0;;j++){
                if(i-j*sb<0)break;
                dp[1][i-j*sb]=max(dp[1][i-j*sb],dp[0][i]+(sa-sb)*j);
            }
        }
        ll ans=0;
        rep(i,0,n+1){
            for(int j=0;;j++){
                if(i-j*bb<0)break;
                dp[2][i-j*bb]=max(dp[2][i-j*bb],dp[1][i]+(ba-bb)*j);
                ans=max(ans,dp[2][i-j*bb]);
            }
        }
        cout<<ans<<endl;
        
    }
}


Measured memory_bytes: 384