#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int dp[30][30];
int main()
{
    int n;
    cin>>n;
    memset(dp,0,sizeof dp);
    vector<string> v(n);
    for(int i=0;i<n;i++)
    {
        cin>>v[i];
        int a=v[i][0]-'a';
        int b=v[i][v[i].size()-1]-'a';
        int len=v[i].size();
        for(int j=0;j<25;j++)
        {
            if(dp[j][a]!=0)
            {
                dp[j][b]=max(dp[j][b],(int)(dp[j][a]+v[i].size()));
            }
        }
        dp[a][b]=max(dp[a][b],len);
    }
    int ans=0;
    for(int i=0;i<25;i++)
    ans=max(ans,dp[i][i]);
    cout<<ans<<"\n";
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<b;i++)
#define S second
#define F first
#define all(v) v.begin(), v.end()
#define ppi pair<int, pair<int, int>>
using namespace std;

/*ll modular(ll a,ll n)
{
    if(n==0)
        return 1;

        if(n&1)
            return (a*modular((a*a)%hell, (n-1)/2))%hell;

        return modular((a*a)%hell, n/2);

}*/

int main()
{
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

    int n;
    cin>>n;

    int dp[26][26]={0};
    while(n--)
    {
        string a;
        cin>>a;

        int len=a.length();

        int l=a[0]-'a';
        int r=a[len-1]-'a';

        for(int i=0;i<26;i++)
        {
            if(dp[i][l] || i==l)
                dp[i][r]=dp[i][l]+len;
        }
    }

    int ans=0;
    for(int i=0;i<26;i++)
        ans=max(ans, dp[i][i]);

    cout<<ans;

            return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<b;i++)
#define S second
#define F first
#define all(v) v.begin(), v.end()
#define ppi pair<int, pair<int, int>>
using namespace std;

/*ll modular(ll a,ll n)
{
    if(n==0)
        return 1;

        if(n&1)
            return (a*modular((a*a)%hell, (n-1)/2))%hell;

        return modular((a*a)%hell, n/2);

}*/

int main()
{
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

    int n;
    cin>>n;

    int dp[26][26]={0};
    while(n--)
    {
        string a;
        cin>>a;

        int len=a.length();

        int l=a[0]-'a';
        int r=a[len-1]-'a';

        for(int i=0;i<26;i++)
        {
            if(dp[i][l] || i==l)
                dp[i][r]=max(dp[i][r],dp[i][l]+len);
        }
    }

    int ans=0;
    for(int i=0;i<26;i++)
        ans=max(ans, dp[i][i]);

    cout<<ans;

            return 0;
}


#include <bits/stdc++.h>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <functional>
using namespace std;

 #define tr(c, i) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define   ipr         	pair<int, int>
#define   pb            push_back
#define   ff            first
#define   ss            second
#define fill(a,b) memset(a, b, sizeof(a))
#define   fr(i, j, k)   for (int i = j; i < k; i++)
#define   rf(i, j, k)   for (int i = j; i >= k;i--)
#define   int           long long
#define   ll 			unsigned long long
#define   ld            long double
#define   all(x)        x.begin(), x.end()
#define   rall(x)       x.rbegin(),x.rend()
#define   mp            make_pair
#define   inf           INT_MAX
#define   read_file();	freopen("input.txt", "r", stdin);	freopen("output.txt", "w", stdout);
#define   IOS           std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

const int Maxn  =  200*1000 + 1;
const ld  pi   =  acos(-1);
const int mod =  1000000007;

template<class T> ostream& operator <<(ostream &out,vector<T> &A){for(auto x:A) out<<x<<" ";return out;}
template<class T> ostream& operator <<(ostream &out,unordered_set<T> &A){for(auto x:A) out<<x<<" ";return out;}
template <class T1,class T2>T1 powr(T1 a,T2 b){T1 res = 1;fr(i,1,b+1)res = res*a;return res;}
int roundUp(int numToRound, int multiple)
{
 if(multiple == 0)
 {
  return numToRound;
 }

 int roundDown = ( (int) (numToRound) / multiple) * multiple;
 int roundUp = roundDown + multiple;
 int roundCalc = roundUp;
 return (roundCalc);
}

int calc(int a, int b, int c) {
	return abs(a - b) + abs(a - c) + abs(b - c);
}

// gcd
int gcd(int a, int b){
    while(b){
        a=a%b;
        swap(a,b);
    }
    return a;
}
//lcm
int lcm(int a, int b){
    return (a*b)/gcd(a,b);
}

 bool isSubSequence(string str1, string str2 )
{ int m=str1.length(),n=str2.length();
   int j = 0;
   for (int i=0; i<n&&j<m; i++)
       if (str1[j] == str2[i])
         j++;

   return (j==m);
}

  void eraseAllSubStr(string &mainStr, string &toErase)
{
	size_t pos = std::string::npos;

	while ((pos  = mainStr.find(toErase) )!= std::string::npos)
	{

		mainStr.erase(pos, toErase.length());
        mainStr.insert(pos," ");
	}
}

int isSubstring(string s1, string s2)
{
    int M = s1.length();
    int N = s2.length();

    /* A loop to slide pat[] one by one */
    for (int i = 0; i <= N - M; i++) {
        int j;

        /* For current index i, check for pattern match */
        for (j = 0; j < M; j++)
            if (s2[i + j] != s1[j])
                break;

        if (j == M)
            return i;
    }

    return -1;
}

int countPrimitiveRoots(int p)
{
    int result = 1;
    for (int i = 2; i < p; i++)
        if (__gcd(i, p) == 1)
            result++;

    return result;
}

int cnt(int t){
    int c=0;
     while(t>0){
           t=t&(t-1);
           c++;
       }
       return c;
}

const int ma=10000007;

//Binary exponentiation
int binpow(int a,int b) {
    a=a%mod;

    int res = 1;
while (b > 0) {
        if (b & 1)res = (res * a)%mod;
        a = (a * a)%mod;
        b >>= 1;
}
return (res+mod)%mod;
}

#define MAX 1000000

vector<int> arr;

bool prime[MAX];

void SieveOfEratosthenes()
{

    memset(prime, true, sizeof(prime));

    for (int p = 2; p * p < MAX; p++) {

        if (prime[p] == true) {

            for (int i = p * 2; i < MAX; i += p)
                prime[i] = false;
        }
    }

}

 int bindiff(int a,int b){
     string as="",bs="";
     while(a){
         if(a%2==0)as+='1';
         else as+='0';
         a/=2;
     }
     reverse(as.begin(),as.end());
     while(b){
         if(b%2==0)bs+='1';
         else bs+='0';
         b/=2;
     }
     reverse(bs.begin(),bs.end());

     string xplusy=as+bs;
     string yplusx=bs+as;

     int x=0,y=0;

     int r=1,l=1;

     for(int i=xplusy.length()-1;i>=0;i--){
         if(xplusy[i]=='1')x+=(r);
         r*=2;
     }

     for(int i=yplusx.length()-1;i>=0;i--){
         if(yplusx[i]=='1')y+=(l);
         l*=2;
     }

     return abs(x-y);

 }

int dp[26][26];

int32_t main(){
 IOS;

  	int t;
	//cin>>t;
    t=1;
	while(t--){

        int n;
       cin >> n;
       fr(i, 0 , n) {
           string s;
           cin >> s;
           int len = s.length();
           int u = s[ 0 ]- 'a' ;
           int v = s[len- 1 ]- 'a' ;
           fr(i, 0 , 26 ) if (dp[i][u])
               dp[i][v] = max(dp[i][v] , dp[i][u] + len);
           if (len> dp[u][v]) dp[u][v] = len;
       }
       int ans = 0 ;
       fr(i, 0 , 26 ) ans = max(ans, dp[i][i]);
       cout << ans << endl;
       return  0 ;

}

}


#include <bits/stdc++.h>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <functional>
using namespace std;

 #define tr(c, i) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define   ipr         	pair<int, int>
#define   pb            push_back
#define   ff            first
#define   ss            second
#define fill(a,b) memset(a, b, sizeof(a))
#define   fr(i, j, k)   for (int i = j; i < k; i++)
#define   rf(i, j, k)   for (int i = j; i >= k;i--)
#define   int           long long
#define   ll 			unsigned long long
#define   ld            long double
#define   all(x)        x.begin(), x.end()
#define   rall(x)       x.rbegin(),x.rend()
#define   mp            make_pair
#define   inf           INT_MAX
#define   read_file();	freopen("input.txt", "r", stdin);	freopen("output.txt", "w", stdout);
#define   IOS           std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

const int Maxn  =  200*1000 + 1;
const ld  pi   =  acos(-1);
const int mod =  1000000007;

template<class T> ostream& operator <<(ostream &out,vector<T> &A){for(auto x:A) out<<x<<" ";return out;}
template<class T> ostream& operator <<(ostream &out,unordered_set<T> &A){for(auto x:A) out<<x<<" ";return out;}
template <class T1,class T2>T1 powr(T1 a,T2 b){T1 res = 1;fr(i,1,b+1)res = res*a;return res;}
int roundUp(int numToRound, int multiple)
{
 if(multiple == 0)
 {
  return numToRound;
 }

 int roundDown = ( (int) (numToRound) / multiple) * multiple;
 int roundUp = roundDown + multiple;
 int roundCalc = roundUp;
 return (roundCalc);
}

int calc(int a, int b, int c) {
	return abs(a - b) + abs(a - c) + abs(b - c);
}

// gcd
int gcd(int a, int b){
    while(b){
        a=a%b;
        swap(a,b);
    }
    return a;
}
//lcm
int lcm(int a, int b){
    return (a*b)/gcd(a,b);
}

 bool isSubSequence(string str1, string str2 )
{ int m=str1.length(),n=str2.length();
   int j = 0;
   for (int i=0; i<n&&j<m; i++)
       if (str1[j] == str2[i])
         j++;

   return (j==m);
}

  void eraseAllSubStr(string &mainStr, string &toErase)
{
	size_t pos = std::string::npos;

	while ((pos  = mainStr.find(toErase) )!= std::string::npos)
	{

		mainStr.erase(pos, toErase.length());
        mainStr.insert(pos," ");
	}
}

int isSubstring(string s1, string s2)
{
    int M = s1.length();
    int N = s2.length();

    /* A loop to slide pat[] one by one */
    for (int i = 0; i <= N - M; i++) {
        int j;

        /* For current index i, check for pattern match */
        for (j = 0; j < M; j++)
            if (s2[i + j] != s1[j])
                break;

        if (j == M)
            return i;
    }

    return -1;
}

int countPrimitiveRoots(int p)
{
    int result = 1;
    for (int i = 2; i < p; i++)
        if (__gcd(i, p) == 1)
            result++;

    return result;
}

int cnt(int t){
    int c=0;
     while(t>0){
           t=t&(t-1);
           c++;
       }
       return c;
}

const int ma=10000007;

//Binary exponentiation
int binpow(int a,int b) {
    a=a%mod;

    int res = 1;
while (b > 0) {
        if (b & 1)res = (res * a)%mod;
        a = (a * a)%mod;
        b >>= 1;
}
return (res+mod)%mod;
}

#define MAX 1000000

vector<int> arr;

bool prime[MAX];

void SieveOfEratosthenes()
{

    memset(prime, true, sizeof(prime));

    for (int p = 2; p * p < MAX; p++) {

        if (prime[p] == true) {

            for (int i = p * 2; i < MAX; i += p)
                prime[i] = false;
        }
    }

}

 int bindiff(int a,int b){
     string as="",bs="";
     while(a){
         if(a%2==0)as+='1';
         else as+='0';
         a/=2;
     }
     reverse(as.begin(),as.end());
     while(b){
         if(b%2==0)bs+='1';
         else bs+='0';
         b/=2;
     }
     reverse(bs.begin(),bs.end());

     string xplusy=as+bs;
     string yplusx=bs+as;

     int x=0,y=0;

     int r=1,l=1;

     for(int i=xplusy.length()-1;i>=0;i--){
         if(xplusy[i]=='1')x+=(r);
         r*=2;
     }

     for(int i=yplusx.length()-1;i>=0;i--){
         if(yplusx[i]=='1')y+=(l);
         l*=2;
     }

     return abs(x-y);

 }

int dp[26][26];

int32_t main(){
 IOS;

  	int t;
	//cin>>t;
    t=1;
	while(t--){

        int n;

    	cin>>n;

       fr(i,0,n){
           string s;
           cin>>s;
           int l=s.length();
           int u=s[0]-'a';
           int v=s[l-1]-'a';

           fr(i,0,26){
              if(dp[i][u]) dp[i][v]=max(dp[i][v],dp[i][u]+l);

               dp[u][v]=max(dp[u][v],l);
           }
       }

       int x=0;

       fr(i,0,26){
           x=max(x,dp[i][i]);
       }

       cout<<x<<endl;

}

}


#include <bits/stdc++.h>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <functional>
using namespace std;

 #define tr(c, i) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define   ipr         	pair<int, int>
#define   pb            push_back
#define   ff            first
#define   ss            second
#define fill(a,b) memset(a, b, sizeof(a))
#define   fr(i, j, k)   for (int i = j; i < k; i++)
#define   rf(i, j, k)   for (int i = j; i >= k;i--)
#define   int           long long
#define   ll 			unsigned long long
#define   ld            long double
#define   all(x)        x.begin(), x.end()
#define   rall(x)       x.rbegin(),x.rend()
#define   mp            make_pair
#define   inf           INT_MAX
#define   read_file();	freopen("input.txt", "r", stdin);	freopen("output.txt", "w", stdout);
#define   IOS           std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

const int Maxn  =  200*1000 + 1;
const ld  pi   =  acos(-1);
const int mod =  1000000007;

