#include<bits/stdc++.h>
using namespace std;
int n;
long long value[2][3];
long long dfs(long long capital, long long i, vector<pair<long long,long long>> &product){
  if(product.size() == 0) return capital;
  else if(i == product.size()-1) return capital/product[i].first*product[i].second + capital%product[i].first;
  long long ret=0;
  for(int j=0;j*product[i].first<=capital;j++) ret=max(ret,dfs(capital-j*product[i].first,i+1,product) + j*product[i].second);
  return ret;
}
int main(){
  cin >> n;
  for(int i=0;i<2;i++) for(int j=0;j<3;j++) cin >> value[i][j];
  vector<pair<long long,long long>> forth,back;
  for(int j=0;j<3;j++){
    if(value[0][j]<value[1][j]) forth.push_back({value[0][j],value[1][j]});
    if(value[0][j]>value[1][j]) back.push_back({value[1][j],value[0][j]});                                             
  }
  cout << dfs(dfs(n,0,forth),0,back) << endl;
}
