import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; 
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	long n = read.to!long;
	long ga = read.to!long;
	long sa = read.to!long;
	long ba = read.to!long;
	long gb = read.to!long;
	long sb = read.to!long;
	long bb = read.to!long;
	
	// A → B
	long best1 = -1;
	for(long g = 0; g * ga <= n; g += 1){
		for(long s = 0; g * ga + s * sa <= n; s += 1){
			long b;
			if(ba < bb) b = (n - g * ga - s * sa) / ba;
			else b = 0;
			
			long d = n - g * ga - s * sa - b * ba;
			
			long x = g * gb + s * sb + b * bb + d;
			if(best1 < x) log("g:", g, " s:", s, " b:", b, " d:", d, "best was:", best1, " now:", x);
			best1 = max(best1, x);
		}
	}
	
	n = best1;
	log("ok. n:", n);
	
	long z;
	if(gb < sb){
		z = gb, gb = sb, sb = z;
		z = ga, ga = sa, sa = z;
	}
	if(sb < bb){
		z = sb, sb = bb, bb = z;
		z = sa, sa = ba, ba = z;
	}
	if(gb < sb){
		z = gb, gb = sb, sb = z;
		z = ga, ga = sa, sa = z;
	}
	
	// B → A
	long best2 = -1;
	for(long g = 0; g * gb <= n; g += 1){
		for(long s = max(0, (n - g * gb - lcm(sb, bb) - 1) / sb); g * gb + s * sb <= n; s += 1){
			long b;
			if(bb < ba) b = (n - g * gb - s * sb) / bb;
			else b = 0;
			
			long d = n - g * gb - s * sb - b * bb;
			
			long x = g * ga + s * sa + b * ba + d;
			if(best2 < x) log("g:", g, " s:", s, " b:", b, " d:", d, "best was:", best2, " now:", x);
			best2 = max(best2, x);
		}
	}
	
	best2.writeln;
}

/*
Bでは一旦全部どんぐりに替えるとして、
A→BとB→Aでそれぞれ考えればよい。


*/


long gcd(long a, long b){
	if(a % b == 0) return b;
	if(a < b) return gcd(b, a);
	else return gcd(b, a % b);
}
long lcm(long a, long b){
	return (a / gcd(a, b) * b);
}
