#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e3+5;

int read()
{
	int s=0;
	char c=getchar(),lc='+';
	while (c<'0'||'9'<c) lc=c,c=getchar();
	while ('0'<=c&&c<='9') s=s*10+c-'0',c=getchar();
	return lc=='-'?-s:s;
}
void write(int x)
{
	if (x<0)
	{
		putchar('-');
		x=-x;
	}
	if (x<10) putchar(x+'0');
	else
	{
		write(x/10);
		putchar(x%10+'0');
	}
}
void print(int x,char c='\n')
{
	write(x);
	putchar(c);
}
int dp[N*N],a[5][5];

signed main()
{
	int n=read();
	for (int i=0;i<=1;i++)
	for (int j=1;j<=3;j++) a[i][j]=read();
	for (int p=0;p<=1;p++)
	{
		int m=0;
		for (int i=0;i<=n;i++) dp[i]=0;
		for (int i=1;i<=3;i++)
		for (int j=a[p][i];j<=n;j++)
		dp[j]=max(dp[j],dp[j-a[p][i]]+a[p^1][i]-a[p][i]);
		for (int i=0;i<=n;i++) m=max(m,n+dp[i]);
		n=m;
	}
	print(n);

	return 0;
}