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 r;
    auto as = [0,1,2,3];
    do {
        auto h = [0, 0, 0, N];
        foreach (i; as) {
            if (i == 3) break;
            h[i] = h[3] / AS[i];
            h[3] = h[3] % AS[i];
        }

        auto bs = [0,1,2,3];
        do {
            auto hh = [0, 0, 0, h[0] * BS[0] + h[1] * BS[1] + h[2] * BS[2] + h[3]];
            foreach (i; bs) {
                if (i == 3) break;
                hh[i] += hh[3] / BS[i];
                hh[3] = hh[3] % BS[i];
            }
            foreach (i; 0..3) {
                hh[3] += hh[i] / AS[i];
            }

            r = max(r, hh[3]);
        } while (nextPermutation(bs));
    } while (nextPermutation(as));

    writeln(r);
}