#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include #include #include #include #include #include #define FOR(i,a,b) for (ll i=(a);i<(ll)(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() #define SUM(v) accumulate(ALL(v),0ll) using ll = long long; const ll INF=1e24; const ll eps=1; const double pi=3.14159265359; const ll mod=1e9+7; using I32 = int32_t; using I64 = int64_t; using namespace std; //Union Find テンプレ struct UnionFind { vector par , siz; UnionFind(int n) : par(n,-1) , siz(n,1) { } int root(int x) { if(par[x] == -1) return x; else return par[x] = root(par[x]); } bool issame(int x, int y) { return root(x) == root(y); } bool unite(int x, int y) { x = root(x) ; y = root(y); if(x == y) return false; if(siz[x] < siz[y]) swap(x,y); par[y] = x; siz[x] += siz[y]; return true; } int size(int x) { return siz[root(x)]; } }; typedef pair P; int func(int a,int b) { if(b==0)return a; return func(b,a%b); } int main() { vector> moji(29,vector(29,0)); ll n; cin>>n; REP(i,n) { string s; cin>>s; bool ok=true; REP(j,s.size()-1) { if(s[j]>s[j+1]) { ok=false; break; } } if(ok==true) { int x=s[0]-'a'; int y=s[s.size()-1]-'a'; int z=s.size(); moji[x][y]=max(z,moji[x][y]); } } vector dp(29,0); REP(i,29) { for(int j=0;j<=i;j++) { dp[i]=max(dp[i],moji[j][i]+dp[j]); } } cout<