// includes {{{
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<random>
#include<cassert>
#include<bitset>
#include<cstdlib>
// #include<deque>
// #include<multiset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;

// #undef DEBUG
// #define DEBUG
// DEBUG {{{
#include <array>
#include <deque>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <tuple>
#include <valarray>
#include <vector>
template < int n, class... T >
typename std::enable_if< (n >= sizeof...(T)) >::type __output_tuple(
    std::ostream &, std::tuple< T... > const &) {}
template < int n, class... T >
typename std::enable_if< (n < sizeof...(T)) >::type __output_tuple(
    std::ostream &os, std::tuple< T... > const &t) {
  os << (n == 0 ? "" : ", ") << std::get< n >(t);
  __output_tuple< n + 1 >(os, t);
}
template < class... T >
std::ostream &operator<<(std::ostream &os, std::tuple< T... > const &t) {
  os << "(";
  __output_tuple< 0 >(os, t);
  os << ")";
  return os;
}
template < class T, class U >
std::ostream &operator<<(std::ostream &os, std::pair< T, U > const &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template < class T >
std::ostream &operator<<(std::ostream &os, const std::stack< T > &a) {
  os << "{";
  for(auto tmp = a; tmp.size(); tmp.pop())
    os << (a.size() == tmp.size() ? "" : ", ") << tmp.top();
  os << "}";
  return os;
}
template < class T, class Container, class Compare >
std::ostream &operator<<(std::ostream &os,
    std::priority_queue< T, Container, Compare > a) {
  os << "{ (top) ";
  while(a.size()) os << a.top() << (a.size() == 1 ? "" : ", "), a.pop();
  os << " }";
  return os;
}
template < class T, class Container >
std::ostream &operator<<(std::ostream &os, std::queue< T, Container > a) {
  os << "{ ";
  while(a.size()) os << a.front() << (a.size() == 1 ? "" : ", "), a.pop();
  os << " }";
  return os;
}
#ifdef DEBUG
#if !defined(DEBUG_OUT)
// #define DEBUG_OUT std::cerr
#endif
#define dump(...)                                                                \
  [&]() {                                                                        \
    auto __debug_tap = std::make_tuple(__VA_ARGS__);                             \
    DEBUG_OUT << "[" << __LINE__ << "] " << #__VA_ARGS__ << " = " << __debug_tap \
    << std::endl;                                                      \
  }()
template < class T >
inline void dump2D(T &d, size_t sizey, size_t sizex) {
  for(size_t i = 0; i < sizey; i++) {
    DEBUG_OUT << "\t";
    for(size_t j = 0; j < sizex; j++)
      DEBUG_OUT << d[i][j] << (j + 1 == sizex ? "" : "\t");
    DEBUG_OUT << std::endl;
  }
}
template < class T >
inline void dump1D(T &d, size_t sizey) {
  for(size_t i = 0; i < sizey; i++) {
    DEBUG_OUT << d[i] << (i + 1 == sizey ? "" : " ");
  }
  DEBUG_OUT << std::endl;
}
template <
class T, class = typename std::iterator_traits< decltype(begin(T())) >::value_type,
      class = typename std::enable_if< !std::is_same< T, std::string >::value >::type >
      std::ostream &operator<<(std::ostream &os, const T &a) {
        os << "{";
        for(auto ite = begin(a); ite != end(a); ++ite)
          os << (ite == begin(a) ? "" : ", ") << *ite;
        os << "}";
        return os;
      }
#else
#define dump(...) ((void) 42)
#define dump2D(...) ((void) 42)
#define dump1D(...) ((void) 42)
template <
class T, class = typename std::iterator_traits< decltype(begin(T())) >::value_type,
      class = typename std::enable_if< !std::is_same< T, std::string >::value >::type >
      std::ostream &operator<<(std::ostream &os, const T &a) {
        for(auto ite = begin(a); ite != end(a); ++ite)
          os << (ite == begin(a) ? "" : " ") << *ite;
        return os;
      }
#endif
// }}}

// x : cost
// y : value
// z : limit
// knapsack

// O(min{NW, N \sum {xz}}) < O(N^2 XZ) time
// O(W) space
// knapsack1(<ull>, <ll>, <uint>, uint) {{{
#include <cassert>
#include <deque>
#include <vector>
vector< ll > knapsack1(const vector< unsigned long long >& x, const vector< ll >& y,
    const vector< unsigned >& z, unsigned W) {
  using ull = unsigned long long;
  size_t n = x.size();
  assert(n == y.size());
  assert(n == z.size());
  if(n == 0) return vector< ll >();
  ull w_max = 0;
  for(size_t i = 0; i < n && w_max < W && (x[i] < W || z[i] == 0); i++) {
    w_max += x[i] * z[i];
    if(i == n - 1 && w_max < W) W = w_max;
  }
  vector< ll > dp(W + 1);
  for(size_t i = 0; i < n; i++)
    if(z[i]) {
      if(x[i]) {
        vector< ll > dp0(W + 1);
        swap(dp, dp0);
        for(size_t r = 0; r < x[i] && r <= W; r++) {
          // k := z[i]
          // a[i][j] := dp[i][j * x[i] + r]
          // dp[i + 1][(j + k) * x[i] + r] ( = a[i + 1][j + k] )
          //  = max{a[i][j] + k * y[i], a[i][j + 1] + (k - 1) * y[i], ...}
          //  = max{a[i][j] + -j * y[i], a[i][j + 1] + -(j + 1) * y[i], ...}
          //      + (j + k) * y[i]
          size_t u = (W - r) / x[i];
          deque< pair< ll, size_t > > deq;
          for(size_t j = 0; j <= u; j++) {
            ll nval = dp0[j * x[i] + r] - j * y[i];
            while(deq.size() && deq.back().first < nval) deq.pop_back();
            deq.emplace_back(nval, j);
            dp[j * x[i] + r] = deq.front().first + j * y[i];
            if(deq.front().second + z[i] == j) deq.pop_front();
          }
        }
      } else {
        for(auto& el : dp) el += y[i] * z[i];
      }
    }
  return dp;
}
// }}}

// check whether \sum {yz} won't overflow
// O(N \sum {yz}) < O(N^2 YZ) time
// knapsack2(<ll>, <uint>, <uint>, ull) {{{
vector< ll > knapsack2(const vector< ll >& x, const vector< unsigned >& y,
    const vector< unsigned >& z, unsigned long long W) {
  using ull = unsigned long long;
  size_t n = x.size();
  assert(n == y.size());
  assert(n == z.size());
  if(n == 0) return vector< ll >();
  ull value_max = 0;
  for(size_t i = 0; i < n; i++) value_max += y[i] * z[i];
  vector< ll > dp(value_max + 1, W + 1);
  dp[0] = 0;
  for(size_t i = 0; i < n; i++)
    if(y[i] && z[i]) {
      vector< ll > dp0(value_max + 1, W + 1);
      swap(dp, dp0);
      for(size_t r = 0; r < y[i]; r++) {
        // k := z[i]
        // a[i][j] := dp[i][j * y[i] + r]
        // dp[i + 1][(j + k) * y[i] + r] ( = a[i + 1][j + k] )
        //  = min{a[i][j] + k * x[i], a[i][j + 1] + (k - 1) * x[i], ...}
        //  = min{a[i][j] + -j * x[i], a[i][j + 1] + -(j + 1) * x[i], ...}
        //      + (j + k) * x[i]
        size_t u = (value_max - r) / y[i];
        deque< pair< ll, size_t > > deq;
        for(size_t j = 0; j <= u; j++) {
          if((ull) dp0[j * y[i] + r] <= W) {
            ll nval = dp0[j * y[i] + r] - (ll) j * x[i];
            while(deq.size() && deq.back().first > nval) deq.pop_back();
            deq.emplace_back(nval, j);
          }
          if(deq.size()) dp[j * y[i] + r] = deq.front().first + j * x[i];
          if(deq.size() && deq.front().second + z[i] == j) deq.pop_front();
        }
      }
    }
  return dp;
}

int knapsack2_a(const vector< ll >& x, const vector< unsigned >& y,
    const vector< unsigned >& z, unsigned long long W) {
  auto dp = knapsack2(x,y,z,W);
  for(int i = dp.size() - 1; i >= 0; i--) if(dp[i] <= W) return i;
}
// }}}

// check whether \sum {yz} won't overflow
// O(N^2 Y^2)
// knapsack3(<ull>, <uint>, <ull>, ull) {{{
#include <algorithm>
#include <numeric>
unsigned long long knapsack3(const vector< unsigned long long >& x,
    const vector< unsigned >& y,
    const vector< unsigned long long >& z,
    unsigned long long W) {
  using ull = unsigned long long;
  size_t n = x.size();
  assert(n == y.size());
  assert(n == z.size());
  if(n == 0) return 0;
  size_t Y = 0;
  for(size_t i = 0; i < n; i++)
    if(Y < y[i]) Y = y[i];
  if(Y == 0) return 0;
  vector< size_t > ord(n);
  iota(begin(ord), end(ord), 0);
  sort(rbegin(ord), rend(ord),
      [&](size_t a, size_t b) { return (double) y[a] * x[b] < (double) y[b] * x[a]; });

  vector< ll > nx(n);
  vector< unsigned > ny(n);
  vector< unsigned > nz(n);
  vector< ull > zz(n);

  for(size_t i = 0; i < n; i++) {
    nx[i] = x[ord[i]];
    ny[i] = y[ord[i]];

    nz[i] = min(z[ord[i]], (ull) Y - 1);
    zz[i] = z[ord[i]] - nz[i];
  }

  auto sub = knapsack2(nx, ny, nz, W);

  vector< ull > xz_sum(n);
  vector< ull > yz_sum(n);
  for(size_t i = 0; i < n; i++) xz_sum[i] = nx[i] * zz[i];
  for(size_t i = 0; i < n; i++) yz_sum[i] = ny[i] * zz[i];
  for(size_t i = 1; i < n; i++) xz_sum[i] = min(xz_sum[i - 1] + xz_sum[i], W + 1);
  for(size_t i = 1; i < n; i++) yz_sum[i] = yz_sum[i - 1] + yz_sum[i];

  ull res = 0;
  // O(N Y^2 log N)
  for(unsigned v = 0; v < sub.size(); v++) {
    ull use = sub[v];
    if(use > W) continue;
    ull rest = W - use;
    size_t xp = upper_bound(begin(xz_sum), end(xz_sum), rest) - begin(xz_sum);
    ull val = v;
    if(xp >= 1) val += yz_sum[xp - 1], rest -= xz_sum[xp - 1];
    assert(xp == n || nx[xp] != 0); // explicit
    if(xp < n) val += rest / nx[xp] * ny[xp];
    if(res < val) res = val;
  }

  return res;
}
// }}}



ll n;
int g[2][3];


int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  cin >> n;
  for(int i = 0; i < 2; i++) for(int j = 0; j < 3; j++) cin >> g[i][j];
  using ull = unsigned long long;
  using uint = unsigned;

  dump(1);

  {
    vector<ll> x(1,1);
    vector<uint> y(1,1);
    vector<uint> z(1,n);
    for(int i = 0; i < 3; i++) {
      if(g[0][i] < g[1][i]) {
        x.push_back(g[0][i]);
        y.push_back(g[1][i]);
        z.push_back(n / g[0][i]);
      }
    }

    if(x.size() > 1) {
      ll ans = n;
      int k = 0;
      for(ll i = 0; i <= n / g[k][0]; i++) for(ll j = 0; j <= n / g[k][1]; j++) {
        ll G = i * g[k][0];
        ll S = j * g[k][1];
        if(g[k][0] >= g[!k][0] && i) continue;
        if(g[k][1] >= g[!k][1] && j) continue;
        ll rest = n - (G + S);
        if(rest < 0) continue;
        // dump(i, j, rest);
        ll res = i * g[!k][0] + j * g[!k][1];
        if(g[k][2] < g[!k][2]) {
          ll b = rest / g[k][2];
          res += b * g[!k][2];
          rest -= b * g[k][2];
        }
        res += rest;
        ans = max(res, ans);
      }
      n = ans;
    }
  }

  dump(2, n);

  {
    vector<ull> x(1,1);
    vector<uint> y(1,1);
    vector<ull> z(1,n);
    for(int i = 0; i < 3; i++) {
      if(g[1][i] < g[0][i]) {
        // if(g[1][i] == 1) {
        //   y[0] = max<ll>(y[0], g[0][i]);
        // } else {
          x.push_back(g[1][i]);
          y.push_back(g[0][i]);
          z.push_back(n / g[1][i]);
        // }
      }
    }

    // dump(knapsack3(x, y, z, n));

    // if(x.size() > 1) {
      if(n <= 5000) {
        ll ans = n;
        int k = 1;
        for(ll i = 0; i <= n / g[k][0]; i++) for(ll j = 0; j <= n / g[k][1]; j++) {
          ll G = i * g[k][0];
          ll S = j * g[k][1];
          if(g[k][0] >= g[!k][0] && i) continue;
          if(g[k][1] >= g[!k][1] && j) continue;
          ll rest = n - (G + S);
          if(rest < 0) continue;
          // dump(i, j, rest);
          ll res = i * g[!k][0] + j * g[!k][1];
          if(g[k][2] < g[!k][2]) {
            ll b = rest / g[k][2];
            res += b * g[!k][2];
            rest -= b * g[k][2];
          }
          res += rest;
          ans = max(res, ans);
        }
        n = ans;
      } else {
        if(x.size() == 1) {
        } else if(x.size() == 2) {
          ll u = n / x[1];
          n = u * y[1] + n - u * x[1];
        } else if(x.size() == 3) {

          ll ans = n;
          {
            ll hi = n / x[1];
            auto f = [&](ll n, ll u) {
              ll rest = n - u * x[1];
              n = u * y[1];
              ll buy = rest / x[2];
              n += buy * y[2];
              rest -= buy * x[2];
              n += rest;
              return n;
            };
            for(ll i = max<ll>(hi - 5000 * 5000, 0); i <= hi; i++) {
              ans = max(ans, f(n, i));
            }
          }
          {
            ll hi = n / x[2];
            auto f = [&](ll n, ll u) {
              ll rest = n - u * x[2];
              n = u * y[2];
              ll buy = rest / x[1];
              n += buy * y[1];
              rest -= buy * x[1];
              n += rest;
              return n;
            };
            for(ll i = max<ll>(hi - 5000 * 5000, 0); i <= hi; i++) {
              ans = max(ans, f(n, i));
            }
          }
          n = ans;
          // while(abs(hi - lo) > 1) {
          //   ll u = (lo + hi) / 2;
          //   if(f(n, u) < f(n, u + 1)) lo = u; else hi = u;
          // }
          // dump(lo);
          // for(int i = 0; i < 20; i++) dump(f(n, 2000 + i));
          // dump(f(n, lo), f(n, lo + 1));
          // n = max(f(n, lo), f(n, lo + 1));
        } else assert(0);
      }
    // }
  }

  dump(3);

  cout << n << endl;
  return 0;
}


Measured memory_bytes: 256