#include <bits/stdc++.h>

#define forn(i,n) for (int i = 0; i < int(n); i++)

using namespace std;

typedef long long ll;
typedef vector<ll> vl;

template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}

int n,ga,sa,ba,gb,sb,bb;

ll calc(int n, int g1, int g2, int s1, int s2, int b1, int b2) {
  vl dp(n+1);
  forn(i,dp.size()) dp[i] = i;

  for (int i = 0; i <= n; i++) {
    if (i+g1 <= n) chmax(dp[i+g1],dp[i]+g2);
    if (i+s1 <= n) chmax(dp[i+s1],dp[i]+s2);
    if (i+b1 <= n) chmax(dp[i+b1],dp[i]+b2);
  }

  return dp[n];
}

int main()
{
  cin >> n >> ga >> sa >> ba >> gb >> sb >> bb;

  ll m = calc(n,ga,gb,sa,sb,ba,bb);

  cout << calc((int)m,gb,ga,sb,sa,bb,ba) << endl;

  return 0;
}
