#include<bits/stdc++.h>
using namespace std ; 
int main()
{
	int g[3],b[3],s[3],cnt=0,n; 
	cin >> n ; 
	for ( int i = 1 ; i <= 3 ; i++ ) cin >> g[1] >> s[1] >> b[1] ; 
	for ( int i = 1 ; i <= 3 ; i++ ) cin >> g[2] >> s[2] >> b[2] ; 
	int p1 = 1 , p2 = 1 , p3 = 1 ; 
	p1 ^= ( g[1] < g[2] ) ;
	p2 ^= ( s[1] < s[2] ) ; 
	p3 ^= ( b[1] < b[2] ) ; 
	if ( p1 && p2 && p3 ) {
		for ( int i = 0 ; i <= n/g[1] ; i++ ) {
			for ( int j = 0 ; j <= (n-i*g[1])/s[1] ; j++ ) {
				int k = (n-i*g[1]-j*s[1])/b[1] ; 
				cnt = max(cnt,i*(g[1]-g[2])+j*(s[1]-s[2])+k*(b[1]-b[2])) ;
			}
		}
		n += cnt ; 
	}
	else if ( p1 && p2 )
	{
		for ( int i = 0 ; i <= n/g[1] ; i++ ) {
			int k = (n-i*g[1])/s[1] ; 
			cnt = max(cnt,i*(g[1]-g[2])+k*(s[1]-s[2])) ; 
		}
		n += cnt + (n+cnt)/b[2]*(b[2]-b[1]) ; 
	}		
 	else if ( p2 && p3 ) {
 		for ( int i = 0 ; i <= n/s[1] ; i++ ) {
			int k = (n-i*s[1])/b[1] ; 
			cnt = max(cnt,i*(s[1]-s[2])+k*(b[1]-b[2])) ; 
		}
		n += cnt + (n+cnt)/g[2]*(g[2]-g[1]) ;
 	}
	else if ( p1 && p3 ) {
		for ( int i = 0 ; i <= n/g[1] ; i++ ) {
			int k = (n-i*g[1])/b[1] ; 
			cnt = max(cnt,i*(g[1]-g[2])+k*(b[1]-b[2])) ; 
		}
		n += cnt + (n+cnt)/s[2]*(s[2]-s[1]) ;
	}
	else if ( p1 ) {
		for ( int i = 0 ; i <= n/s[2] ; i++ ) {
			int k = (n-i*s[2])/b[2] ; 
			cnt = max(cnt,i*(s[2]-s[1])+k*(b[2]-b[1])) ; 
		}
		n += cnt + (n+cnt)/s[1]*(s[1]-s[2]) ;
	}
	else if ( p2 ) {
		for ( int i = 0 ; i <= n/g[2] ; i++ ) {
			int k = (n-i*g[2])/b[2] ; 
			cnt = max(cnt,i*(g[2]-g[1])+k*(b[2]-b[1])) ; 
		}
		n += cnt + (n+cnt)/s[1]*(s[1]-s[2]) ;
	}
	else {
		for ( int i = 0 ; i <= n/g[2] ; i++ ) {
			int k = (n-i*g[2])/s[2] ; 
			cnt = max(cnt,i*(g[2]-g[1])+k*(s[2]-s[1])) ; 
		}
		n += cnt + (n+cnt)/b[1]*(b[1]-b[2]) ;
	}
	cout << n ; 
}