INF = 10 ** 6 c,n = 2.times.map { gets.to_i} a = gets.split.map(&:to_i) dp = Array.new(c+1,INF) dp[0] = 0 (1..c).each do |i| n.times do |j| if a[j] <= i && dp[i] > dp[i - a[j]] + 1 dp[i] = dp[i - a[j]] + 1 end end end puts dp[c] > c ? -1 : dp[c]