#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll,ll> P;
#define Mod 1000000007
using VP = vector<P>; using VVP = vector<VP>;
using VI = vector<ll>; using VVI = vector<VI>; using VVVI = vector<VVI>;
#define INF 1ll<<59


vector<ll> dp1(25002500,0),dp2(25002500,0);

int main(){
  ll i,j;
  ll n;
  cin>>n;
 
  ll ga,sa,ba,gb,sb,bb;
  cin>>ga>>sa>>ba>>gb>>sb>>bb;

  for(i=1;i<25002500;i++){
    dp1[i]=i;
    dp2[i]=i;
    if(i-ga>=0) dp1[i]=max(dp1[i],dp1[i-ga]+gb);
    if(i-sa>=0) dp1[i]=max(dp1[i],dp1[i-sa]+sb);
    if(i-ba>=0) dp1[i]=max(dp1[i],dp1[i-ba]+bb);
    if(i-gb>=0) dp2[i]=max(dp2[i],dp2[i-gb]+ga);
    if(i-sb>=0) dp2[i]=max(dp2[i],dp2[i-sb]+sa);
    if(i-bb>=0) dp2[i]=max(dp2[i],dp2[i-bb]+ba);
  }

  cout<<dp2[dp1[n]]<<endl;
  return 0;
}