using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static string[] SList(long n) => Enumerable.Repeat(0, (int)n).Select(_ => ReadLine()).ToArray(); public static void Main() { Solve(); } static void Solve() { var n = NN; var s = SList(n); var dic = new List[26]; for (var i = 0; i < dic.Length; ++i) dic[i] = new List(); var same = new int[26]; foreach (var si in s) { var flg = true; for (var i = 0; i + 1 < si.Length; ++i) { if (si[i] > si[i + 1]) { flg = false; break; } } if (flg) { if (si[0] == si[^1]) same[si[0] - 'a'] += si.Length; else dic[si[0] - 'a'].Add(si); } } var dp = new int[27]; for (var i = 0; i + 1 < dp.Length; ++i) { dp[i] += same[i]; dp[i + 1] = Math.Max(dp[i + 1], dp[i]); foreach (var si in dic[i]) { dp[si[^1] - 'a'] = Math.Max(dp[si[^1] - 'a'], dp[i] + si.Length); } } WriteLine(dp[^1]); } }