#include <bits/stdc++.h>
#define rep(i, n) for (lli i = 0; i < (n); i++)
#define rrep(i, n) for (lli i = (n)-1; i >= 0; i--)
using namespace std;
using lli = long long int;
lli dp1[5000 * 5100] = {};
lli dp2[5000 * 5100] = {};

int c[3][2] = {};
lli calc1(int u){
    if(u<0)return 0;
    lli &ret = dp1[u];
    // Bでu円持ってるときの最適
    if(ret>0)return ret;
    ret = u;
    rep(i,3){
        if(u-c[i][1]>=0)
        ret = max(calc1(u-c[i][1]) + c[i][0], ret);
    }
    return ret;
}
lli calc2(int u){
    if(u<0)return 0;
    lli &ret = dp2[u];
    // Aでu円持ってるときの最適
    if(ret>0)return ret;
    ret = u;
    rep(i,3){
        if(u-c[i][0]>=0)
        ret = max(calc2(u-c[i][0]) + c[i][1], ret);
    }
    return ret;
}
int main(){
    lli n;cin >> n;
    rep(j,2)rep(i,3)cin >> c[i][j];
    lli x = calc2(n);
    cout << max(calc1(x),n) << endl;
}
