#include using namespace::std; const int inf = 100000000 + 10; const int N = 200000 + 5; const int E = 26; int n; int L[N]; int R[N]; int len[N]; int maxi[E]; bool valid[N]; bool check(string s){ for(int i = 1; i < s.size(); i++){ if(s[i - 1] > s[i]) return false; } return true; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for(int i = 0; i < n; i++){ string s; cin >> s; len[i] = s.size(); L[i] = s[0] - 'a'; R[i] = s[len[i] - 1] - 'a'; valid[i] = check(s); } vector p(n); iota(p.begin(), p.end(), 0); sort(p.begin(), p.end(), [&] (int i, int j){ return R[i] < R[j]; }); for(int i = 0; i < E; i++) maxi[i] = -inf; if(valid[p[0]]) maxi[R[p[0]]] = len[p[0]]; for(int at = 1; at < n; at++){ int i = p[at]; if(valid[i]){ int best = 0; for(int j = 0; j <= L[i]; j++){ best = max(best, maxi[j]); } best += len[i]; maxi[R[i]] = max(maxi[R[i]], best); } } cout << max(0, *max_element(maxi, maxi + E)) << endl; return 0; }