#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;

const long double PI = acos(-1);
constexpr long double EPS = 1e-15;
constexpr int inf = 2e9;
constexpr ll INF = 2e18;
constexpr ll MOD = 1e9+7;
constexpr ll MOD1 = 998244353;
typedef pair<ll,ll> P;

//#define all(v) (v).begin(), (v).end()
#define rep(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) rep(i,0,n)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
//#define mp make_pair

void solve() {
  ll N;
  ll n;
  cin >> n;
  N = n;
  ll ga,gb,sa,sb,ba,bb;
  cin >> ga >> sa >> ba;
  cin >> gb >> sb >> bb;
  
  int bigger = 0;
  bigger += (gb > ga);
  bigger += (sb > sa);
  bigger += (bb > ba);
  if (bigger == 1) {
    ll A,B;
    ll C[2],D[2];
    if (gb > ga) {
      A = ga;
      B = gb;
      C[0] = sb;
      D[0] = sa;
      C[1] = bb;
      D[1] = ba;
    } else if (sb > sa) {
      A = sa;
      B = sb;
      C[0] = gb;
      D[0] = ga;
      C[1] = bb;
      D[1] = ba;
    } else {
      A = ba;
      B = bb;
      C[0] = gb;
      D[0] = ga;
      C[1] = sb;
      D[1] = sa;
    }
    ll num = n / A;
    n -= num * A;
    n += num * B;
    ll ans = n;
    for (ll i = 0; i <= n; i++) {
      ll tmp = n;
      ll j = n - i;
      ll ni = i / C[0];
      ll nj = j / C[1];
      tmp -= ni * C[0];
      tmp -= nj * C[1];
      tmp += ni * D[0];
      tmp += nj * D[1];
      ans = max(ans,tmp);
    }
    n = ans;
  } else if (bigger == 2) {
    ll A[2], B[2];
    ll C,D;
    if (gb <= ga) {
      A[0] = sa;
      B[0] = sb;
      A[1] = ba;
      B[1] = bb;
      C = gb;
      D = ga;
    } else if (sb <= sa) {
      A[0] = ga;
      B[0] = gb;
      A[1] = ba;
      B[1] = bb;
      C = sb;
      D = sa;
    } else if (bb <= ba) {
      A[0] = ga;
      B[0] = gb;
      A[1] = sa;
      B[1] = sb;
      C = bb;
      D = ba;
    }
    ll ans = n;
    for (ll i = 0; i <= n; i++) {
      ll j = n - i;
      ll tmp = n;
      ll ni = i / A[0];
      ll nj = j / A[1];
      tmp -= ni * A[0];
      tmp -= nj * A[1];
      tmp += ni * B[0];
      tmp += nj * B[1];
      ans = max(ans, tmp);
    }
    n = ans;
    ll k = n / C;
    n -= k * C;
    n += k * D;
  } else if (bigger == 3) {
    ll ans = n;
    for (ll i = 0; i <= n; i++) {
      for (ll j = 0; j + i <= n; j++) {
        ll k = n - i - j;
        ll tmp = n;
        ll ni = i / ga;
        ll nj = j / sa;
        ll nk = k / ba;
        tmp -= ni * ga;
        tmp -= nj * sa;
        tmp -= nk * ba;
        tmp += ni * gb;
        tmp += nj * sb;
        tmp += nk * bb;
        ans = max(ans, tmp);
      }
    }
    n = ans;
  } else if (bigger == 0) {
    ll ans = n;
    for (ll i = 0; i <= n; i++) {
      for (ll j = 0; j + i <= n; j++) {
        ll k = n - i - j;
        ll tmp = n;
        ll ni = i / gb;
        ll nj = j / sb;
        ll nk = k / bb;
        tmp -= ni * gb;
        tmp -= nj * sb;
        tmp -= nk * bb;
        tmp += ni * ga;
        tmp += nj * sa;
        tmp += nk * ba;
        ans = max(ans, tmp);
      }
    }
    n = ans;
  }
  cout << max(N,n) << endl;
}

int main(int argc, char *argv[]){

  /*Regular*/
  int case_num = 1;
  if (argc > 1 && stoi(argv[1])) cin >> case_num;
  REP(case_no, case_num) {
    cerr << endl << "case " << case_no + 1 << endl;
    solve();
  }

  // /*ICPC*/
  // while (cin >> t >> d >> l, t) {
  //   solve();
  // }

  //solve();
  return 0;
}

Measured memory_bytes: 256