N, k = gets.split.map(&:to_i) S=gets.chomp total = 1 count = 1 (0...N).each{|i| if count == 0 count +=1 total +=1 #puts "i=#{i}, total=#{total}" end count -=1 count += S[i].to_i if i>=k puts total exit end } #puts "1:total=#{total}, count=#{count}, k=#{k}, N=#{N}" k -= N su = S.split('').map(&:to_i).inject(&:+) #puts "su=#{su}" if su > S.length count += (k/N) * (su - S.length ) else total += (k/N) * (S.length - su) end k%=N #uts "2:total=#{total}, count=#{count}, k=#{k}, N=#{N}" (0...k).each{|i| if count == 0 count +=1 total +=1 #puts "i=#{i}, total=#{total}" end count -=1 count += S[i].to_i if i>=k puts total exit end } puts total