#include <bits/stdc++.h>
using namespace std;
const int maxn=5000*5000+233;
long long f[maxn];
void dfs(int w,int c)
{
	if(c<=0) return;
	for(int i=0;i<=5000*5000-w;i++)
	{
		f[i+w]=max(f[i+w],f[i]+c);
	}
}
int main()
{
	int n,ga,gb,sa,sb,ba,bb;
	cin>>n>>ga>>sa>>ba>>gb>>sb>>bb;
	memset(f,0,sizeof(f));
	dfs(ga,gb-ga);dfs(sa,sb-sa);dfs(ba,bb-ba);
	n=n+f[n];
	memset(f,0,sizeof(f));
	dfs(gb,ga-gb);dfs(sb,sa-sb);dfs(bb,ba-bb);
	cout<<n+f[n]<<endl;
	return 0;
}
