#include using namespace std; typedef long long ll; int main(){ ios::sync_with_stdio(false); cin.tie(0); ll V; cin >> V; int n = 3; std::vector v(3),c(3); ll w = 0,m = 1; ll mn = 1e9; for(int i = 0;i < 3;i ++){ cin >> v[i] >> c[i]; if(w * v[i] < m * c[i])w = c[i],m = v[i]; mn = min(v[i],mn); } std::vector f(m); for(int i = 0; i < n; i++){ for(int j = 0, lim = __gcd(v[i], m); j < lim; j++){ for(int t = j, _ = 0; _ < 2; _ += t == j) { int q = (t + v[i]) % m; f[q] = max(f[q], f[t] + c[i] - ((t + v[i]) / m) * w), t = q; } } } if(V < mn) cout << 0 << "\n"; else cout << f[V % m] + (V / m * w) << "\n"; return 0; }