import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Obj[] arr = new Obj[n]; for (int i = 0; i < n; i++) { Obj o = new Obj(); o.s = sc.next().toCharArray(); arr[i] = o; } sc.close(); Arrays.sort(arr, (o1, o2) -> o1.s[o1.s.length - 1] - o2.s[o2.s.length - 1]); int[] dp = new int[26]; label: for (int i = 0; i < n; i++) { char[] t = arr[i].s; for (int j = 1; j < t.length; j++) { if (t[j - 1] > t[j]) { continue label; } } int d0 = t[0] - 'a'; int d1 = t[t.length - 1] - 'a'; for (int j = d0; j >= 0; j--) { dp[d1] = Math.max(dp[d1], dp[j] + t.length); } } int ans = 0; for (int i = 0; i < dp.length; i++) { ans = Math.max(ans, dp[i]); } System.out.println(ans); } static class Obj { char[] s; } }