N = gets.to_i S = N.times.map { gets.chomp } T = S.select { |s| s.chars.each_cons(2).all? { |a, b| a <= b } }.sort dp = Array.new(26, 0) D = 'a'.ord T.each do |t| i = t[0].ord - D j = t[-1].ord - D v = t.size (0..i).to_a.reverse_each do |c| len = dp[c] + v if dp[j] < len dp[j] = len end end end puts dp.max