#ifdef DEBUG_IS_VALID
#define DEB 1 
#define _LIBCPP_DEBUG 0
#else
#define DEB 0
#define NDEBUG
#endif

#include <bits/stdc++.h>
using namespace std;

#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i, x, n) for(int i = x; i >= n; i--)
#define rrep(i, n) RREP(i,n,0)
#define pb push_back
#pragma GCC optimize ("-O3")

using namespace std;

#define DUMPOUT cout
#define dump(...) if(DEB) DUMPOUT<<"  "<<#__VA_ARGS__<<" :["<<__LINE__<<":"<<__FUNCTION__<<"]"<<endl<<"    "; if(DEB) dump_func(__VA_ARGS__)
template<typename T1,typename T2>ostream& operator << (ostream& os, pair<T1,T2> p){cout << "(" << p.first << ", " << p.second << ")"; return os;}
template<typename T>ostream& operator << (ostream& os, vector<T>& vec) { os << "{"; for (int i = 0; i<vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : ", "); os << "}"; return os; }
template<typename T>ostream& operator << (ostream& os, set<T>& st){cout << "{"; for(auto itr = st.begin(); itr != st.end(); itr++) cout << *itr << (next(itr)!=st.end() ? ", " : ""); cout << "}"; return os;}
template<typename T1,typename T2>ostream& operator << (ostream& os, map<T1,T2> mp){cout << "{"; for(auto itr = mp.begin(); itr != mp.end(); itr++) cout << "(" << (itr->first) << ", " << (itr->second) << ")" << (next(itr)!=mp.end() ? "," : ""); cout << "}"; return os; }

void dump_func(){DUMPOUT << endl;}
template <class Head, class... Tail>void dump_func(Head&& head, Tail&&... tail){ DUMPOUT << head; if (sizeof...(Tail) == 0) { DUMPOUT << " "; } else { DUMPOUT << ", "; } dump_func(std::move(tail)...);}
template<class T> inline bool chmax(T& a,T const& b){if(a>=b) return false; a=b; return true;}
template<class T> inline bool chmin(T& a,T const& b){if(a<=b) return false; a=b; return true;}

using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vp = vector<Pl>;
using vvp = vector<vp>;

const int INF = 1<<29;
const long long LINF=1LL<<59;

ll solve1(ll d,ll a1,ll b1){
  // O(d)
  ll res = 0;
  for(ll i = 0; a1 * i <= d; i++){
    ll temp = (d-a1*i) + i*b1;
    chmax(res,temp);
  }
  return res;
}

ll solve2(ll d,ll a1,ll a2,ll b1,ll b2){
  // O(d)
  ll res = 0;
  for(ll i = 0; a1 * i <= d; i++){
    ll temp = b1*i;
    ll rest = d-a1*i;
    temp += (rest%a2 + rest/a2*b2);
    chmax(res,temp);
  }
  return res;
}

ll solve3(ll d,ll a1,ll a2,ll a3,ll b1,ll b2,ll b3){
  // o(d^2)
  ll res = 0;
  for(ll i = 0; a1 * i <= d; i++){
    ll rest = d - a1 * i;
    for(ll j = 0; a2 * j <= rest; j++ ){
      ll r2 = rest - a2 * j;
      ll temp = i * b1 + j * b2 + r2 % a3 + (r2/a3)*b3;
      chmax(res,temp);
    }
  }
  return res;
}

void solve(long long N, long long g_A, long long s_A, long long b_A, long long g_B, long long s_B, long long b_B){
  vector<Pl> x(3);
  x[0] = Pl(g_A,g_B);
  x[1] = Pl(s_A,s_B);
  x[2] = Pl(b_A,b_B);
  sort(ALL(x),[&](Pl l,Pl r){return l.second-l.first > r.second-r.first;});
  int cnt = 0;
  rep(i,3) if(x[i].second-x[i].first>0) cnt++;
  ll ans;
  if(cnt==3){
    // g_A < g_B, s_A < s_B, b_A < b_B
    ans = solve3(N,g_A,s_A,b_A,g_B,s_B,b_B);
  }else if(cnt==2){
    ans = solve2(N,x[0].first,x[1].first,x[0].second,x[1].second);
    ans = solve1(ans,x[2].second,x[2].first);
  }else if(cnt==1){
    ans = solve1(N,x[0].first,x[0].second);
    ans = solve2(ans,x[1].second,x[2].second,x[1].first,x[2].first);
  }else{
    ans = solve3(N,g_B,s_B,b_B,g_A,s_A,b_A);
  }
  cout << ans << endl;
}

int main(){
  long long N;
  scanf("%lld",&N);
  long long g_A;
  scanf("%lld",&g_A);
  long long s_A;
  scanf("%lld",&s_A);
  long long b_A;
  scanf("%lld",&b_A);
  long long g_B;
  scanf("%lld",&g_B);
  long long s_B;
  scanf("%lld",&s_B);
  long long b_B;
  scanf("%lld",&b_B);
  solve(N, g_A, s_A, b_A, g_B, s_B, b_B);
  return 0;
}


Measured memory_bytes: 256