N = gets.to_s.to_i a = 'a'.ord S = [] of {Int32, Int32, Int32} def good?(s) c = s[0] s.each_char.all? { |d| x = c <= d c = d x } end N.times do s = gets.to_s.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 |t| f, l, s = t[0], t[1], t[2] x = A[f] y = x + s (l ... 26).each do |i| A[i] = y if y > A[i] end end puts A.max