mod = 998244353 N = gets.to_i A = Array.new(3) B = Array.new(3) (0..2).each do |i| A[i], B[i] = gets.split.map(&:to_i) end N2 = A[0]*A[1]*A[2] loop = [0,N.div(N2)-2].max ans = [B[0]*A[1]*A[2],A[0]*B[1]*A[2],A[0]*A[1]*B[2]].max * loop N -= loop * N2 dp = Array.new(N+1, 0) (0..N).each do |i| (0..2).each do |j| dp[i+A[j]] = [dp[i+A[j]], dp[i]+B[j]].max if i+A[j] <= N end end puts (ans+dp[N])%mod