puts (1..read_line.to_i).map { read_line.chars.map(&.-('a')) }.select { |s| s.each_cons(2).all? { |(i, j)| i <= j } }.sort_by { |s| {s.last, s.first} }.each_with_object([0] * 26) { |s, dp| dp[s.last] = {dp[s.last], dp[..s.first].max + s.size}.max }.max