template<class T> ostream& operator <<(ostream &out,vector<T> &A){for(auto x:A) out<<x<<" ";return out;}
template<class T> ostream& operator <<(ostream &out,unordered_set<T> &A){for(auto x:A) out<<x<<" ";return out;}
template <class T1,class T2>T1 powr(T1 a,T2 b){T1 res = 1;fr(i,1,b+1)res = res*a;return res;}
int roundUp(int numToRound, int multiple)
{
 if(multiple == 0)
 {
  return numToRound;
 }

 int roundDown = ( (int) (numToRound) / multiple) * multiple;
 int roundUp = roundDown + multiple;
 int roundCalc = roundUp;
 return (roundCalc);
}

int calc(int a, int b, int c) {
	return abs(a - b) + abs(a - c) + abs(b - c);
}

// gcd
int gcd(int a, int b){
    while(b){
        a=a%b;
        swap(a,b);
    }
    return a;
}
//lcm
int lcm(int a, int b){
    return (a*b)/gcd(a,b);
}

 bool isSubSequence(string str1, string str2 )
{ int m=str1.length(),n=str2.length();
   int j = 0;
   for (int i=0; i<n&&j<m; i++)
       if (str1[j] == str2[i])
         j++;

   return (j==m);
}

  void eraseAllSubStr(string &mainStr, string &toErase)
{
	size_t pos = std::string::npos;

	while ((pos  = mainStr.find(toErase) )!= std::string::npos)
	{

		mainStr.erase(pos, toErase.length());
        mainStr.insert(pos," ");
	}
}

int isSubstring(string s1, string s2)
{
    int M = s1.length();
    int N = s2.length();

    /* A loop to slide pat[] one by one */
    for (int i = 0; i <= N - M; i++) {
        int j;

        /* For current index i, check for pattern match */
        for (j = 0; j < M; j++)
            if (s2[i + j] != s1[j])
                break;

        if (j == M)
            return i;
    }

    return -1;
}

int countPrimitiveRoots(int p)
{
    int result = 1;
    for (int i = 2; i < p; i++)
        if (__gcd(i, p) == 1)
            result++;

    return result;
}

int cnt(int t){
    int c=0;
     while(t>0){
           t=t&(t-1);
           c++;
       }
       return c;
}

const int ma=10000007;

//Binary exponentiation
int binpow(int a,int b) {
    a=a%mod;

    int res = 1;
while (b > 0) {
        if (b & 1)res = (res * a)%mod;
        a = (a * a)%mod;
        b >>= 1;
}
return (res+mod)%mod;
}

#define MAX 1000000

vector<int> arr;

bool prime[MAX];

void SieveOfEratosthenes()
{

    memset(prime, true, sizeof(prime));

    for (int p = 2; p * p < MAX; p++) {

        if (prime[p] == true) {

            for (int i = p * 2; i < MAX; i += p)
                prime[i] = false;
        }
    }

}

 int bindiff(int a,int b){
     string as="",bs="";
     while(a){
         if(a%2==0)as+='1';
         else as+='0';
         a/=2;
     }
     reverse(as.begin(),as.end());
     while(b){
         if(b%2==0)bs+='1';
         else bs+='0';
         b/=2;
     }
     reverse(bs.begin(),bs.end());

     string xplusy=as+bs;
     string yplusx=bs+as;

     int x=0,y=0;

     int r=1,l=1;

     for(int i=xplusy.length()-1;i>=0;i--){
         if(xplusy[i]=='1')x+=(r);
         r*=2;
     }

     for(int i=yplusx.length()-1;i>=0;i--){
         if(yplusx[i]=='1')y+=(l);
         l*=2;
     }

     return abs(x-y);

 }

int dp[26][26];

int32_t main(){
 IOS;

  	int t;
	//cin>>t;
    t=1;
	while(t--){

        int n;

    	cin>>n;

       fr(i,0,n){
           string s;
           cin>>s;
           int l=s.length();
           int u=s[0]-'a';
           int v=s[l-1]-'a';

           fr(i,0,26){
              if(dp[i][v]) dp[i][v]=max(dp[i][v],dp[i][u]+l);

               dp[u][v]=max(dp[u][v],l);
           }
       }

       int x=0;

       fr(i,0,26){
           x=max(x,dp[i][i]);
       }

       cout<<x<<endl;

}

}


#include<bits/stdc++.h>

using namespace std;

  void solve(){
    int n ;
    cin>>n;
    int dp[26][26];
    memset(dp, 0, sizeof dp);
    for(int i = 0; i < n; ++i){
      string temp ;
      cin>>temp;
      int m = temp.size();
      dp[temp[0] - 'a'][temp[m - 1] - 'a'] = max(m, dp[temp[0] - 'a'][temp[m - 1] - 'a']);
      if(dp[temp[m - 1] - 'a'][temp[0] - 'a'] != 0){
        dp[temp[m - 1] - 'a'][temp[m - 1] - 'a'] = max(dp[temp[m - 1] - 'a'][temp[0] - 'a'] + dp[temp[0] - 'a'][temp[m - 1]-'a' ], dp[temp[m - 1] - 'a'][temp[m - 1] - 'a'] );
      }
    }
    int an = 0;
    for(int i = 0; i < 26; ++i)an = max(an, dp[i][i]);
    cout<<an<<'\n';
  }

