N = gets.to_i a = ?a.ord S = [] def good?(s) c = s[0] s.each_char.all? { |d| x = c <= d c = d x } end N.times do s = gets.chomp next unless good?(s) S << [s[0].ord - a, s[-1].ord - a, s.size] end S.sort! ans = 0 A = Array.new(26, 0) S.each do |(f, l, s)| x = A[f] y = x + s (l ... 26).each do |i| A[i] = y if y > A[i] end end puts A.max