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