import java.io.*; import java.util.*; public class Main { static ArrayList words = new ArrayList<>(); static int[][] dp; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); while (n-- > 0) { Word tmp = new Word(sc.next()); if (tmp.isGood()) { words.add(tmp); } } Collections.sort(words); dp = new int[words.size()][26]; for (int[] arr : dp) { Arrays.fill(arr, -1); } System.out.println(dfw(words.size() - 1, 25)); } static int dfw(int idx, int c) { if (idx < 0) { return 0; } if (dp[idx][c] < 0) { Word current = words.get(idx); if (current.right <= c) { dp[idx][c] = Math.max(dfw(idx - 1, c), dfw(idx - 1, current.left) + current.length()); } else { dp[idx][c] = dfw(idx - 1, c); } } return dp[idx][c]; } static class Word implements Comparable { String name; int left; int right; boolean good; public Word(String name) { this.name = name; char prev = 'a'; good = true; for (char c : name.toCharArray()) { if (prev > c) { good = false; break; } prev = c; } left = name.charAt(0) - 'a'; right = prev - 'a'; } public int compareTo(Word another) { if (right == another.right) { return left - another.left; } else { return right - another.right; } } public int length() { return name.length(); } public boolean isGood() { return good; } } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }