#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;

typedef long long ll;
typedef vector <ll> v_ll;
typedef vector <vector <ll> > vv_ll;
typedef vector < pair <ll,ll> > vp_ll;
typedef pair <ll,ll> p_ll;

bool TEST=false;
ll MOD = 1000000007;

void show(){if (TEST){cout << endl;}}
void show(string x){if (TEST){cout << x;}}
void show(string x,bool enter){
  if (TEST){if (enter) cout << "<#> " << x << endl;else cout << x;}
}
void show(ll x){if (TEST){cout << x;}}
void show(ll x,bool enter){
  if (TEST){if (enter) cout << "<#> " << x << endl;else cout << x;}
}
void show(p_ll p){
  if (TEST){cout << "{ ";show(p.first);cout << " ";show(p.second);cout << " }";}
}
void show(v_ll v){
  if (TEST){
    cout << "<begin> ";
    for (ll i=0;i<v.size();i++){show(v[i]); cout << " ";}cout <<"<end>"<< endl;
}}
void show(vv_ll v){
  if (TEST){
    cout << "//////////" << endl;
    for (ll i=0;i<v.size();i++){
      show(v[i]); cout << endl;
}}}
void show(vp_ll v){
  if (TEST){
    cout << "//////////" << endl;
    for (ll i=0;i<v.size();i++){
      show(v[i]); cout << endl;
}}}

ll mod(ll x){
  ll n=MOD;
  ll ret=x%n;
  if (ret<0)ret+=n;
  return ret;
}
void mod_self(ll &x){
  x=mod(x);
}
ll mod_inv(ll x){
  ll n=MOD;
  ll y = x,z = n-2, ret=1;
  y=mod(y);
  while (z>0){
    if(z%2==1) ret=mod(ret*y);
    y=mod(y*y);
    z/=2;
  }
  return ret;
}
ll mod_pow(ll x,ll a){
  ll y = x,z = a,ret=1;
  y=mod(y);
  if (z<0)z=-z;
  while (z>0){
    if(z%2==1) ret=mod(ret*y);
    y=mod(y*y);
    z/=2;
  }
  if (a<0) ret=mod_inv(ret);
  return ret;
}
v_ll mod_nck(ll n){
  v_ll nck(n+1,0);
  nck[0]=1;
  for (ll i=1;i<=n;i++){
    nck[i]=mod(nck[i-1]*(n+1-i));
    nck[i]=mod(nck[i]*mod_inv(i));
  }
  return nck;
}
v_ll mod_factorial(ll n){
  v_ll fct(n+1,0);
  fct[0]=1;
  for (ll i=1;i<=n;i++){
    fct[i]=mod(fct[i-1]*i);
  }
  return fct;
}

ll N,gA,gB,sA,sB,bA,bB;

ll max1(ll x, ll y){
  return x>y?x:y;
}

int main(){
  TEST=true;
  MOD=1000000007;
  cin >> N >> gA >> sA >> bA >> gB >> sB >> bB;
  ll gainG,gainS,gainB;
  gainG=gB-gA;
  gainS=sB-sA;
  gainB=bB-bA;
  if (gainG<gainS){
    swap(gainG,gainS);
    swap(gA,sA);
    swap(gB,sB);
  }
  if (gainS<gainB){
    swap(gainS,gainB);
    swap(sA,bA);
    swap(sB,bB);
  }
  if (gainG<gainS){
    swap(gainG,gainS);
    swap(gA,sA);
    swap(gB,sB);
  }
  ll ans1=0,ans2=0;
  ll hoge_max=0,hoge=0;

  if (gainG>=0 && gainS>=0 && gainB>=0){
    hoge_max=0;
    for (ll i=0;i<=N/gA;i++){
      for (ll j=0;j<=(N-i*gA)/sA;j++){
        ll g,s,b;
        g=i; s=j; b=(N-gA*i-sA*j)/bA;
        if (b>=0 && g*gA+s*sA+b*bA<=N){
          hoge = g*gainG+s*gainS+b*gainB;
          if (hoge>hoge_max)hoge_max=hoge;
        }else{break;}
      }
    }
    cout << N+hoge_max << endl;
    return 0;
  }
  if (gainG>=0 && gainS<0 && gainB<0){
    ll ans1=N+gainG*(N/gA);

    for (ll i=0;i<=ans1/sB;i++){
      ll s,b;
      s=i; b=(ans1-sB*i)/bB;
      if (b>=0 && s*sB+b*bB<=ans1){
        hoge = -s*gainS-b*gainB;
        if (hoge>hoge_max)hoge_max=hoge;
      }else{break;}
    }
    cout << ans1+hoge_max << endl;
    return 0;
  }
  if (gainG>=0 && gainS>=0 && gainB<0){
    for (ll i=0;i<=N/gA;i++){
      ll g,s;
      g=i; s=(N-gA*i)/sA;
      if (s>=0 && g*gA+s*sA<=N){
        hoge = g*gainG+s*gainS;
        if (hoge>hoge_max)hoge_max=hoge;
      }else{break;}
    }
    ans1=N+hoge_max;
    ans2=ans1+(-gainB)*(ans1/bB);
    cout << ans2 << endl;
    return 0;
  }
/*
  if (gainG<0 && gainS>=0 && gainB<0){
    ll ans1=N+gainS*(N/sA);

    for (ll i=0;i<=ans1/gB;i++){
      ll g,b;
      g=i; b=(ans1-gB*i)/bB;
      if (b>=0 && g*gB+b*bB<=ans1){
        hoge = -g*gainG-b*gainB;
        if (hoge>hoge_max)hoge_max=hoge;
      }else{break;}
    }
    cout << ans1+hoge_max << endl;
    return 0;
  }
  if (gainG<0 && gainS<0 && gainB>=0){
    ll ans1=N+gainB*(N/bA);

    for (ll i=0;i<=ans1/gB;i++){
      ll g,b;
      g=i; b=(ans1-gB*i)/bB;
      if (b>=0 && g*gB+b*bB<=ans1){
        hoge = -g*gainG-b*gainB;
        if (hoge>hoge_max)hoge_max=hoge;
      }else{break;}
    }
    cout << ans1+hoge_max << endl;
    return 0;
  }
  */
  if (gainG<0 && gainS<0 && gainB<0){
    for (ll i=0;i<=N/gB;i++){
      for (ll j=0;j<=(N-i*gB)/sB;j++){
        ll g,s,b;
        g=i; s=j; b=(N-gB*i-sB*j)/bB;
        if (b>=0 && g*gB+s*sB+b*bB<=N){
          hoge = -g*gainG-s*gainS-b*gainB;
          if (hoge>hoge_max)hoge_max=hoge;
        }else{break;}
      }
    }
    cout << N+hoge_max << endl;
    return 0;
  }
}


Measured memory_bytes: 888