//AtCoder 5091
//https://www.luogu.com.cn/problem/AT5091
#include<bits/stdc++.h>
using namespace std;
long long f[4][25000010],v[5],c[5];
long long knapsack(int C){
	memset(f,0,sizeof(f));
	for(int i=1;i<=3;i++){
		for(int j=0;j<=C;j++){
			if(j>=v[i])f[i][j] = max(f[i-1][j],f[i][j-v[i]]+c[i]);
			else f[i][j] = f[i-1][j];
		}
	}
	return f[3][C];
}
int main()
{
	int n;cin>>n;
	int ga,sa,ba,gb,sb,bb;
	cin>>ga>>sa>>ba>>gb>>sb>>bb;
	long long ans = n;
	v[1] = ga;c[1] = gb-ga;
	v[2] = sa;c[2] = sb-sa;
	v[3] = ba;c[3] = bb-ba;
	ans += knapsack(ans);
	
	v[1] = gb;c[1] = -c[1];
	v[2] = sb;c[2] = -c[2];
	v[3] = bb;c[3] = -c[3];
	ans += knapsack(ans);
	cout << ans << endl;
}