#include #include #define elif else if #define ll long long #define vll vector #define vec vector #define embk emplace_back #define rep(i, n) for (ll i = 0; i < n; i++) #define rep3(i, n, k) for (ll i = k; i < n; i++) #define all(a) a.begin(), a.end() #define YNeos(bool) (bool ? "Yes" : "No") using namespace std; using namespace atcoder; const ll INF = 1LL << 60; const ll mod = 998244353; const double pi = acos(-1); int main() { ll n; cin >> n; vll a(3), b(3); rep(i, 3) cin >> a[i] >> b[i]; ll k = 3 * lcm(a[0], lcm(a[1], a[2])); vll dp(k+110); //dp[うどん札の枚数] = 割引額の最大 rep(i, k) { rep(j, 3) { dp[i+a[j]] = max(dp[i+a[j]], dp[i] + b[j]); } } ll mlt = (n / k) % mod; ll dpk = dp[k] % mod; cout << (dpk * mlt + dp[n%k]) % mod << endl; }