#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 = lcm(a[0], lcm(a[1], a[2])); ll k3 = k * 3; vll dp(k3 + 110); //dp[うどん札の枚数] = 割引額の最大 rep(i, k3) { rep(j, 3) { dp[i+a[j]] = max(dp[i+a[j]], dp[i] + b[j]); } } ll mlt = max(0LL, (n / k) - 2); mlt %= mod; ll dpk = dp[k] % mod; ll ans = (n >= k3 ? dpk * mlt + dp[n%k + 2*k] : dp[n]) % mod; //cout << dpk << ' ' << mlt << ' ' << dp[n%k + 2*k] << endl; cout << ans << endl; }