#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 1e9 + 7;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;


template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}


int main() {
  int64 N, A[3], B[3];
  cin >> N;
  for(int i = 0; i < 3; i++) cin >> A[i];
  for(int i = 0; i < 3; i++) cin >> B[i];


  int64 ret = 0;
  for(int i = 0; i <= N; i++) {
    for(int j = 0; j <= N; j++) {
      int64 rest = N - i * A[0] - j * A[1];
      if(rest < 0) continue;

      int64 uku = 0;
      uku += i * B[0];
      uku += j * B[1];
      if(A[2] < B[2]) {
        int64 get = rest / A[2];
        rest -= get * A[2];
        uku += get * B[2];
      }
      uku += rest;
      chmax(ret, uku);
    }
  }

  N = ret;

  for(int i = 0; i <= N; i++) {
    int64 rest = N - i * B[0];
    if(rest < 0) continue;

    int64 uku = 0;
    uku += i * A[0];

    chmax(ret, uku + rest);

    int64 j_from = 0, j_to = rest / B[1] + 1;
    auto unimodalFunc = [&](int64 j) {
      int64 uku2 = uku;
      int64 rest2 = rest - j * B[1];
      uku2 += j * A[1];
      if(B[2] < A[2]) {
        int64 get = rest2 / B[2];
        rest2 -= get * B[2];
        uku2 += get * A[2];
      }
      uku2 += rest2;
      chmax(ret, uku2);
      return uku2;
    };
    unimodalFunc(j_from);
    unimodalFunc(j_to - 1);

    while(j_to - j_from > 1) {
      int64 j = (j_from + j_to) / 2;
      (unimodalFunc(j) - unimodalFunc(j - 1) > 0 ? j_from : j_to) = j;
    }
    int64 lim = rest / B[1];
    for(int k = max(0LL, j_from - 10); k <= min(lim, j_from + 10); k++) {
      unimodalFunc(k);
    }
  }


  swap(A, B);
  for(int i = 0; i <= min(5000LL, N); i++) {
    for(int j = 0; j <= min(5000LL, N); j++) {
      int64 rest = N - i * A[0] - j * A[1];
      if(rest < 0) continue;

      int64 uku = 0;
      uku += i * B[0];
      uku += j * B[1];
      if(A[2] < B[2]) {
        int64 get = rest / A[2];
        rest -= get * A[2];
        uku += get * B[2];
      }
      uku += rest;
      chmax(ret, uku);
    }
  }


  cout << ret << endl;
}




Measured memory_bytes: 256