#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vt vector
#define pb push_back
#define pob pop_back
#define vi vt<int>
#define vvi vt<vi>
#define vii vt<ii>
#define vvii vt<vii>
typedef long long ll;
#define For(i,a,b) for(int i = a; i < b; i++)
#define foR(i,a,b) for(int i = a; i <= b; i++)
#define roF(i,a,b) for(int i = a - 1; i >= b; i--)
#define Rof(i,a,b) for(int i = a; i >= b; i--)

int dp[5001];
int gt[25000001];
int a[3], b[3];
signed main (){
	/*
	freopen(".inp","r",stdin);
	freopen(".out","w",stdout);
	*/
	int n;
	cin >> n;
	cin >> a[0] >> a[1] >> a[2] >> b[0] >> b[1] >> b[2];
	foR(i,1,n){
		For(j,0,3){
			if(i - a[j] >= 0){
				dp[i] = max(dp[i],dp[i-a[j]]+b[j]-a[j]);
			}
		}
	}
	n = n+dp[n];
	foR(i,1,n){
		For(j,0,3){
			if(i - b[j] >= 0){
				gt[i] = max(gt[i],gt[i-b[j]]+a[j]-b[j]);
			}
		}
	}
	n = n+gt[n];
	cout << n;
}

