#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <queue>

using namespace std;

class pairClass { // ペアのソート
public:
	pairClass() { key = 0, value = 0; }
	float key;
	long diff, value;
};
int qsort_int_compare_ascend(const void* a, const void* b) {//keyを見比べる
	float x = ((pairClass*)a)->key - ((pairClass*)b)->key;
	return (x > 0) ? 1 : ((x < 0) ? -1 : 0);
}
int qsort_int_compare_descend(const void* a, const void* b) {//keyを見比べる
	float x = ((pairClass*)b)->key - ((pairClass*)a)->key;
	return (x > 0) ? 1 : ((x < 0) ? -1 : 0);
}
void ascendOrder(int size, pairClass* array) {//昇順
	qsort(array, size, sizeof(pairClass), qsort_int_compare_ascend);
}
void descendOrder(int size, pairClass* array) {//降順
	qsort(array, size, sizeof(pairClass), qsort_int_compare_descend);
}

int main() {
	long N;
	long gA, sA, bA;
	long gB, sB, bB;

	cin >> N >> gA >> sA >> bA >> gB >> sB >> bB;

	// 差分
	long gD = gB - gA;
	long sD = sB - sA;
	long bD = bB - bA;

	{// AB
		pairClass* p = new pairClass[3];
		p[0].key = (float)gD / (float)gA; p[0].diff = gD; p[0].value = gA;
		p[1].key = (float)sD / (float)sA; p[1].diff = sD; p[1].value = sA;
		p[2].key = (float)bD / (float)bA; p[2].diff = bD; p[2].value = bA;

		descendOrder(3, p);

		if (p[0].key > 0) {
			long c = N / p[0].value;
			if (p[1].key > 0) {
				long M = 0;
				for (; c >= 0; c--) {
					long N1 = N - p[0].value * c;
					long d = N1 / p[1].value;

					if (p[2].key > 0) {
						long M2 = 0;

						for (; d >= 0; d--) {
							long N2 = N1 - p[1].value * d;
							long e = N2 / p[2].value;

							if (N + c * p[0].diff + d * p[1].diff + e * p[2].diff > M2) {
								M2 = N + c * p[0].diff + d * p[1].diff + e * p[2].diff;
							}
							else if ((N1 / p[1].value - d) * p[1].diff > ((float)N2 * p[2].key)) { break; }
						}
						if (M2 > M) {
							M = M2;
						}
						else if ((N / p[0].value - c) * p[0].diff > ((float)N1 * p[1].key)) { break; }
					}
					else {
						if (N + c * p[0].diff + d * p[1].diff > M) {
							M = N + c * p[0].diff + d * p[1].diff;
						}
						else if ((N / p[0].value - c) * p[0].diff > ((float)N1 * p[1].key)) { break; }
					}
				}
				N = M;
			}
			else {
				N += p[0].diff * c;
			}
		}
	}

	{// BA
		pairClass* p = new pairClass[3];
		p[0].key = -(float)gD / (float)gB; p[0].diff = -gD; p[0].value = gB;
		p[1].key = -(float)sD / (float)sB; p[1].diff = -sD; p[1].value = sB;
		p[2].key = -(float)bD / (float)bB; p[2].diff = -bD; p[2].value = bB;

		descendOrder(3, p);

		if (p[0].key > 0) {
			long c = N / p[0].value;
			if (p[1].key > 0) {
				long M = 0;
				for (; c >= 0; c--) {
					long N1 = N - p[0].value * c;
					long d = N1 / p[1].value;

					if (p[2].key > 0) {
						long M2 = 0;

						for (; d >= 0; d--) {
							long N2 = N1 - p[1].value * d;
							long e = N2 / p[2].value;

							if (N + c * p[0].diff + d * p[1].diff + e * p[2].diff > M2) {
								M2 = N + c * p[0].diff + d * p[1].diff + e * p[2].diff;
							}
							else if ((N1 / p[1].value - d) * p[1].diff > ((float)N2 * p[2].key)) { break; }
						}
						if (M2 > M) {
							M = M2;
						}
						else if ((N / p[0].value - c) * p[0].diff > ((float)N1 * p[1].key)) { break; }
					}
					else {
						if (N + c * p[0].diff + d * p[1].diff > M) {
							M = N + c * p[0].diff + d * p[1].diff;
						}
						else if ((N / p[0].value - c) * p[0].diff > ((float)N1 * p[1].key)) { break; }
					}
				}
				N = M;
			}
			else {
				N += p[0].diff * c;
			}
		}
	}

	cout << N << endl;
	return 0;
}


Measured memory_bytes: 256