#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#include <time.h>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector< vector<ll> > matrix;
#define REP(i, x, y) for(ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for(ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for(ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for(ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    ll ga, sa, ba, gb, sb, bb;
    cin >> ga >> sa >> ba >> gb >> sb >> bb;
    ll g = false, s = false, b = false;
    if (ga > gb) {
        g = true;
    }
    if (sa > sb) {
        s = true;
    }
    if (ba > bb) {
        b = true;
    }
    ll ans = N;
    if ((g && s && b) || (!g && !s && !b)) {
        REP(i, 0, 5010) {
            REP(j, 0, 5010) {
                if (i * ga + j * sa > N) {
                    continue;
                }
                ll rem = N - (i * ga + j * sa);
                ll br = rem / ba;
                ll tmp = rem;
                tmp += i * gb;
                tmp += j * sb;
                if (bb > ba) {
                    tmp += br * (bb - ba);
                }
                ans = max(tmp, ans);
            }
        }
        REP(i, 0, 5010) {
            REP(j, 0, 5010) {
                if (i * gb + j * sb > N) {
                    continue;
                }
                ll rem = N - (i * gb + j * sb);
                ll br = rem / bb;
                ll tmp = rem;
                tmp += i * ga;
                tmp += j * sa;
                if (ba > bb) {
                    tmp += br * (ba - bb);
                }
                ans = max(tmp, ans);
            }
        }
    } else {
        tlglglg dif[3];
        dif[0] = tlglglg(ga - gb, ga, gb);
        dif[1] = tlglglg(sa - sb, sa, sb);
        dif[2] = tlglglg(ba - bb, ba, bb);
        sort(dif, dif + 3);
        ll cnt = 0;
        if (g) {
            cnt++;
        }
        if (s) {
            cnt++;
        }
        if (b) {
            cnt++;
        }
        if (cnt == 1) {
            REP(i, 0, 5010) {
                tlglglg t = dif[0];
                if (i * get<1>(t) > N) {
                    continue;
                }
                ll rem = N - i * get<1>(t);
                tlglglg tt = dif[1];
                ll two = rem / get<1>(tt);
                ll tmp = rem;
                tmp += i * get<2>(t);
                tmp += two * (get<2>(tt) - get<1>(tt));
                tlglglg ttt = dif[2];
                ll three = tmp / get<2>(ttt);
                tmp += three * get<0>(ttt);
                ans = max(tmp, ans);
            }
        } else {
            tlglglg t = dif[0];
            ll one = N / get<1>(t);
            ll tmpbase = N + one * (-get<0>(t));
            REP(i, 0, 5010 * 5010) {
                ll tmp = tmpbase;
                tlglglg tt = dif[1];
                if (i * get<2>(tt) > tmp) {
                    continue;
                }
                ll rem = tmp - i * get<2>(tt);
                tlglglg ttt = dif[2];
                ll three = rem / get<2>(ttt);
                tmp += i * get<0>(tt);
                tmp += three * get<0>(ttt);
                ans = max(tmp, ans);
            }
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 256