import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

long[] AS, BS;

void main()
{
    auto N = readln.chomp.to!long;
    AS = readln.split.to!(long[]);
    BS = readln.split.to!(long[]);

    long[][3] DP;
    foreach (ref dp; DP) dp.length = N+1;
    long a(int i, long j) {
        if (i == 3) return N-j;
        if (DP[i][j]) return DP[i][j];
        long r1, r2;
        r1 = a(i+1, j);
        if (j+AS[i] <= N) {
            r2 = a(i, j+AS[i]) + BS[i];
        }
        return DP[i][j] = max(r1, r2);
    }
    auto M = a(0, 0);
    foreach (ref dp; DP) {
        dp[] = 0;
        dp.length = M+1;
    }
    long b(int i, long j) {
        if (i == 3) return M-j;
        if (DP[i][j]) return DP[i][j];
        long r1, r2;
        r1 = b(i+1, j);
        if (j+BS[i] <= M) {
            r2 = b(i, j+BS[i]) + AS[i];
        }
        return DP[i][j] = max(r1, r2);
    }

    writeln(b(0, 0));
}