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

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

    long solve(int i, long n, long g, long s, long b) {
        auto XS = i == 1 ? BS : AS;
        n += g * XS[0] + s * XS[1] + b * XS[2];

        if (i == 2) return n;

        long r;
        auto ps = [0,1,2,3];
        do {
            auto nn = n;
            int[3] gsb;
            foreach (p; ps) {
                if (p == 3) break;
                gsb[p] = nn / XS[p];
                nn %= XS[p];
            }
            r = max(r, solve(i+1, nn, gsb[0], gsb[1], gsb[2]));
        } while (nextPermutation(ps));
        return r;
    }

    writeln(solve(0, N, 0, 0, 0));
}