//#include<MrUnknown.h>
//#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f_(i,a,b) for (int i=a;i>=b;i--)
#define f(i,a,b) for (int i=a;i<=b;i++)
const ll mod=1000000007;

ll n,a[5],b[5],f[25000005];
int main() {
//	freopen("","r",stdin);
//	freopen("","w",stdout);
	cin>>n;
	f(j,1,3) cin>>a[j];
	f(j,1,3) cin>>b[j];
	f(i,1,n) {
		f[i]=0;
		f(j,1,3) if (i-a[j]>=0) f[i]=max(f[i],f[i-a[j]]+b[j]-a[j]);
//		cout<<f[i]<<" ";
	}
//	cout<<"\n";
	n+=f[n];
	f(i,1,n) {
		f[i]=0;
		f(j,1,3) if (i-b[j]>=0) f[i]=max(f[i],f[i-b[j]]+a[j]-b[j]);
//		cout<<f[i]<<" ";
	}
//	cout<<"\n";
	n+=f[n];
	cout<<n;
	return 0;
}