#include using namespace std; #define ll long long #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define Pr pair #define Tp tuple using Graph = vector>; ll mod = 998244353; int main() { ll N; cin >> N; ll dp[26]; rep(i,26) dp[i] = 0; rep(i,N){ string S; cin >> S; int n = S.size(); int num = 0; bool ok = true; int ini = S[0]-'a'; rep(j,n){ if(num>(S[j]-'a')) ok = false; if(num<(S[j]-'a')) num = S[j]-'a'; } if(ok){ rep(j,26){ if(j<=ini) dp[num] = max(dp[num],dp[j]+n); } } } ll ans = 0; rep(i,26) ans = max(ans,dp[i]); cout << ans << endl; }