int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
    int tc = 1;
    //cin>>tc;
    while (tc--)
    {
      solve();
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

  void solve(){
    int n ;
    cin>>n;
    int dp[26][26];
    memset(dp, 0, sizeof dp);
    vector<int>dpp(26, 0);
    for(int i = 0; i < n; ++i){
      string temp ;
      cin>>temp;
      int m = temp.size();
      dp[temp[0] - 'a'][temp[m - 1] - 'a'] = max(m, dp[temp[0] - 'a'][temp[m - 1] - 'a']);
      if(dp[temp[m - 1] - 'a'][temp[0] - 'a'] != 0){
        dpp[temp[m - 1] - 'a']  = max(dp[temp[m - 1] - 'a'][temp[0] - 'a'] + dp[temp[0] - 'a'][temp[m - 1]-'a' ], dpp[temp[m - 1] - 'a'] );
      }
      dp[temp[m - 1] - 'a'][temp[m - 1] - 'a'] = max(dpp[temp[m - 1] - 'a'], dp[temp[m - 1] - 'a'][temp[m - 1] - 'a']);
    }
    int an = 0;
    for(int i = 0; i < 26; ++i)an = max(an, dpp[i]);
    cout<<an<<'\n';
  }

int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
    int tc = 1;
    //cin>>tc;
    while (tc--)
    {
      solve();
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

  void solve(){
    int n ;
    cin>>n;
    int dp[26][26];
    memset(dp, 0, sizeof dp);
    for(int i = 0; i < n; ++i){
      string temp ;
      cin>>temp;
      int m = temp.size();

      if(dp[temp[m - 1] - 'a'][temp[0] - 'a'] != 0){
        dp[temp[m - 1] - 'a'][temp[m - 1] - 'a']  = max(dp[temp[m - 1] - 'a'][temp[0] - 'a'] + m, dp[temp[m - 1] - 'a'][temp[m - 1] - 'a'] );
      }

       dp[temp[0] - 'a'][temp[m - 1] - 'a'] = max(m, dp[temp[0] - 'a'][temp[m - 1] - 'a']);
    }
    int an = 0;
    for(int i = 0; i < 26; ++i)an = max(an, dp[i][i]);
    cout<<an<<'\n';
  }

int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
    int tc = 1;
    //cin>>tc;
    while (tc--)
    {
      solve();
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

  void solve(){
    int n ;
    cin>>n;
    int dp[26][26];
    memset(dp, 0, sizeof dp);
    for(int i = 0; i < n; ++i){
      string temp ;
      cin>>temp;
      int m = temp.size();

      if(dp[temp[m - 1] - 'a'][temp[0] - 'a'] != 0){
        dp[temp[0] - 'a'][temp[m - 1] - 'a'] = max(m, dp[temp[0] - 'a'][temp[m - 1] - 'a']);
        dp[temp[m - 1] - 'a'][temp[m - 1] - 'a'] = max(dp[temp[m - 1] - 'a'][temp[0] - 'a'] + dp[temp[0] - 'a'][temp[m - 1]-'a' ], dp[temp[m - 1] - 'a'][temp[m - 1] - 'a'] );
      }
      else {
        dp[temp[0] - 'a'][temp[m - 1] - 'a'] = max(m, dp[temp[0] - 'a'][temp[m - 1] - 'a']);
      }
    }
    int an = 0;
    for(int i = 0; i < 26; ++i)an = max(an, dp[i][i]);
    cout<<an<<'\n';
  }

int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
    int tc = 1;
    //cin>>tc;
    while (tc--)
    {
      solve();
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

  void solve(){
    int n ;
    cin>>n;
    int dp[26][26];
    memset(dp, 0, sizeof dp);
    for(int i = 0; i < n; ++i){
      string temp ;
      cin>>temp;
      int m = temp.size();
      int r = temp[m - 1] - 'a';
      int l = temp[0] - 'a';
        for(int j = 0; j < 26; ++j){
        if(dp[j][l] != 0)  dp[j][r] = max(dp[j][r], dp[j][l] + m);
        }
        dp[l][r] = max(m, dp[l][r]);
    }
    int an = 0;
    for(int i = 0; i < 26; ++i)an = max(an, dp[i][i]);
    cout<<an<<'\n';
  }

int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
    int tc = 1;
    //cin>>tc;
    while (tc--)
    {
      solve();
    }

    return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define ll long long
using namespace std;
const ll mod =1e9+7;
const int nax=5e5+7;

bool vis[nax]={0};
ll n,max1=0;

vector<string> vs;
vector<vector<ll> > dp(26,vector<ll> (26,0));

int main(){

//	freopen("input.inp","r",stdin);

	cin>>n;
	vs.resize(n);
	for (ll i=0;i<n;++i) {
		cin>>vs[i];
		//dp[vs[i].front()-'a'][vs[i].back()-'a']=vs[i].size();
	}

	for (auto it: vs){
		for (int i=0;i<26;++i){

			if (dp[i][it.front()-'a']||i==it.front()-'a') dp[i][it.back()-'a']=max(dp[i][it.back()-'a'],dp[i][it.front()-'a']+(ll)it.size());

		}
	}

	ll max1=0;
	for (int i=0;i<26;++i) max1=max(dp[i][i],max1);

	cout<<max1<<"\n";
}


#include <bits/stdc++.h>
#define x first
#define y second
#define ll long long
using namespace std;
const ll mod =1e9+7;
const int nax=5e5+7;

bool vis[nax]={0};
ll n,max1=0;
vector<int> edge[26];
vector<string> vs;

void DFS(ll s,ll d){
	vis[s]=true;
	d+=vs[s].size();

	for (auto it: edge[vs[s].back()-'a']){
		if (!vis[it]){
			DFS(it,d);
		}

		else max1=max(max1,d);
	}
}

int main(){

//	freopen("input.inp","r",stdin);

	cin>>n;
	vs.resize(n);
	for (ll i=0;i<n;++i) {
		cin>>vs[i];
		edge[vs[i].front()-'a'].push_back(i);
	}

	for (ll i=0;i<n;++i){
		if (!vis[i]) DFS(i,0);
	}

	cout<<max1<<"\n";
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cmath>
#include <tuple>
#include <sstream>
#include <iomanip>
int64_t M = 1e9+7;

using namespace std;

int main() {
    //freopen("in.txt", "r", stdin);
    int t=1;
    //cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int dp[27][27]={0};
        int v[27] = {0};
        for(int i=0; i<n; i++)
        {
            string s;
            cin>>s;
            char a = *s.begin()-'a';
            char b = *s.rbegin()-'a';

            for(int j=0; j<26; j++)
                if (dp[j][a])
                    dp[j][b] = dp[j][a]+s.size();
            v[a]=1;
            dp[a][b] = max(dp[a][b], (int)s.size());

        }
        int ans = 0;
        for(int j=0; j<26; j++)
            ans = max(ans, dp[j][j]);
        cout << ans << endl;
    }

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cmath>
#include <tuple>
#include <sstream>
#include <iomanip>
int64_t M = 1e9+7;

using namespace std;

int main() {
    //freopen("in.txt", "r", stdin);
    int t=1;
    //cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int dp[27][27]={0};
        int v[27] = {0};
        for(int i=0; i<n; i++)
        {
            string s;
            cin>>s;
            char a = *s.begin()-'a';
            char b = *s.rbegin()-'a';
            v[a]=1;
            for(int j=0; j<26; j++)
                if (v[j])
                    dp[j][b] = dp[j][a]+s.size();
            dp[a][b] = max(dp[a][b], (int)s.size());

        }
        int ans = 0;
        for(int j=0; j<26; j++)
            ans = max(ans, dp[j][j]);
        cout << ans << endl;
    }

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cmath>
#include <tuple>
#include <sstream>
#include <iomanip>
int64_t M = 1e9+7;

using namespace std;

int main() {
    //freopen("in.txt", "r", stdin);
    int t=1;
    //cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int dp[27][27]={0};
        for(int i=0; i<n; i++)
        {
            string s;
            cin>>s;
            char a = *s.begin()-'a';
            char b = *s.rbegin()-'a';

            for(int j=0; j<26; j++)
                if (dp[j][a])
                    dp[j][b] = max(dp[j][b], dp[j][a]+(int)s.size());
            dp[a][b] = max(dp[a][b], (int)s.size());
        }
        int ans = 0;
        for(int j=0; j<26; j++)
            ans = max(ans, dp[j][j]);
        cout << ans << endl;
    }

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  string S[500000];

  cin >> N;
  for(int i = 0; i < N; i++) {
    cin >> S[i];
  }

  multiset< int > add[26][26];
  for(int i = 0; i < N; i++) {
    add[S[i].front() - 'a'][S[i].back() - 'a'].emplace(S[i].size());
  }

  int ret = 0;
  for(int i = 0; i < N; i++) {
    auto& p = add[S[i].front() - 'a'][S[i].back() - 'a'];
    p.erase(p.find(S[i].size()));
    auto& q = add[S[i].back() - 'a'][S[i].front() - 'a'];
    if(q.size()) ret = max< int >(ret, S[i].size() + *--q.end());
    if(S[i].front() == S[i].back()) ret = max< int >(ret, S[i].size());
  }

  cout << ret << endl;

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  string S[500000];

  cin >> N;
  for(int i = 0; i < N; i++) {
    cin >> S[i];
  }

  int dp[26][26];
  memset(dp, -1, sizeof(dp));

  for(int i = 0; i < N; i++) {
    auto s = S[i].front() - 'a';
    auto t = S[i].back() - 'a';
    const int sz = S[i].size();
    for(int j = 0; j < 26; j++) {
      if(~dp[j][s]) dp[j][t] = max(dp[j][t], dp[j][s] + sz);
    }
    dp[s][t] = max(dp[s][t], sz);
  }

  int ret = 0;
  for(int i = 0; i < 26;i++) {
    ret = max(ret, dp[i][i]);
  }
  cout << ret << endl;
}


#ifdef ONLINE_JUDGE

#include "bits/stdc++.h"
using namespace std;

#define ui unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double

#define all(v) v.begin(), v.end()

#define speed ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

const ld PI = 3.1415926535897932384626;
ll MOD = 1000000007;
const ld EPS = 1e-9;
const int N = 3e5 + 7;

inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ui setBits(ui v) { ui c; for(c = 0 ; v; ++c) v &= (v - 1); return c; }
inline bool isPowerOfTwo(ui v) { return (v && !(v & (v - 1))); }
ll power(ll base, ull exp, ll mod = MOD) {
	ll res = 1; base %= mod;
	while(exp) {
		if(exp & 1) res = (res * base)%mod;
		exp >>= 1; base = (base * base)%mod;
	}
	return res;
}
ll modInv(ll a, ll mod = MOD) {
	if(mod == 1) return 0;
	ll x = 1, y = 0, m = mod;
	while(a > 1) {
		ll q = a/m, t = m; m = a%m; a = t; t = y; y = x - q * y; x = t;
	}
	if(x < 0) x += mod;
	return x;
}
inline ui iabs(int x) { ui y = x; return (y > 0) ? y : -y; }
inline ll add(ll a, ll b, ll mod = MOD) { ll res = (a%mod + b%mod)%mod; return res; }
inline ll sub(ll a, ll b, ll mod = MOD) { ll res = (a%mod - b%mod + mod)%mod; return res; }
inline ll mult(ll a, ll b, ll mod = MOD) { ll res = (a%mod * b%mod)%mod; return res; }
inline ll div(ll a, ll b, ll mod = MOD) { ll res = (a%mod * modInv(b, mod))%mod; return res; }

#else

#include "custom.h"
using namespace std;

#endif

void openf() {
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
}

int main() {
	speed;
	openf();
	int n, ans = 0;
	cin >> n;
	vector<string> arr(n);
	for(int i = 0 ; i < n ; ++i) {
		cin >> arr[i];
	}
	vector<vector<int>> dp(26, vector<int>(26));
	int m = arr[0].length();
	char st = arr[0][0], en = arr[0][m - 1];
	dp[st - 'a'][en - 'a'] = m;
	for(int i = 1 ; i < n ; ++i) {
		int m = arr[i].length();
		char st = arr[i][0], en = arr[i][m - 1];
		for(int j = 0 ; j < 26 ; ++j) {
			if(dp[j][st - 'a']) {
				dp[j][en - 'a'] = dp[j][st - 'a'] + m;
			}
		}
		dp[st - 'a'][en - 'a'] = max(dp[st - 'a'][en - 'a'], m);
	}
	for(int i = 0 ; i < 26 ; ++i) {
		ans = max(ans, dp[i][i]);
	}
	cout << ans << endl;
	return 0;
}


#ifdef ONLINE_JUDGE

#include "bits/stdc++.h"
using namespace std;

#define ui unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double

#define all(v) v.begin(), v.end()

#define speed ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

const ld PI = 3.1415926535897932384626;
ll MOD = 1000000007;
const ld EPS = 1e-9;
const int N = 3e5 + 7;

inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ui setBits(ui v) { ui c; for(c = 0 ; v; ++c) v &= (v - 1); return c; }
inline bool isPowerOfTwo(ui v) { return (v && !(v & (v - 1))); }
ll power(ll base, ull exp, ll mod = MOD) {
	ll res = 1; base %= mod;
	while(exp) {
		if(exp & 1) res = (res * base)%mod;
		exp >>= 1; base = (base * base)%mod;
	}
	return res;
}
ll modInv(ll a, ll mod = MOD) {
	if(mod == 1) return 0;
	ll x = 1, y = 0, m = mod;
	while(a > 1) {
		ll q = a/m, t = m; m = a%m; a = t; t = y; y = x - q * y; x = t;
	}
	if(x < 0) x += mod;
	return x;
}
inline ui iabs(int x) { ui y = x; return (y > 0) ? y : -y; }
inline ll add(ll a, ll b, ll mod = MOD) { ll res = (a%mod + b%mod)%mod; return res; }
inline ll sub(ll a, ll b, ll mod = MOD) { ll res = (a%mod - b%mod + mod)%mod; return res; }
inline ll mult(ll a, ll b, ll mod = MOD) { ll res = (a%mod * b%mod)%mod; return res; }
inline ll div(ll a, ll b, ll mod = MOD) { ll res = (a%mod * modInv(b, mod))%mod; return res; }

#else

#include "custom.h"
using namespace std;

#endif

void openf() {
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
}

int main() {
	speed;
	openf();
	int n, ans = 0;
	cin >> n;
	vector<string> arr(n);
	for(int i = 0 ; i < n ; ++i) {
		cin >> arr[i];
	}
	vector<vector<int>> dp(26, vector<int>(26));
	int m = arr[0].length();
	char st = arr[0][0], en = arr[0][m - 1];
	dp[st - 'a'][en - 'a'] = m;
	for(int i = 1 ; i < n ; ++i) {
		int m = arr[i].length();
		char st = arr[i][0], en = arr[i][m - 1];
		dp[st - 'a'][en - 'a'] = max(dp[st - 'a'][en - 'a'], m);
		for(int j = 0 ; j < 26 ; ++j) {
			if(dp[j][st - 'a'] && j != st - 'a') {
				dp[j][en - 'a'] = dp[j][st - 'a'] + m;
			}
		}
	}
	for(int i = 0 ; i < 26 ; ++i) {
		ans = max(ans, dp[i][i]);
	}
	cout << ans << endl;
	return 0;
}


#ifdef ONLINE_JUDGE

#include "bits/stdc++.h"
using namespace std;

#define ui unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double

#define all(v) v.begin(), v.end()

#define speed ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

const ld PI = 3.1415926535897932384626;
ll MOD = 1000000007;
const ld EPS = 1e-9;
const int N = 3e5 + 7;

inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ui setBits(ui v) { ui c; for(c = 0 ; v; ++c) v &= (v - 1); return c; }
inline bool isPowerOfTwo(ui v) { return (v && !(v & (v - 1))); }
ll power(ll base, ull exp, ll mod = MOD) {
	ll res = 1; base %= mod;
	while(exp) {
		if(exp & 1) res = (res * base)%mod;
		exp >>= 1; base = (base * base)%mod;
	}
	return res;
}
ll modInv(ll a, ll mod = MOD) {
	if(mod == 1) return 0;
	ll x = 1, y = 0, m = mod;
	while(a > 1) {
		ll q = a/m, t = m; m = a%m; a = t; t = y; y = x - q * y; x = t;
	}
	if(x < 0) x += mod;
	return x;
}
inline ui iabs(int x) { ui y = x; return (y > 0) ? y : -y; }
inline ll add(ll a, ll b, ll mod = MOD) { ll res = (a%mod + b%mod)%mod; return res; }
inline ll sub(ll a, ll b, ll mod = MOD) { ll res = (a%mod - b%mod + mod)%mod; return res; }
inline ll mult(ll a, ll b, ll mod = MOD) { ll res = (a%mod * b%mod)%mod; return res; }
inline ll div(ll a, ll b, ll mod = MOD) { ll res = (a%mod * modInv(b, mod))%mod; return res; }

#else

#include "custom.h"
using namespace std;

#endif

void openf() {
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
}

int main() {
	speed;
	openf();
	int n, ans = 0;
	cin >> n;
	vector<string> arr(n);
	for(int i = 0 ; i < n ; ++i) {
		cin >> arr[i];
	}
	vector<vector<int>> dp(26, vector<int>(26));
	int m = arr[0].length();
	char st = arr[0][0], en = arr[0][m - 1];
	dp[st - 'a'][en - 'a'] = m;
	for(int i = 1 ; i < n ; ++i) {
		int m = arr[i].length();
		char st = arr[i][0], en = arr[i][m - 1];
		for(int j = 0 ; j < 26 ; ++j) {
			if(dp[j][st - 'a']) {
				dp[j][en - 'a'] = max(dp[j][en - 'a'], dp[j][st - 'a'] + m);
			}
		}
		dp[st - 'a'][en - 'a'] = max(dp[st - 'a'][en - 'a'], m);
	}
	for(int i = 0 ; i < 26 ; ++i) {
		ans = max(ans, dp[i][i]);
	}
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define pa pair<int,int>
#define v1d vector<int>
#define v2d vector<vector<int>>
#define mod 1000000007
#define mp make_pair
#define ff first
#define ss second
#define ump map<int,int>
#define endl "\n"
#define v1s vector<string>
#define S(v) sort(v.begin(),v.end())
#define mxpq priority_queue <int>
#define mnpq priority_queue<int,vector<int>,greater<int>>
void Weapons19(){
    cin.sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

}

int32_t main(){
    Weapons19();
    int n;cin>>n;
    v2d v(26,v1d (26,0));
    for(int i=0;i<n;i++){
        string s;cin>>s;
        int f=s[0]-97;
        int r=s[s.size()-1]-97;
        //cout<<r<<endl;
        if(v[f][r]==0){

        }
        for(int j=0;j<26;j++){
            if(j==f&&v[j][r]<s.size()){
                v[f][r]=s.size();
            }
            else if(v[j][f]+s.size()>v[j][r]&&v[j][f]!=0){
                v[j][r]=v[j][f]+s.size();
            }
        }
    }
    int ans=0;
    for(int i=0;i<26;i++){
        ans=max(ans,v[i][i]);
        //cout<<i<<" "<<v[i][i]<<endl;
    }

    cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define pa pair<int,int>
#define v1d vector<int>
#define v2d vector<vector<int>>
#define mod 1000000007
#define mp make_pair
#define ff first
#define ss second
#define ump map<int,int>
#define endl "\n"
#define v1s vector<string>
#define S(v) sort(v.begin(),v.end())
#define mxpq priority_queue <int>
#define mnpq priority_queue<int,vector<int>,greater<int>>
void Weapons19(){
    cin.sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

}

int32_t main(){
    Weapons19();
    int n;cin>>n;
    v2d v(26,v1d (26,0));
    for(int i=0;i<n;i++){
        string s;cin>>s;
        int f=s[0]-97;
        int r=s[s.size()-1]-97;

        for(int j=0;j<26;j++){
            if(j==f&&f==r){
                v[f][r]+=s.size();continue;
            }
            if(j==f&&v[j][r]<s.size()){
                v[f][r]=s.size();
            }
            if(v[j][f]+s.size()>v[j][r]&&v[j][f]!=0){
                v[j][r]=v[j][f]+s.size();
            }
        }
    }
    int ans=0;
    for(int i=0;i<26;i++){
        ans=max(ans,v[i][i]);
        //cout<<i<<" "<<v[i][i]<<endl;
    }

    cout<<ans;//<<endl;
  /*  for(int i=0;i<26;i++){
        for(int j=0;j<26;j++){
            cout<<v[i][j]<<" ";
        }
        cout<<endl;
    }
    */

}


//g++  5.4.0

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define mp make_pair
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin>>n;
    string s[n+1];
    for(int i=1;i<=n;++i)
        cin>>s[i];
    ll dp[27][27];
    for(int i=1;i<=26;++i)
        for(int j=1;j<=26;++j)
            dp[i][j]=0;
    for(int i=1;i<=n;++i)
    {
        int st=s[i][0]-'a'+1;
        int en=s[i][s[i].length()-1]-'a'+1;
        for(int i=1;i<=26;++i)
            if(dp[i][st])
                dp[i][en]=max(dp[i][en],dp[i][st]+(ll)s[i].length());
        dp[st][en]=max(dp[st][en],(ll)s[i].length());
    }
    ll ma=0;
    for(int i=1;i<=26;++i)
        ma=max(ma,dp[i][i]);
    cout<<ma;
}


//g++  5.4.0

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define mp make_pair
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin>>n;
    string s[n+1];
    for(int i=1;i<=n;++i)
        cin>>s[i];
    ll dp[27][27];
    for(int i=1;i<=26;++i)
        for(int j=1;j<=26;++j)
            dp[i][j]=0;
    for(int i=1;i<=n;++i)
    {
        int st=s[i][0]-'a'+1;
        int en=s[i][s[i].length()-1]-'a'+1;
        for(int i1=1;i1<=26;++i1)
        {
            if(dp[i1][st])
                dp[i1][en]=max(dp[i1][en],dp[i1][st]+(ll)s[i].length());
        }
        dp[st][en]=max(dp[st][en],(ll)s[i].length());
    }

    ll ma=0;
    for(int i=1;i<=26;++i)
        ma=max(ma,dp[i][i]);
    cout<<ma;
}


#include<bits/stdc++.h>
#include <math.h>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include<map>
using namespace std;
#define ll long long int
typedef unsigned long long int ull;
typedef long double ldb;
#define B 1000000007
#define vi vector<int>
#define vll vector<long long>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define all(v) v.begin(),v.end()
#define INF 1e18
#define LB lower_bound
#define UB upper_bound
#define deci(n) fixed << setprecision(n)
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define PI 3.141592653589793238462643
#define BLOCK 555
//ans.resize(unique(ALL(ans)) - ans.begin());

ll smallestDivisor(ll n)
{
    // if divisible by 2
    if (n % 2 == 0)
        return 2;

    // iterate from 3 to sqrt(n)
    for (ll i = 3; i * i <= n; i += 2) {
        if (n % i == 0)
            return i;
    }

    return n;
}
ll max(ll a,ll b){return a>b?a:b;}
ll min(ll a,ll b){return a<b?a:b;}
ll gcd(ll a,ll b){while(b){ll t=a;a=b;b=t%b;}return a;}
ll lcm(ll a,ll b){return (max(a,b)/gcd(a,b))*min(a,b);}

int cnt[200005]={0};
vector<int> adj[200005];
map<int,int,greater<int> > factorize(int n)
{
    int count=0;
    map<int,int,greater<int> > fact;
    while(!(n%2))
    {
        n/=2;
        count++;
    }
    if (count)
    fact[2]+=count;
    for(int i=3;i<=sqrt(n);i+=2)
    {
        count=0;
        while((n%i)==0)
        {
            count++;
            n/=i;
        }
        if (count)
        fact[i]+=count;
    }
    if(n>2)
        fact[n]++;
    for(auto it:fact)
    {
        cnt[it.first]++;
        adj[it.first].pb(it.second);
    }
    return fact;
}

long long int power(long long int x, int p)
{
    if (p == 0)
        return 1;
    long long int ans = power(x, p / 2);
    ans = ans * ans;
    if (p % 2 == 1)
        ans = ans * x;
    return ans;
}
/*
long long int factorial(long long int n){
    if(n <= 1)
        return 1;
    long long int ans = 1;
    for(ll i=1;i<=n;i++){
        ans = ans * i;
        // ans = ans % B;
    }
    return ans;
}

*/

/*
ll nCr(ll n,ll k){
    ll C[n + 1][k + 1];
    ll i, j;
    for (i=0;i<=n;i++){
        for(j=0;j<=min(i,k);j++){
            if(j==0||j==i)
                C[i][j]=1;
            else
                C[i][j]=(C[i-1][j-1]%B+C[i-1][j]%B)%B;
        }
    }
    return C[n][k]%B;
}
*/

bool isPrime(ll n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;
    for (ll i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;
    return true;
}
vector<ll> countDivisors(ll n)
{
    ll cnt = 0;
    std::vector<ll> v;
    for (ll i = 1; i <= sqrt(n); i++)
    {
        if (n % i == 0)
        {
         v.pb(i);
            // If divisors are equal,
            // count only one
            if (n / i == i)
                cnt++;

            else // Otherwise count both
                {cnt = cnt + 2;
                    v.pb(n/i);}
        }
    }
    return v;
}

bool isPowerOfTwo(ll x)
    {   // x will check if x == 0 and !(x & (x - 1)) will check if x is a power of 2 or not
        return (x && !(x & (x - 1)));
    }

map<ll,ll> factors(ll n)
 {
        vector<ll> f;
        map<ll,ll> m;
        for (ll x = 2; x*x <= n; x++)
        {
           while (n%x == 0) {
            f.push_back(x);
            m[x]++;
            n /= x; }
        }
    if (n > 1)
     {f.push_back(n);
        m[n]++;
     }
  return m;
}

bool cmp(pair<ll,ll>v1,pair<ll,ll>v2)
{
    if(v1.first>v2.first)
    {
        return true;
    }
    else if(v1.first<v2.first)
    {
        return false;
    }
    else
    {
        return v1.second<v2.second;
    }
}

ll binarySearch(ll arr[], ll l, ll r, ll x)
{
    if (r >= l) {
        ll mid = l + (r - l) / 2;
        if (arr[mid] == x)
            return mid;
        if (arr[mid] > x)
            return binarySearch(arr, l, mid - 1, x);
        return binarySearch(arr, mid + 1, r, x);
    }
    return -1;
}
int rub[2000001];
bool ispali(int n){
    int no=n;
    int rev=0;
    while(no>0){
            int ld=no%10;
    no/=10;
        rev=rev*10+ld;
    }
    if(n==rev)
        return true;
    else
        return false;
}
void np(){
for(int i=1;i<=2000000;i++){
    if(ispali(i))
        rub[i]=rub[i-1]+1;
    else
        rub[i]=rub[i-1];
}
}

int pi[20000001];
bool isprime[20000001];
void seive(){
for(int i=4;i<=20000000;i+=2)
    isprime[i]=1;
for(int i=3;i*i<=20000000;i+=2){
    if(!isprime[i]){
        for(int j=i*i;j<=20000000;j+=i)
            isprime[j]=1;
    }
}
for(int i=2;i<=20000000;i++){
    if(!isprime[i])
    pi[i]=pi[i-1]+1;
    else
    pi[i]=pi[i-1];
}
}
int g1;
void bfs( vector<vector<int> > &g , int pos , vector<int> visited )
{
    queue <int> q;
    q.push( pos );
    while( q.size()>0 )
    {
       // cout<<q.front()<<" ";
        if(q.front()==g1){ cout<<"yes";return ;}
        int i = q.front();
        q.pop();
         visited[i]=1;

        for(int j = 0 ; j < g[i].size() ; j++)
        {
            if( visited[ g[i][j] ] == 0 )
            {
                q.push( g[i][j] );
                visited[g[i][j]] = 1;
            }
        }
    }
  return;
}
void pairsort(ll a[], ll b[], ll n)
{
    pair<ll, ll> pairt[n];
    for (ll i = 0; i < n; i++)
    {
        pairt[i].first = a[i];
        pairt[i].second = b[i];
    }
    sort(pairt, pairt + n);
    for (ll i = 0; i < n; i++)
    {
        a[i] = pairt[i].first;
        b[i] = pairt[i].second;
    }
}

ll modpower(ll x, ll y, ll p)
{
    ll res = 1;      // Initialize result

    x = x % p;  // Update x if it is more than or
                // equal to p
    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
ll modsum(ll a, ll b, ll m){

    return ((a%m)+(b%m))%m;
}

ll modmul(ll a, ll b, ll m){

    return ((a%m)*(b%m))%m;
}

ll modInverse(ll a, ll m)
{
    return modpower(a, m-2, m);
}
#define SIZE 200002
vector<long long int> factorial()
{
    int i;
    vector<long long int> fact(SIZE + 1, 0);
    fact[0] = 1;
    for (i = 1; i <= SIZE; i++)
        fact[i] = (fact[i - 1] * i) % B;
    return fact;
}
ll highestPowerof2(ll n)
{
   ll p = (ll)log2(n);
    return (ll)pow(2, p);
}

int main()
{
    IOS;// seive(); np();
    int t1=1;
    // cin>>t1;
    while(t1--)
    {
        // ll n;cin>>n; ll a[n]; rep(i,0,n)cin>>a[i];

        int dp[26][26]={0};
        int n;cin>>n;
        while(n--)
        {
            string s;cin>>s;
            int fs=(int)(s[0]-'a');
            int ls = (int)(s[(int)(s.length())-1]-'a');

            rep(i,0,26)
            {
                if(dp[i][fs])
                    dp[i][ls] = max(dp[i][fs]+(int)(s.length()), dp[i][ls]);
            }
               dp[fs][ls] = max(dp[fs][ls], (int)(s.length()));

        }
         int ans=-1;
         rep(i,0,26)
         ans = max(ans, dp[i][i]);

         cout << ans << "\n";

        }

    }
  return 0;
}


#include<bits/stdc++.h>
#include <math.h>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include<map>
using namespace std;
#define ll long long int
typedef unsigned long long int ull;
typedef long double ldb;
#define B 1000000007
#define vi vector<int>
#define vll vector<long long>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define all(v) v.begin(),v.end()
#define INF 1e18
#define LB lower_bound
#define UB upper_bound
#define deci(n) fixed << setprecision(n)
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define PI 3.141592653589793238462643
#define BLOCK 555
//ans.resize(unique(ALL(ans)) - ans.begin());

ll smallestDivisor(ll n)
{
    // if divisible by 2
    if (n % 2 == 0)
        return 2;

    // iterate from 3 to sqrt(n)
    for (ll i = 3; i * i <= n; i += 2) {
        if (n % i == 0)
            return i;
    }

    return n;
}
ll max(ll a,ll b){return a>b?a:b;}
ll min(ll a,ll b){return a<b?a:b;}
ll gcd(ll a,ll b){while(b){ll t=a;a=b;b=t%b;}return a;}
ll lcm(ll a,ll b){return (max(a,b)/gcd(a,b))*min(a,b);}

int cnt[200005]={0};
vector<int> adj[200005];
map<int,int,greater<int> > factorize(int n)
{
    int count=0;
    map<int,int,greater<int> > fact;
    while(!(n%2))
    {
        n/=2;
        count++;
    }
    if (count)
    fact[2]+=count;
    for(int i=3;i<=sqrt(n);i+=2)
    {
        count=0;
        while((n%i)==0)
        {
            count++;
            n/=i;
        }
        if (count)
        fact[i]+=count;
    }
    if(n>2)
        fact[n]++;
    for(auto it:fact)
    {
        cnt[it.first]++;
        adj[it.first].pb(it.second);
    }
    return fact;
}

long long int power(long long int x, int p)
{
    if (p == 0)
        return 1;
    long long int ans = power(x, p / 2);
    ans = ans * ans;
    if (p % 2 == 1)
        ans = ans * x;
    return ans;
}
/*
long long int factorial(long long int n){
    if(n <= 1)
        return 1;
    long long int ans = 1;
    for(ll i=1;i<=n;i++){
        ans = ans * i;
        // ans = ans % B;
    }
    return ans;
}

*/

/*
ll nCr(ll n,ll k){
    ll C[n + 1][k + 1];
    ll i, j;
    for (i=0;i<=n;i++){
        for(j=0;j<=min(i,k);j++){
            if(j==0||j==i)
                C[i][j]=1;
            else
                C[i][j]=(C[i-1][j-1]%B+C[i-1][j]%B)%B;
        }
    }
    return C[n][k]%B;
}
*/

bool isPrime(ll n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;
    for (ll i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;
    return true;
}
vector<ll> countDivisors(ll n)
{
    ll cnt = 0;
    std::vector<ll> v;
    for (ll i = 1; i <= sqrt(n); i++)
    {
        if (n % i == 0)
        {
         v.pb(i);
            // If divisors are equal,
            // count only one
            if (n / i == i)
                cnt++;

            else // Otherwise count both
                {cnt = cnt + 2;
                    v.pb(n/i);}
        }
    }
    return v;
}

bool isPowerOfTwo(ll x)
    {   // x will check if x == 0 and !(x & (x - 1)) will check if x is a power of 2 or not
        return (x && !(x & (x - 1)));
    }

map<ll,ll> factors(ll n)
 {
        vector<ll> f;
        map<ll,ll> m;
        for (ll x = 2; x*x <= n; x++)
        {
           while (n%x == 0) {
            f.push_back(x);
            m[x]++;
            n /= x; }
        }
    if (n > 1)
     {f.push_back(n);
        m[n]++;
     }
  return m;
}

bool cmp(pair<ll,ll>v1,pair<ll,ll>v2)
{
    if(v1.first>v2.first)
    {
        return true;
    }
    else if(v1.first<v2.first)
    {
        return false;
    }
    else
    {
        return v1.second<v2.second;
    }
}

ll binarySearch(ll arr[], ll l, ll r, ll x)
{
    if (r >= l) {
        ll mid = l + (r - l) / 2;
        if (arr[mid] == x)
            return mid;
        if (arr[mid] > x)
            return binarySearch(arr, l, mid - 1, x);
        return binarySearch(arr, mid + 1, r, x);
    }
    return -1;
}
int rub[2000001];
bool ispali(int n){
    int no=n;
    int rev=0;
    while(no>0){
            int ld=no%10;
    no/=10;
        rev=rev*10+ld;
    }
    if(n==rev)
        return true;
    else
        return false;
}
void np(){
for(int i=1;i<=2000000;i++){
    if(ispali(i))
        rub[i]=rub[i-1]+1;
    else
        rub[i]=rub[i-1];
}
}

int pi[20000001];
bool isprime[20000001];
void seive(){
for(int i=4;i<=20000000;i+=2)
    isprime[i]=1;
for(int i=3;i*i<=20000000;i+=2){
    if(!isprime[i]){
        for(int j=i*i;j<=20000000;j+=i)
            isprime[j]=1;
    }
}
for(int i=2;i<=20000000;i++){
    if(!isprime[i])
    pi[i]=pi[i-1]+1;
    else
    pi[i]=pi[i-1];
}
}
int g1;
void bfs( vector<vector<int> > &g , int pos , vector<int> visited )
{
    queue <int> q;
    q.push( pos );
    while( q.size()>0 )
    {
       // cout<<q.front()<<" ";
        if(q.front()==g1){ cout<<"yes";return ;}
        int i = q.front();
        q.pop();
         visited[i]=1;

        for(int j = 0 ; j < g[i].size() ; j++)
        {
            if( visited[ g[i][j] ] == 0 )
            {
                q.push( g[i][j] );
                visited[g[i][j]] = 1;
            }
        }
    }
  return;
}
void pairsort(ll a[], ll b[], ll n)
{
    pair<ll, ll> pairt[n];
    for (ll i = 0; i < n; i++)
    {
        pairt[i].first = a[i];
        pairt[i].second = b[i];
    }
    sort(pairt, pairt + n);
    for (ll i = 0; i < n; i++)
    {
        a[i] = pairt[i].first;
        b[i] = pairt[i].second;
    }
}

ll modpower(ll x, ll y, ll p)
{
    ll res = 1;      // Initialize result

    x = x % p;  // Update x if it is more than or
                // equal to p
    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
ll modsum(ll a, ll b, ll m){

    return ((a%m)+(b%m))%m;
}

ll modmul(ll a, ll b, ll m){

    return ((a%m)*(b%m))%m;
}

ll modInverse(ll a, ll m)
{
    return modpower(a, m-2, m);
}
#define SIZE 200002
vector<long long int> factorial()
{
    int i;
    vector<long long int> fact(SIZE + 1, 0);
    fact[0] = 1;
    for (i = 1; i <= SIZE; i++)
        fact[i] = (fact[i - 1] * i) % B;
    return fact;
}
ll highestPowerof2(ll n)
{
   ll p = (ll)log2(n);
    return (ll)pow(2, p);
}

int main()
{
    IOS;// seive(); np();
    int t1=1;
    // cin>>t1;
    while(t1--)
    {
        // ll n;cin>>n; ll a[n]; rep(i,0,n)cin>>a[i];

        int dp[26][26]={0};
        int n;cin>>n;
        while(n--)
        {
            string s;cin>>s;
            int fs=(int)(s[0]-'a');
            int ls = (int)(s[(int)(s.length())-1]-'a');

            rep(i,0,26)
            {
                if(dp[i][fs])
                    dp[i][ls] = max(dp[i][fs]+(int)(s.length()), dp[i][ls]);
            }
               dp[fs][ls] = max(dp[fs][ls], (int)(s.length()));

        }
         int ans=-1;
         rep(i,0,26)
         ans = max(ans, dp[i][i]);

         cout << ans << "\n";

    }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int dp[26][26];
char S[17];

int main()
{
    int n;      scanf("%d", &n);
    for(int i=1; i<=n; ++i)
    {
        scanf("%s", S);
        int len = strlen(S), s = S[0] - 'a', t = S[len - 1] - 'a';
        for(int k=0; k<26; ++k)
            if(dp[k][s])
                dp[k][t] = max(dp[k][t], dp[k][s] + len);
        dp[s][t] = max(dp[s][t], len);
    }
    int ans = 0;
    for(int i=0; i<26; ++i)
        ans = max(ans, dp[i][i]);
    printf("%d", ans);
}


#include <bits/stdc++.h>
using namespace std;

int dp[26][26];

int main()
{
    int n;      cin >> n;
    for(int i=1; i<=n; ++i)
    {
        string S;   cin >> S;
        int len = S.size(), s = S[0] - 'a', t = S[len - 1] - 'a';
        for(int k=0; k<26; ++k)
            if(dp[k][s])
                dp[k][t] = max(dp[k][t], dp[k][s] + len);
        dp[s][t] = max(dp[s][t], len);
    }
    int ans = 0;
    for(int i=0; i<26; ++i)
        ans = max(ans, dp[i][i]);
    cout << ans;
}


#include<bits/stdc++.h>
using namespace std;

int pre[500050];
int sub[500050];
int len[500050];
string s[500050];
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>s[i];
		len[i]=s[i].size();
	}
	int ans=0;
	for(int i='a';i<='z';i++)
	{
		int temp[200];
		memset(pre,0,sizeof(pre));
		memset(sub,0,sizeof(sub));
		memset(temp,0,sizeof(temp));//看看前面有没有  i开头以fir结尾的 最大字符串 的长度
		for(int j=1;j<=n;j++)
		{
			char fir=s[j][0];//看看前面有没有  以i开头  以fir结尾的 最大字符串
			char ed=s[j][len[j]-1];
			if(temp[fir]==0){
				if(fir==i){
					temp[ed]=max(temp[ed],len[j]);
					pre[j]=temp[ed];
				}
			} else{
				temp[ed]=max(temp[ed],temp[fir]+len[j]);
				pre[j]=temp[ed];
			}
		}
		memset(temp,0,sizeof(temp));
		for(int j=n;j>=1;j--)
		{
			char fir=s[j][0];//看看前面有没有  以i开头  以fir结尾的 最大字符串
			char ed=s[j][len[j]-1];
			if(temp[ed]==0){
				if(ed==i){
					temp[fir]=max(temp[fir],len[j]);
					sub[j]=temp[fir];
				}
			} else{
				temp[fir]=max(temp[fir],temp[ed]+len[j]);
				sub[j]=temp[fir];
			}
		}

		for(int j=1;j<=n;j++)
		{
			if(sub[j]&&pre[j])
				ans=max(ans,sub[j]+pre[j]-len[j]);
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int pre[500050];
int sub[500050];
int len[500050];
string s[500050];
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>s[i];
		len[i]=s[i].size();
	}
	int ans=0;
	for(int i='a';i<='z';i++)
	{
		int temp[200];
		memset(temp,0,sizeof(temp));//看看前面有没有  i开头以fir结尾的 最大字符串 的长度
		for(int j=1;j<=n;j++)
		{
			char fir=s[j][0];//看看前面有没有  以i开头  以fir结尾的 最大字符串
			char ed=s[j][len[j]-1];
			if(temp[fir]==0){
				if(fir==i){
					temp[ed]=max(temp[ed],len[j]);
				}
			} else{
				temp[ed]=max(temp[ed],temp[fir]+len[j]);
			}
			pre[j]=temp[ed];
		}
		memset(temp,0,sizeof(temp));
		for(int j=n;j>=1;j--)
		{
			char fir=s[j][0];//看看前面有没有  以i开头  以fir结尾的 最大字符串
			char ed=s[j][len[j]-1];
			if(temp[ed]==0){
				if(ed==i){
					temp[fir]=max(temp[fir],len[j]);
				}
			} else{
				temp[fir]=max(temp[fir],temp[ed]+len[j]);
			}
			sub[j]=temp[fir];
		}

		for(int j=1;j<=n;j++)
		{
			if(sub[j]&&pre[j])
				ans=max(ans,sub[j]+pre[j]-len[j]);
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int dp[200][200];
string s[500050];
int len[500050];
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>s[i];
		len[i]=s[i].size();
	}
	for(int i=1;i<=n;i++)
	{
		char fir=s[i][0];
		char ed=s[i][len[i]-1];
		for(int j='a';j<='z';j++)
		{
			if(dp[j][fir]!=0)
				dp[j][ed]=max(dp[j][ed],dp[j][fir]+len[i]);
		}
		dp[fir][ed]=max(dp[fir][ed],len[i]);
	}
	int ans=0;
	for(int i='a';i<='z';i++)
	{
		ans=max(dp[i][i],ans);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll  long long
#define pb push_back
#define mkp make_pair
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pi 3.14159265359
//PLEASE DON'T HACK I AM POOR :(
using namespace std;
int main()
{
fastio
ll n,i,j,k;
cin>>n;
ll mx=0;
ll dp[26][26]={0};
while(n--)
{   char temp;
	string s;
	cin>>s;
	ll m=s.size();
	char f=s[0],l=s[s.size()-1];
	if(dp[l-'a'][f-'a'])
	mx=max(mx,dp[l-'a'][f-'a']+m);
	dp[f-'a'][l-'a']=max(dp[f-'a'][l-'a'],m);
	if(l==f)
	mx=max(mx,m);
	for(temp='a';temp<='z';temp++)
	{
		dp[temp-'a'][l-'a']=max(dp[temp-'a'][l-'a'],dp[temp-'a'][f-'a']+dp[f-'a'][l-'a']);
	}
}
cout<<mx<<"\n";
return 0;
}


#include<bits/stdc++.h>
#define ll  long long
#define pb push_back
#define mkp make_pair
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pi 3.14159265359
//PLEASE DON'T HACK I AM POOR :(
using namespace std;
int main()
{
fastio
ll n,i,j,k;
cin>>n;
ll mx=0;
ll dp[26][26]={0};
while(n--)
{   char temp;
	string s;
	cin>>s;
	ll m=s.size();
	char f=s[0],l=s[s.size()-1];
	if(dp[l-'a'][f-'a'])
	mx=max(mx,dp[l-'a'][f-'a']+m);
	if(l==f)
	mx=max(mx,m);
	for(temp='a';temp<='z';temp++)
	{
		dp[temp-'a'][l-'a']=max(dp[temp-'a'][l-'a'],dp[temp-'a'][f-'a']+dp[f-'a'][l-'a']);
	}
	dp[f-'a'][l-'a']=max(dp[f-'a'][l-'a'],m);
}
cout<<mx<<"\n";
return 0;
}


#include<bits/stdc++.h>
#define ll  long long
#define pb push_back
#define mkp make_pair
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pi 3.14159265359
//PLEASE DON'T HACK I AM POOR :(
using namespace std;
int main()
{
fastio
ll n,i,j,k;
cin>>n;
ll mx=0;
ll dp[26][26]={0};
while(n--)
{   char temp;
	string s;
	cin>>s;
	ll m=s.size();
	char f=s[0],l=s[s.size()-1];
	if(dp[l-'a'][f-'a'])
	mx=max(mx,dp[l-'a'][f-'a']+m);
	if(f==l)mx=max(mx,m);
	for(temp='a';temp<='z';temp++)
	{
		if(dp[temp-'a'][f-'a'])
		dp[temp-'a'][l-'a']=max(dp[temp-'a'][f-'a']+m,dp[temp-'a'][l-'a']);
	}
	dp[f-'a'][l-'a']=max(dp[f-'a'][l-'a'],m);
}
cout<<mx<<"\n";
return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <locale>
using namespace std;
#define MS0(I) memset(I, 0, sizeof(I));
#define MS1(I) memset(I, -1, sizeof(I));
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define rep1(i, s, n) for(int i = (s); i < (n); ++i)
#define per1(i, a, b) for(int i = a; i >= b; --i)
#define DRI(A) int A; scanf("%d", &A);
#define DRII(A, B) int A, B; scanf("%d %d", &A, &B);
#define DRIII(A, B, C) int A, B, C; scanf("%d %d %d", &A, &B, &C);
#define MP make_pair
#define pb push_back
#define F first
#define S second
#define ALL(I) (I).begin(), (I).end()
typedef const void CV;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int INF = (int)1e9;
const int mod = (int)1e9+7;
const int MAX = (int)5e5+5;
struct data{
	int s, e, l;
}store[MAX];
int dp[26][26];
int main(void){
	DRI(n);
	rep1(i, 1, n+1){
		char buf[MAX]; scanf("%s", buf);
		int l = strlen(buf);
		store[i] = (data){buf[0] - 'a', buf[l-1] - 'a', l};
	}
	rep1(i, 1, n+1){
		rep(j, 26) if(dp[j][store[i].s]){
			dp[j][store[i].e] = max(dp[j][store[i].e], dp[j][store[i].s] + store[i].l);
		}
		dp[store[i].s][store[i].e] = max(dp[store[i].s][store[i].e], store[i].l);
	}

	int ans = 0;
	rep(i, 26) ans = max(ans, dp[i][i]);
	printf("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <locale>
using namespace std;
#define MS0(I) memset(I, 0, sizeof(I));
#define MS1(I) memset(I, -1, sizeof(I));
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define rep1(i, s, n) for(int i = (s); i < (n); ++i)
#define per1(i, a, b) for(int i = a; i >= b; --i)
#define DRI(A) int A; scanf("%d", &A);
#define DRII(A, B) int A, B; scanf("%d %d", &A, &B);
#define DRIII(A, B, C) int A, B, C; scanf("%d %d %d", &A, &B, &C);
#define MP make_pair
#define pb push_back
#define F first
#define S second
#define ALL(I) (I).begin(), (I).end()
typedef const void CV;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int INF = (int)1e9;
const int mod = (int)1e9+7;
const int MAX = (int)5e5+5;
char store[MAX][13];
int dp[26][2][26];
int main(void){
	DRI(n);
	rep1(i, 1, n+1) scanf("%s", store[i]);
	int l = strlen(store[1]);
	dp[ store[1][0] - 'a' ][0][ store[1][l-1] - 'a' ] = l;
	rep1(i, 2, n+1){
		l = strlen(store[i]);
		rep(j, 26) rep(k, 26) dp[j][1][k] = dp[j][0][k];
		rep(j, 26) if(dp[j][0][store[i][0] - 'a']){
			dp[j][1][store[i][l-1] - 'a'] = max(dp[j][1][store[i][l-1] - 'a'],
			dp[j][0][store[i][0] - 'a'] + l);
		}
		dp[ store[i][0] - 'a' ][1][ store[i][l-1] - 'a'] = max(dp[ store[i][0] - 'a' ][1][ 			store[i][l-1] - 'a'], l);
		rep(j, 26) rep(k, 26) dp[j][0][k] = dp[j][1][k];
	}

	int ans = 0;
	rep(i, 26) ans = max(ans, dp[i][0][i]);
	printf("%d\n", ans);
	return 0;
}


//By Vasu Agrawal
#include<iostream>
#include<bits/stdc++.h>
typedef long long int ll;
#define pb push_back
#define pob pop_back
#define mk make_pair
#define fr first
#define sc second
#define pu push
#define ins insert
#define mod 1000000007
#define mem(a,val) memset(a,val,sizeof(a))
#define V(a) vector<a>
#define pp pair<ll,ll>
#define lb(a,n) lower_bound(a.begin(),a.end(),n)-a.begin()
#define ub(a,n) upper_bound(a.begin(),a.end(),n)-a.begin()
#define F(i,n) for(ll i=0;i<n;i++)
#define in(a,n) F(i,n) cin>>a[i]
#define in1(a,n) f(i,n) cin>>a[i]
#define out1(a,n); f(i,n) cout<<a[i]<<" ";cout<<endl;
#define out(a,n); F(i,n) cout<<a[i]<<" ";cout<<endl;
#define f(i,n) for(ll i=1;i<=n;i++)
#define vasu2907 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
bool isprime(ll n) //Time Complexity--->sqrt(n)
{   if (n <= 1)  return false; if (n <= 3)  return true;
  	if (n%2 == 0 || n%3 == 0) return false;
    for (int i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false;
    return true;
}
vector<ll> prime;
void sieve(ll n)
{	bool bakh[n+1];memset(bakh, true, sizeof(bakh));
    for (ll p=2; p*p<=n; p++)
    {	if (bakh[p] == true)
        {	for (ll i=p*p; i<=n; i += p)    bakh[i] = false; } }
  	for (ll p=2; p<=n; p++)    if (bakh[p])
	  	prime.pb(p);
}//Taken from https://www.geeksforgeeks.org/eulers-totient-function-for-all-numbers-smaller-than-or-equal-to-n/
ll eulertotient(ll z)   //Time--->n/logn
{	ll fac=z;
	for(ll i=0;prime[i]*prime[i]<=z;i++)
	{	if(z%prime[i]==0)
		{	fac-=(fac/prime[i]);	while(z%prime[i]==0)	z/=prime[i];	}
	}	if(z>1)	fac-=(fac/z);
	return fac;
}
//Taken from https://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/
ll power(ll x,ll y,ll p)
{	ll res = 1; x = x % p;	while (y > 0)  {	if (y & 1)	res = (res*x) % p; 	y = y>>1;	x = (x*x) % p; }return res; }
//Taken from https://www.geeksforgeeks.org/euclidean-algorithms-basic-and-extended/
ll gcd(ll a,ll b)
{	if (a == 0)    return b; 	return gcd(b % a, a); }
ll lcm(ll a,ll b)
{	ll g=gcd(a,b); ll ans=(a*b)/g; return ans;	}
//Taken from https://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/
ll modInverse(ll a, ll m)
{	ll hvf=gcd(a,m);	if(hvf==1)	return power(a,m-2,m);	return -1;}
void multiply(ll F[2][2], ll M[2][2])
{ ll x =  F[0][0]*M[0][0] + F[0][1]*M[1][0];  ll y =  F[0][0]*M[0][1] + F[0][1]*M[1][1];
  ll z =  F[1][0]*M[0][0] + F[1][1]*M[1][0];  ll w =  F[1][0]*M[0][1] + F[1][1]*M[1][1];
  F[0][0] = x;   F[0][1] = y;   F[1][0] = z;   F[1][1] = w;
}
void powermat(ll F[2][2], ll n)
{	if( n == 0 || n == 1)
    return; ll M[2][2] = {{1,1},{1,0}};   powermat(F, n/2);   multiply(F, F);   if (n%2 != 0)      multiply(F, M);
}
ll fib(ll n)
{ ll F[2][2] = {{1,1},{1,0}};
  if (n == 0) return 0; powermat(F, n-1);   return F[0][0];
}
//ll root(ll x)
//{	while(par[x]!=x)
//	{	par[x]=par[par[x]];
//		x=par[x];
//	}
//	return x;
//}
//void uni(ll x,ll y)
//{	x=root(x);
//	y=root(y);
//	if(val[x]<=val[y])
//		par[y]=par[x];
//	else
//		par[x]=par[y];
//}

//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
// Code Starts Here
ll dp[26][26];
int main()
{	vasu2907;
	ll n;
	cin>>n;
	char l,f;
	string st;
	F(i,n)
	{	cin>>st;
		f=st[0]-'a',l=st[st.size()-1]-'a';
		for(ll j=0;j<26;j++)
		{	if(dp[j][f]!=0)
				dp[j][l]=max(dp[j][l],dp[j][f]+(ll)st.size());
		}
		dp[f][l]=max(dp[f][l],(ll)st.size());
	}
	ll a=0;
	for(ll i=0;i<26;i++)
		a=max(dp[i][i],a);
	cout<<a<<endl;
}


//By Vasu Agrawal
#include<iostream>
#include<bits/stdc++.h>
typedef long long int ll;
#define pb push_back
#define pob pop_back
#define mk make_pair
#define fr first
#define sc second
#define pu push
#define ins insert
#define mod 1000000007
#define mem(a,val) memset(a,val,sizeof(a))
#define V(a) vector<a>
#define pp pair<ll,ll>
#define lb(a,n) lower_bound(a.begin(),a.end(),n)-a.begin()
#define ub(a,n) upper_bound(a.begin(),a.end(),n)-a.begin()
#define F(i,n) for(ll i=0;i<n;i++)
#define in(a,n) F(i,n) cin>>a[i]
#define in1(a,n) f(i,n) cin>>a[i]
#define out1(a,n); f(i,n) cout<<a[i]<<" ";cout<<endl;
#define out(a,n); F(i,n) cout<<a[i]<<" ";cout<<endl;
#define f(i,n) for(ll i=1;i<=n;i++)
#define vasu2907 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
bool isprime(ll n) //Time Complexity--->sqrt(n)
{   if (n <= 1)  return false; if (n <= 3)  return true;
  	if (n%2 == 0 || n%3 == 0) return false;
    for (int i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false;
    return true;
}
vector<ll> prime;
void sieve(ll n)
{	bool bakh[n+1];memset(bakh, true, sizeof(bakh));
    for (ll p=2; p*p<=n; p++)
    {	if (bakh[p] == true)
        {	for (ll i=p*p; i<=n; i += p)    bakh[i] = false; } }
  	for (ll p=2; p<=n; p++)    if (bakh[p])
	  	prime.pb(p);
}//Taken from https://www.geeksforgeeks.org/eulers-totient-function-for-all-numbers-smaller-than-or-equal-to-n/
ll eulertotient(ll z)   //Time--->n/logn
{	ll fac=z;
	for(ll i=0;prime[i]*prime[i]<=z;i++)
	{	if(z%prime[i]==0)
		{	fac-=(fac/prime[i]);	while(z%prime[i]==0)	z/=prime[i];	}
	}	if(z>1)	fac-=(fac/z);
	return fac;
}
//Taken from https://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/
ll power(ll x,ll y,ll p)
{	ll res = 1; x = x % p;	while (y > 0)  {	if (y & 1)	res = (res*x) % p; 	y = y>>1;	x = (x*x) % p; }return res; }
//Taken from https://www.geeksforgeeks.org/euclidean-algorithms-basic-and-extended/
ll gcd(ll a,ll b)
{	if (a == 0)    return b; 	return gcd(b % a, a); }
ll lcm(ll a,ll b)
{	ll g=gcd(a,b); ll ans=(a*b)/g; return ans;	}
//Taken from https://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/
ll modInverse(ll a, ll m)
{	ll hvf=gcd(a,m);	if(hvf==1)	return power(a,m-2,m);	return -1;}
void multiply(ll F[2][2], ll M[2][2])
{ ll x =  F[0][0]*M[0][0] + F[0][1]*M[1][0];  ll y =  F[0][0]*M[0][1] + F[0][1]*M[1][1];
  ll z =  F[1][0]*M[0][0] + F[1][1]*M[1][0];  ll w =  F[1][0]*M[0][1] + F[1][1]*M[1][1];
  F[0][0] = x;   F[0][1] = y;   F[1][0] = z;   F[1][1] = w;
}
void powermat(ll F[2][2], ll n)
{	if( n == 0 || n == 1)
    return; ll M[2][2] = {{1,1},{1,0}};   powermat(F, n/2);   multiply(F, F);   if (n%2 != 0)      multiply(F, M);
}
ll fib(ll n)
{ ll F[2][2] = {{1,1},{1,0}};
  if (n == 0) return 0; powermat(F, n-1);   return F[0][0];
}
//ll root(ll x)
//{	while(par[x]!=x)
//	{	par[x]=par[par[x]];
//		x=par[x];
//	}
//	return x;
//}
//void uni(ll x,ll y)
//{	x=root(x);
//	y=root(y);
//	if(val[x]<=val[y])
//		par[y]=par[x];
//	else
//		par[x]=par[y];
//}

//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
// Code Starts Here
ll dp[26][26];
int main()
{	vasu2907;
	ll n;
	cin>>n;
	char l,f;
	string st;
	F(i,n)
	{	cin>>st;
		f=st[0]-'a',l=st[st.size()-1]-'a';
		for(ll j=0;j<26;j++)
		{	if(dp[j][f]!=0)
				dp[j][l]=dp[j][f]+st.size();
		}
		dp[f][l]=max(dp[f][l],(ll)st.size());
	}
	ll a=0;
	for(ll i=0;i<26;i++)
		a=max(dp[i][i],a);
	cout<<a<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
#define MOD 1000000007
#define INF 1000000000
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
#define endl '\n'
#define pl cout<<endl;
# define PI  3.14159265358979323846
ll maxi=LLONG_MIN;
ll maxii=LLONG_MIN;
ll mini=LLONG_MAX;
int main()
{
	#ifndef ONLINE_JUDGE
 	freopen("D:\\Codes\\in.txt", "r", stdin);
 	freopen("D:\\Codes\\out.txt", "w", stdout);
 	#endif
 	int n;cin>>n;
 	string s[n];
 	for (int i = 0; i < n; ++i)
 	{
 		cin>>s[i];
 	}
 	int dp[26][26]={{0,0}};
    for (int i = 0; i < n; ++i)
    {
    	int len=s[i].length();
    	int f = s[i][0]-'a';
    	int l = s[i][len-1]-'a';
    	for (int k = 0; k < 26 ; ++k)
        {
            if (dp[l][k]!=0)
            {
                dp[f][k]=max(dp[f][k],dp[l][k]+len);
            }
        }
    	dp[f][l]=max(dp[f][l],len);
    }
    int ans=INT_MIN;
    for (int i = 0; i < 26; ++i)
    {
    	ans=max(ans,dp[i][i]);
    }
    cout<<ans;
 	return 0;
 }


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
#define MOD 1000000007
#define INF 1000000000
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
#define endl '\n'
#define pl cout<<endl;
# define PI  3.14159265358979323846
ll maxi=LLONG_MIN;
ll maxii=LLONG_MIN;
ll mini=LLONG_MAX;
int main()
{
	#ifndef ONLINE_JUDGE
 	freopen("D:\\Codes\\in.txt", "r", stdin);
 	freopen("D:\\Codes\\out.txt", "w", stdout);
 	#endif
 	int n;cin>>n;
 	string s[n];
 	for (int i = 0; i < n; ++i)
 	{
 		cin>>s[i];
 	}
 	int dp[26][26]={{0,0}};
    for (int i = 0; i < n; ++i)
    {
    	int len=s[i].length();
    	int f = s[i][0]-'a';
    	int l = s[i][len-1]-'a';
    	for (int j = 0; j < 26; ++j)
        {
            if (dp[j][f]!=0)
            {
                dp[j][l]=max(dp[j][l],dp[j][f]+len);
            }

        }
    	dp[f][l]=max(dp[f][l],len);
    }
    int ans=INT_MIN;
    for (int i = 0; i < 26; ++i)
    {
    	ans=max(ans,dp[i][i]);
    }
    cout<<ans;
 	return 0;
 }


#include<bits/stdc++.h>
using namespace std;

#define N 1000
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 1000000007
#define all(cont) cont.begin(), cont.end()
#define sortcmp(n, cmpfunc) sort(all(n), cmpfunc)
#define fastio ios::sync_with_stdio(false); cin.tie(0)

ll gcd(ll a, ll b);
ll lcm(ll a, ll b);
ll powm(ll a, ll b);
ll divm(ll a, ll b);
ll multm(ll a, ll b);
ll subm(ll a, ll b);
ll addm(ll a, ll b);

int main(){
	fastio;
	#ifndef ONLINE_JUDGE
    	freopen("input.in", "r", stdin);
    	freopen("output.out", "w", stdout);
	#endif

	int n, ans = 0, dp[26][26], a, b, sz;
    memset(dp, 0, sizeof(dp));
    string s;
	cin >> n;

	for(int i = 0 ; i < n ; i++){
		cin >> s;
		sz = s.size();
		a = (int)s[0] - 'a';
		b = (int)s[sz-1] - 'a';

		if(a == b) dp[a][a] += sz;
		else if(dp[b][a]) dp[b][b] = max(dp[b][b], (sz + dp[b][a]));
		dp[a][b] = max(dp[a][b], sz);
	}

	for(int i = 0 ; i < 26 ; i++)
		ans = max(ans, dp[i][i]);

	cout << ans << endl;
	return 0;
}

// template function
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}

ll lcm(ll a, ll b){
	return a*b/gcd(a, b);
}

ll powm(ll a, ll b){
	ll x=1, y=a;
    while (b > 0) {
        if (b%2) x = (x*y) % mod;
        y = (y*y) % mod;
        b /= 2;
    }
    return x % mod;
}

ll multm(ll a, ll b){
	return ((a%mod) * (b%mod)) % mod;
}

ll divm(ll a, ll b){
	return (a * powm(b, mod - 2));
}

ll subm(ll a, ll b){
	a -= b;
  	if (a < 0) a += mod;
	return a;
}

ll addm(ll a, ll b){
	a += b;
  	if (a >= mod) a -= mod;
  	return a;
}


#include<bits/stdc++.h>
using namespace std;

#define N 1000
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 1000000007
#define all(cont) cont.begin(), cont.end()
#define sortcmp(n, cmpfunc) sort(all(n), cmpfunc)
#define fastio ios::sync_with_stdio(false); cin.tie(0)

ll gcd(ll a, ll b);
ll lcm(ll a, ll b);
ll powm(ll a, ll b);
ll divm(ll a, ll b);
ll multm(ll a, ll b);
ll subm(ll a, ll b);
ll addm(ll a, ll b);

int main(){
	fastio;
	#ifndef ONLINE_JUDGE
    	freopen("input.in", "r", stdin);
    	freopen("output.out", "w", stdout);
	#endif

	int n, ans = 0, dp[26][26], a, b, sz;
    memset(dp, 0, sizeof(dp));
    string s;
	cin >> n;
	for(int i = 0 ; i < n ; i++){
		cin >> s;
		sz = s.size();
		a = (int)s[0] - 'a';
		b = (int)s[sz-1] - 'a';
		if(dp[b][a]) {
			if(a != b) dp[a][a] = max(dp[a][a], (sz + dp[b][a]));
			else dp[a][a] += sz;
		}

		dp[a][b] = max(dp[a][b], sz);
	}

	for(int i = 0 ; i < 26 ; i++)
		ans = max(ans, dp[i][i]);

	cout << ans << endl;
	return 0;
}

// template function
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}

ll lcm(ll a, ll b){
	return a*b/gcd(a, b);
}

ll powm(ll a, ll b){
	ll x=1, y=a;
    while (b > 0) {
        if (b%2) x = (x*y) % mod;
        y = (y*y) % mod;
        b /= 2;
    }
    return x % mod;
}

ll multm(ll a, ll b){
	return ((a%mod) * (b%mod)) % mod;
}

ll divm(ll a, ll b){
	return (a * powm(b, mod - 2));
}

ll subm(ll a, ll b){
	a -= b;
  	if (a < 0) a += mod;
	return a;
}

ll addm(ll a, ll b){
	a += b;
  	if (a >= mod) a -= mod;
  	return a;
}


#include<bits/stdc++.h>
using namespace std;

#define N 1000
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 1000000007
#define all(cont) cont.begin(), cont.end()
#define sortcmp(n, cmpfunc) sort(all(n), cmpfunc)
#define fastio ios::sync_with_stdio(false); cin.tie(0)

ll gcd(ll a, ll b);
ll lcm(ll a, ll b);
ll powm(ll a, ll b);
ll divm(ll a, ll b);
ll multm(ll a, ll b);
ll subm(ll a, ll b);
ll addm(ll a, ll b);

int main(){
	fastio;
	#ifndef ONLINE_JUDGE
    	freopen("input.in", "r", stdin);
    	freopen("output.out", "w", stdout);
	#endif

	int n, ans = 0, dp[26][26], a, b, sz;
    memset(dp, 0, sizeof(dp));
    string s;
	cin >> n;
	for(int i = 0 ; i < n ; i++){
		cin >> s;
		sz = s.size();
		a = (int)s[0] - 'a';
		b = (int)s[sz-1] - 'a';

		for(int i = 0 ; i < 26 ; i++)
			if(dp[i][a] || (i == a)) dp[i][b] = max(dp[i][b], dp[i][a] + sz);

		dp[a][b] = max(dp[a][b], sz);
	}

	for(int i = 0 ; i < 26 ; i++)
		ans = max(ans, dp[i][i]);

	cout << ans << endl;
	return 0;
}

// template function
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}

ll lcm(ll a, ll b){
	return a*b/gcd(a, b);
}

ll powm(ll a, ll b){
	ll x=1, y=a;
    while (b > 0) {
        if (b%2) x = (x*y) % mod;
        y = (y*y) % mod;
        b /= 2;
    }
    return x % mod;
}

ll multm(ll a, ll b){
	return ((a%mod) * (b%mod)) % mod;
}

ll divm(ll a, ll b){
	return (a * powm(b, mod - 2));
}

ll subm(ll a, ll b){
	a -= b;
  	if (a < 0) a += mod;
	return a;
}

ll addm(ll a, ll b){
	a += b;
  	if (a >= mod) a -= mod;
  	return a;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define REP(i,a,b) for (int i = a; i <= b; i++)
#define RA(v,n) REP(i, 0, n - 1) cin >> v[i]

template<typename T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

void solve() {
    int n; cin >> n;
    vector<string> names(n);
    RA(names, n);

    int ans = 0;
    for (int c = 0; c < 26; c++) {
        vector<int> dp(26, -1);
        for (int i = 0; i < n; i++) {
            string name = names[i];
            int back = name.back() - 'a';
            int front = name.front() - 'a';

            if (dp[front] != -1) {
                dp[back] = max(dp[back], dp[front] + (int)name.size());
            } else {
                if (front == c) {
                    dp[back] = (int) name.size();
                }
            }
        }

        ans = max(ans, dp[c]);
    }

    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n; cin >> n;
    vector<string> names(n);
    RA(names, n);

    int ans = 0;
    vvi dp(26, vi(26, 0));

    for (int i = 0; i < n; i++) {
        string name = names[i];
        int back = name.back() - 'a';
        int front = name.front() - 'a';

        for (int s = 0; s < 26; s++) {
            if (dp[s][front] != 0) dp[s][back] = max(dp[s][front] + (int)name.size(), dp[s][back]);
        }

        dp[front][back] = max((int)name.size(), dp[front][back]);
    }

    for (int i = 0; i < 26; i++) ans = max(ans, dp[i][i]);

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define REP(i,a,b) for (int i = a; i <= b; i++)
#define RA(v,n) REP(i, 0, n - 1) cin >> v[i]

template<typename T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n; cin >> n;
    vector<string> names(n);
    RA(names, n);

    int ans = 0;
    for (int c = 0; c < 26; c++) {
        vector<int> dp(26, -1);
        for (int i = 0; i < n; i++) {
            string name = names[i];
            int back = name.back() - 'a';
            int front = name.front() - 'a';

            if (dp[front] != -1) {
                dp[back] = max(dp[back], dp[front] + (int)name.size());
            } else {
                if (front == c) {
                    dp[back] = (int) name.size();
                }
            }
        }
        ans = max(ans, dp[c]);
    }

    cout << ans << endl;

    return 0;
}


#include<iostream>
using namespace std;
char names[500000][11],firstLetter,lastLetter;
int lengths[500000],endPoints[26][26],maxTillNow,ans;
int main()
{
	//freopen("input.txt","r",stdin);
	int n;
	cin>>n;
	for(int i=0;i<n;++i)
	{
		cin>>names[i];
		while(names[i][lengths[i]++]!=NULL);
		lengths[i]--;
	}
	for(int i=0;i<n;++i)
	{
		firstLetter = names[i][0];
		lastLetter = names[i][lengths[i]-1];
		maxTillNow=endPoints[lastLetter%97][firstLetter%97];
		if(maxTillNow)
		{
			if(maxTillNow + lengths[i] > endPoints[lastLetter%97][lastLetter%97])
			endPoints[lastLetter%97][lastLetter%97] = maxTillNow + lengths[i];
		}
		//cur word can also be the first word in the list
		if(lengths[i] > endPoints[firstLetter%97][lastLetter%97])
		endPoints[firstLetter%97][lastLetter%97] = lengths[i];
	}
	for(int i=0;i<26;++i)
	{
		if(endPoints[i][i]>ans)
		ans=endPoints[i][i];
	}
	cout<<ans<<endl;
	return 0;
}


#include<iostream>
using namespace std;
char names[500000][12],firstLetter,lastLetter;
int lengths[500000],endPoints[26][26],maxTillNow,ans;
int main()
{
	//freopen("input.txt","r",stdin);
	int n;
	cin>>n;
	for(int i=0;i<n;++i)
	{
		cin>>names[i];
		while(names[i][lengths[i]++]!=NULL);
		lengths[i]--;
		//cout<<lengths[i]<<endl;
	}
	for(int i=0;i<n;++i)
	{
		firstLetter = names[i][0];
		lastLetter = names[i][lengths[i]-1];
		for(int j=0;j<26;++j)
		{
			maxTillNow = endPoints[j][firstLetter%97];
			if(maxTillNow)
			{
				if(maxTillNow + lengths[i] > endPoints[j][lastLetter%97])
				endPoints[j][lastLetter%97] = maxTillNow + lengths[i];
			}
		}
		//cur word can also be the first word in the list
		if(lengths[i] > endPoints[firstLetter%97][lastLetter%97])
		endPoints[firstLetter%97][lastLetter%97] = lengths[i];
	}
	for(int i=0;i<26;++i)
	{
		if(endPoints[i][i]>ans)
		ans=endPoints[i][i];
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define boost_io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define int long long
#define num(a) a-'0'
#define charr(a) (a - 'A' + 1)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define maxs(a,b) if(b>a)a=b
#define mins(a,b) if(b<a)a=b
#define bin1(a) __builtin_popcount(a)
#define debug(x) cerr<<"["<<#x<<":"<<x<<"] "
#define debug2(a,b) debug(a);debug(b)
#define debug3(a,b,c) debug2(a,b);debug(c)
#define de cerr
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/(__gcd(a,b))
#define print(arr) for(auto it = arr.begin();it < arr.end();it ++){cout << *it << " ";}cout << ln;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define all(a) (a).begin(), (a).end()
#define vi vector<long long>
#define v vector
#define p pair
#define pii p<int,int>
#define pb push_back
#define mk make_pair
#define f first
#define s second
#define ln "\n"
typedef long double ld;
typedef double d;
using namespace std;
using namespace __gnu_pbds;
int modF=1e9+7;
int inf=1e18+5;

/*OUTPUT

*/

void solve(){
	int n;
	cin>>n;
	v<string> a(n);
	rep(i,0,n){
		cin>>a[i];
	}
	vi dp(n,0);
	v<vi> d(26,vi(26,0));
	for(int i=0;i<n;i++){
		if(i==0){
			int l=a[i].length();
			if(a[i][0]==a[i][l-1]){
				dp[i]=l;
			}
			else{
				dp[i]=0;
			}
			d[a[i][0]-'a'][a[i][l-1]-'a']=l;
		}
		else{
			int l=a[i].length();
			if(d[a[i][l-1]-'a'][a[i][0]-'a']>0 || a[i][l-1]==a[i][0]){
				dp[i]=l+d[a[i][l-1]-'a'][a[i][0]-'a'];
				// debug(d[a[i][l-1]-'a'][a[i][0]-'a']);
				// debug2(a[i][l-1]-'a',a[i][0]-'a');
			}
			else{
				dp[i]=0;
			}
			vi h(26);
			d[a[i][0]-'a'][a[i][l-1]-'a']=l;
			for(int j=0;j<26;j++){
				if(d[j][a[i][0]-'a']>0){
					maxs(h[j],max(d[j][a[i][l-1]-'a'],l+d[j][a[i][0]-'a']));
				}
				else{
					maxs(h[j],d[j][a[i][l-1]-'a']);
				}
			}
			for(int j=0;j<26;j++){
				d[j][a[i][l-1]-'a']=h[j];
			}
		}
		// debug(dp[i]);
	}
	int ans=0;
	rep(i,0,n){
		maxs(ans,dp[i]);
	}
	cout<<ans<<ln;
}

signed main(){
	boost_io;
	int t=1;
	// cin>>t;
	while(t--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define boost_io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define int long long
#define num(a) a-'0'
#define charr(a) (a - 'A' + 1)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define maxs(a,b) if(b>a)a=b
#define mins(a,b) if(b<a)a=b
#define bin1(a) __builtin_popcount(a)
#define debug(x) cerr<<"["<<#x<<":"<<x<<"] "
#define debug2(a,b) debug(a);debug(b)
#define debug3(a,b,c) debug2(a,b);debug(c)
#define de cerr
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/(__gcd(a,b))
#define print(arr) for(auto it = arr.begin();it < arr.end();it ++){cout << *it << " ";}cout << ln;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define all(a) (a).begin(), (a).end()
#define vi vector<long long>
#define v vector
#define p pair
#define pii p<int,int>
#define pb push_back
#define mk make_pair
#define f first
#define s second
#define ln "\n"
typedef long double ld;
typedef double d;
using namespace std;
using namespace __gnu_pbds;
int modF=1e9+7;
int inf=1e18+5;

/*OUTPUT

*/

void solve(){
	int n;
	cin>>n;
	v<string> a(n);
	rep(i,0,n){
		cin>>a[i];
	}
	vi dp(n);
	v<vi> d(26,vi(26));
	// dp[i] is max length with ith string as last string.
	// d[i][j] is max length of dyn. name starting with ith char and end with jth char.
	// till now
	// if(d[a[i][l-1]-'a'][a[i][0]-'a']!=0) dp[i] = d[a[i][l-1]-'a'][a[i][0]-'a'] + 1;
	// else dp[i]=0;
	// d[a[i][o]-'a'] [a[i][l-1]-'a']
	// for all k d[k][a[i][l-1]-'a'] is affected

	for(int i=0;i<n;i++){
		int l=a[i].length();
		if(i==0){
			if(a[i][0]==a[i][l-1]){
				dp[i]=l;
			}
			else{
				dp[i]=0;
			}
			d[a[i][0]-'a'][a[i][l-1]-'a']=l;
		}
		else{
			if(d[a[i][l-1]-'a'][a[i][0]-'a']!=0){
				dp[i]=l+d[a[i][l-1]-'a'][a[i][0]-'a'];
			}
			else{
				if(a[i][l-1]==a[i][0]){
					dp[i]=l;
				}
			}
			vi h(26,0);
			maxs(h[a[i][0]-'a'],l);
			for(int j=0;j<26;j++){
				if(d[j][a[i][0]-'a']!=0){
					maxs(h[j],l+d[j][a[i][0]-'a']);
				}
			}
			for(int j=0;j<26;j++){
				maxs(d[j][a[i][l-1]-'a'],h[j]);
			}
		}
	}
	int ans=0;
	for(int i=0;i<n;i++){
		maxs(ans,dp[i]);
	}
	cout<<ans<<ln;

}

signed main(){
	boost_io;
	int t=1;
	// cin>>t;
	while(t--){
		solve();
	}
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

inline void swap(double &a, double &b)
{
    double t = a;
    a = b;
    b = t;
}

int main()
{
    int a, b, c;
    while(scanf("%d%d%d", &a, &b, &c) != EOF)
    {
        if(a)
        {
            double dt = b*b-4*a*c;
            if(dt < 0)
            {
                puts("0");
            }
            else if(dt == 0)
            {
                puts("1");
                if(b)
                {
                    printf("%lf\n", -0.5*b/a);
                }
                else
                {
                    puts("0.000000");
                }
            }
            else
            {
                puts("2");
                dt = sqrt(dt);
                double x1 = 1.0*(-b-dt)/(2*a);
                double x2 = 1.0*(-b+dt)/(2*a);
                if(x1 > x2)
                {
                    swap(x1, x2);
                }
                printf("%lf\n", x1);
                printf("%lf\n", x2);
            }
        }
        else
        {
            if(b)
            {
                puts("1");
                printf("%lf\n", -1.0*c/b);
            }
            else
            {
                if(c)
                {
                    puts("0");
                }
                else
                {
                    puts("-1");
                }
            }
        }
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

inline void swap(double &a, double &b)
{
    double t = a;
    a = b;
    b = t;
}

int main()
{
    double a, b, c;
    while(scanf("%lf%lf%lf", &a, &b, &c) != EOF)
    {
        if(a)
        {
            double dt = b*b-4*a*c;
            if(dt < 0)
            {
                puts("0");
            }
            else if(dt == 0)
            {
                puts("1");
                if(b)
                {
                    printf("%f\n", -0.5*b/a);
                }
                else
                {
                    puts("0.000000");
                }
            }
            else
            {
                puts("2");
                dt = sqrt(dt);
                double x1 = 1.0*(-b-dt)/(2*a);
                double x2 = 1.0*(-b+dt)/(2*a);
                if(x1 > x2)
                {
                    swap(x1, x2);
                }
                printf("%f\n", x1);
                printf("%f\n", x2);
            }
        }
        else
        {
            if(b)
            {
                puts("1");
                printf("%f\n", -1.0*c/b);
            }
            else
            {
                if(c)
                {
                    puts("0");
                }
                else
                {
                    puts("-1");
                }
            }
        }
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

inline void swap(double &a, double &b)
{
    double t = a;
    a = b;
    b = t;
}

int main()
{
    int a, b, c;
    while(scanf("%d%d%d", &a, &b, &c) != EOF)
    {
        if(a)
        {
            double dt = b*b-4*a*c;
            if(dt < 0)
            {
                puts("0");
            }
            else if(dt == 0)
            {
                puts("1");
                if(b)
                {
                    printf("%f\n", -0.5*b/a);
                }
                else
                {
                    puts("0.000000");
                }
            }
            else
            {
                puts("2");
                dt = sqrt(dt);
                double x1 = 1.0*(-b-dt)/(2*a);
                double x2 = 1.0*(-b+dt)/(2*a);
                if(x1 > x2)
                {
                    swap(x1, x2);
                }
                printf("%f\n", x1);
                printf("%f\n", x2);
            }
        }
        else
        {
            if(b)
            {
                puts("1");
                printf("%f\n", -1.0*c/b);
            }
            else
            {
                if(c)
                {
                    puts("0");
                }
                else
                {
                    puts("-1");
                }
            }
        }
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

inline void swap(double &a, double &b)
{
    double t = a;
    a = b;
    b = t;
}

int main()
{
    int a, b, c;
    while(scanf("%d%d%d", &a, &b, &c) != EOF)
    {
        if(a)
        {
            double dt = b*b-4*a*c;
            if(dt < 0)
            {
                puts("0");
            }
            else if(dt == 0)
            {
                puts("1");
                if(b)
                {
                    printf("%f\n", -0.5*b/a);
                }
                else
                {
                    puts("0.000000");
                }
            }
            else
            {
                puts("2");
                dt = sqrt(dt);
                double x1 = 1.0*(-b-dt)/(2*a);
                double x2 = 1.0*(-b+dt)/(2*a);
                if(x1 > x2)
                {
                    swap(x1, x2);
                }
                printf("%lf\n", x1);
                printf("%lf\n", x2);
            }
        }
        else
        {
            if(b)
            {
                puts("1");
                printf("%f\n", -1.0*c/b);
            }
            else
            {
                if(c)
                {
                    puts("0");
                }
                else
                {
                    puts("-1");
                }
            }
        }
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

inline void swap(double &a, double &b)
{
    double t = a;
    a = b;
    b = t;
}

int main()
{
    int a, b, c;
    while(scanf("%d%d%d", &a, &b, &c) != EOF)
    {
        if(a)
        {
            double dt = 1.0*(b*b-4*a*c);
            if(dt < 0)
            {
                puts("0");
            }
            else if(dt == 0)
            {
                puts("1");
                if(b)
                {
                    printf("%f\n", -0.5*b/a);
                }
                else
                {
                    puts("0.000000");
                }
            }
            else
            {
                puts("2");
                dt = sqrt(dt);
                double x1 = 1.0*(-b-dt)/(2*a);
                double x2 = 1.0*(-b+dt)/(2*a);
                if(x1 > x2)
                {
                    swap(x1, x2);
                }
                printf("%f\n", x1);
                printf("%f\n", x2);
            }
        }
        else
        {
            if(b)
            {
                puts("1");
                printf("%f\n", -1.0*c/b);
            }
            else
            {
                if(c)
                {
                    puts("0");
                }
                else
                {
                    puts("-1");
                }
            }
        }
    }
    return 0;
}


#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <stack>
using namespace std;
int main(){
  double a,b,c;
  cin>>a>>b>>c;
  if(a==0&&b!=0&&c!=0){
    double x=-c/b;
    cout<<1<<endl;
    printf("%.10f",x);
    return 0;
  }
  if(a!=0&&b==0&&c!=0){
    if(-c/a<0){
      cout<<0;return 0;
    }
    else{
      cout<<2<<endl;
      double x2=sqrt(-c/a);
      double x1=0-x2;
       printf("%.10f",x1);
       printf("%.10f",x2);
      return 0;
    }
  }
  if(a!=0&&b!=0&&c==0){
    cout<<2<<endl;
    double x1=0.0;
    double x2=-b/a;
    if(x1<x2){
     printf("%.10f",x1);
    cout<<endl;
     printf("%.10f",x2);
    }
    else{
       printf("%.10f",x2);
    cout<<endl;
     printf("%.10f",x1);
      }
  }
  if(a==0&&b==0&&c!=0){
    cout<<0;
    return 0;
  }
  if((a==0&&c==0&&b!=0)||(a!=0&&b==0&&c==0)){
    double x=0.0;
    cout<<1<<endl;
     printf("%.10f",x);
    return 0;
    }
  if(a==0&&b==0&&c==0){
    cout<<-1;
    return 0;
  }

  double d=b*b-4*a*c;

  if(d<0){
    cout<<0;
    return 0;
  }
  if(d==0){
    cout<<1<<endl;
    double x=-b/(2*a);
    printf("%.10f",x);
  }
  if(d>0){
    cout<<2<<endl;
    double x1=(-b-sqrt(d))/(2*a);
    double x2=(-b+sqrt(d))/(2*a);
    if(x1<x2){
      printf("%.10f",x1);
      cout<<endl;
      printf("%.10f",x2);
    }
    else{
      printf("%.10f",x2);
      cout<<endl;
      printf("%.10f",x1);
      }
  }
}


#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <stack>
using namespace std;
int main(){
  double a,b,c;
  cin>>a>>b>>c;
  if(a==0&&b!=0&&c!=0){
    double x=-c/b;
    cout<<1<<endl;
    printf("%.10f",x);
    return 0;
  }
  if(a!=0&&b==0&&c!=0){
    if(-c/a<0){
      cout<<0;return 0;
    }
    else{
      cout<<2<<endl;
      double x=sqrt(-c/a);
       printf("%.10f",x);
      cout<<endl;
      x=0-x;
       printf("%.10f",x);
      return 0;
    }
  }
  if(a!=0&&b!=0&&c==0){
    cout<<2<<endl;
    double x1=0.0;
    double x2=-b/a;
     printf("%.10f",x1);
    cout<<endl;
     printf("%.10f",x2);
  }
  if(a==0&&b==0&&c!=0){
    cout<<0;
    return 0;
  }
  if((a==0&&c==0&&b!=0)||(a!=0&&b==0&&c==0)){
    double x=0.0;
    cout<<1<<endl;
     printf("%.10f",x);
    return 0;
    }
  if(a==0&&b==0&&c==0){
    cout<<-1;
    return 0;
  }

  int d=b*b-4*a*c;
  if(d<0){
    cout<<0;
    return 0;
  }
  if(d==0){
    cout<<1<<endl;
    double x=-b/(2*a);
    printf("%.10f",x);
  }
  if(d>0){
    cout<<2<<endl;
    double x1=(-b-sqrt(d))/(2*a);
    double x2=(-b+sqrt(d))/(2*a);
    printf("%.10f",x1);
    cout<<endl;
    printf("%.10f",x2);
  }
}


#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <stack>
using namespace std;
int main(){
  double a,b,c;
  cin>>a>>b>>c;
  int d=b*b-4*a*c;
  if(d<0){
    cout<<0;
    return 0;
  }
  if(d==0){
    cout<<1<<endl;
    double x=-b/(2*a);
    printf("%.10f",x);
  }
  if(d>0){
    cout<<2<<endl;
    double x1=(-b-sqrt(d))/(2*a);
    double x2=(-b+sqrt(d))/(2*a);
    printf("%.10f",x1);
    cout<<endl;
    printf("%.10f",x2);
  }
}


#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <stack>
using namespace std;
int main(){
  double a,b,c;
  cin>>a>>b>>c;
  if(a==0&&b!=0&&c!=0){
    double x=-c/b;
    cout<<1<<endl;
    printf("%.10f",x);
    return 0;
  }
  if(a!=0&&b==0&&c!=0){
    if(-c/a<0){
      cout<<0;return 0;
    }
    else{
      cout<<2<<endl;
      double x2=sqrt(-c/a);
      double x1=0-x2;
       printf("%.10f",x1)
       printf("%.10f",x2);
      return 0;
    }
  }
  if(a!=0&&b!=0&&c==0){
    cout<<2<<endl;
    double x1=0.0;
    double x2=-b/a;
    if(x1<x2){
     printf("%.10f",x1);
    cout<<endl;
     printf("%.10f",x2);
    }
    else{
       printf("%.10f",x2);
    cout<<endl;
     printf("%.10f",x1);
      }
  }
  if(a==0&&b==0&&c!=0){
    cout<<0;
    return 0;
  }
  if((a==0&&c==0&&b!=0)||(a!=0&&b==0&&c==0)){
    double x=0.0;
    cout<<1<<endl;
     printf("%.10f",x);
    return 0;
    }
  if(a==0&&b==0&&c==0){
    cout<<-1;
    return 0;
  }

  double d=b*b-4*a*c;

  if(d<0){
    cout<<0;
    return 0;
  }
  if(d==0){
    cout<<1<<endl;
    double x=-b/(2*a);
    printf("%.10f",x);
  }
  if(d>0){
    cout<<2<<endl;
    double x1=(-b-sqrt(d))/(2*a);
    double x2=(-b+sqrt(d))/(2*a);
    if(x1<x2){
      printf("%.10f",x1);
      cout<<endl;
      printf("%.10f",x2);
    }
    else{
      printf("%.10f",x2);
      cout<<endl;
      printf("%.10f",x1);
      }
  }
}


#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <stack>
using namespace std;
int main(){
  double a,b,c;
  cin>>a>>b>>c;
  if(a==0&&b!=0&&c!=0){
    double x=-c/b;
    cout<<1<<endl;
    printf("%.10f",x);
    return 0;
  }
  if(a!=0&&b==0&&c!=0){
    if(-c/a<0){
      cout<<0;return 0;
    }
    else{
      cout<<2<<endl;
      double x=sqrt(-c/a);
       printf("%.10f",x);
      cout<<endl;
      x=0-x;
       printf("%.10f",x);
      return 0;
    }
  }
  if(a!=0&&b!=0&&c==0){
    cout<<2<<endl;
    double x1=0.0;
    double x2=-b/a;
     printf("%.10f",x1);
    cout<<endl;
     printf("%.10f",x2);
  }
  if(a==0&&b==0&&c!=0){
    cout<<0;
    return 0;
  }
  if((a==0&&c==0&&b!=0)||(a!=0&&b==0&&c==0)){
    double x=0.0;
    cout<<1<<endl;
     printf("%.10f",x);
    return 0;
    }
  if(a==0&&b==0&&c==0){
    cout<<-1;
    return 0;
  }

  double d=b*b-4*a*c;

  if(d<0){
    cout<<0;
    return 0;
  }
  if(d==0){
    cout<<1<<endl;
    double x=-b/(2*a);
    printf("%.10f",x);
  }
  if(d>0){
    cout<<2<<endl;
    double x1=(-b-sqrt(d))/(2*a);
    double x2=(-b+sqrt(d))/(2*a);
    printf("%.10f",x1);
    cout<<endl;
    printf("%.10f",x2);
  }
}