import java.io.*; import java.util.*; public class Main { static ArrayList units = new ArrayList<>(); static int[][] dp; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); for (int i = 0; i < n; i++) { Unit x = new Unit(sc.next()); if (x.isSorted) { units.add(x); } } Collections.sort(units); dp = new int[units.size()][26]; for (int[] arr : dp) { Arrays.fill(arr, -1); } System.out.println(dfw(units.size() - 1, 25)); } static int dfw(int idx, int c) { if (idx < 0) { return 0; } if (dp[idx][c] < 0) { dp[idx][c] = dfw(idx - 1, c); if (units.get(idx).right <= c) { dp[idx][c] = Math.max(dp[idx][c], dfw(idx - 1, units.get(idx).left) + units.get(idx).length); } } return dp[idx][c]; } static class Unit implements Comparable { int left; int right; int length; boolean isSorted; public Unit(String s) { this.left = s.charAt(0) - 'a'; this.right = s.charAt(s.length() - 1) - 'a'; char[] tmp = s.toCharArray(); Arrays.sort(tmp); isSorted = s.equals(new String(tmp)); length = s.length(); } public int compareTo(Unit another) { return right - another.right; } } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); 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(); } }