#include "bits/stdc++.h"

using namespace std;

void solve(void)
{
	int n, m = 5000*5000;
	cin >> n;
	vector<int> xs(3), ys(3);
	for (int&x : xs) cin >> x;
	for (int&y : ys) cin >> y;
	vector<long> dp(n+1), dp2(m+1);
	for (int i = 1; i <= n; i++)
	{
		dp[i] = i;
		for (int j = 0; j < 3; j++)
		{
			if (i - xs[j] >= 0)
			{
				dp[i] = max(dp[i], dp[i-xs[j]] + ys[j]);
			}
		}
	}
	for (int i = 1; i <= m; i++)
	{
		dp2[i] = i;
		for (int j = 0; j < 3; j++)
		{
			if (i - ys[j] >= 0)
			{
				dp2[i] = max(dp2[i], dp2[i-ys[j]] + xs[j]);
			}
		}
	}
	cout << dp2[dp.back()] << endl;
}

int main()
{
	solve();
	//cout << "yui(*-v・)yui" << endl;
	return 0;
}
