class Calc0150 def initialize(args) args = args.map { |l| l.chomp.split(/\s+/) } @t = args.shift.first.to_i @ss = args.map(&:first) end def calc(s) dg = (0..(s.size - 11)).map { |k| distance(s[k, 4], "good") } dp = (0..(s.size - 7)).map { |k| distance(s[k, 7], "problem") } (0..s.size - 11).map { |k| ((k + 4)..s.size - 7).map { |m| dg[k] + dp[m] }.min }.min end def distance(s1, s2) s1.split(//).zip(s2.split(//)).count { |c1, c2| c1 != c2 } end def run @ss.map { |s| calc(s) } end end puts Calc0150.new(STDIN.readlines).run if __FILE__ == $0