// author: Saman Mahdanian
#include <bits/stdc++.h>

using namespace std;
const int N = 5555;

long long AB[N], BA[N * N], n;
long long Ga, Gb, Sa, Sb, Ba, Bb;

int main() {
	cin >> n;
	cin >> Ga >> Sa >> Ba;
	cin >> Gb >> Sb >> Bb;

	for (int i = 1; i <= n; i++) {
		AB[i] = i;
		if (i >= Ga)
			AB[i] = max(AB[i], AB[i - Ga] + Gb);
		if (i >= Sa)
			AB[i] = max(AB[i], AB[i - Sa] + Sb);
		if (i >= Ba)
			AB[i] = max(AB[i], AB[i - Ba] + Bb);
	}

	int profit = AB[n];
	for (int i = 1; i <= profit; i++) {
		BA[i] = i;
		if (i >= Gb)
			BA[i] = max(BA[i], BA[i - Gb] + Ga);
		if (i >= Sb)
			BA[i] = max(BA[i], BA[i - Sb] + Sa);
		if (i >= Bb)
			BA[i] = max(BA[i], BA[i - Bb] + Ba);
	}

	cout << BA[profit] << endl;	
}

// SamMHD :: July3-2019 :: Another Day in home...