#include <bits/stdc++.h>

#define REP(i, a) for( int i = 0; i < a; i++ )
#define RFOR(i,x,y) for(int i = x; i>=y; i--)
#define FOR(i,x,y) for (int i = x; i < y; i++)
#define ITFOR(it,A) for(typeof A.begin() it = A.begin(); it!=A.end(); it++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define VE vector <int>
#define mset(A,x) memset(A, x, sizeof A)
#define PB push_back
#define ones(x) __builtin_popcount(x)
#define cua(x) (x)*(x)
#define debug(x) cout <<#x << " = " << x << endl
#define adebug(x,n) cout <<#x<<endl; REP(i,n)cout<<x[i]<<char(i+1==n?10:32)
#define mdebug(x,m,n) cout <<#x<<endl; REP(i,m)REP(j,n)cout<<x[i][j]<<char(j+1==n?10:32)
#define LSOne(S) (S&(-S))

using namespace std;

#define ll long long
#define lli long long int
#define PI acos(-1.0)
#define ii pair<int,int>
#define inf_ll (((1LL<<62)-1)<<1)+1
#define inf_i (1<<30)-1

int main() {
	int n;
	string cad1,cad2,t1,t2;
	scanf("%d",&n);
	cin.ignore();
	getline(cin,cad1);
	getline(cin,cad2);
	int cm=n,cs=0,freq1[10]={0},freq2[10]={0};
	REP(i,n)
		freq1[cad1[i]-'0']++,freq2[cad2[i]-'0']++;

	REP(i,10)
	{
		if(freq2[i] && freq1[i])
		{
			freq2[i]--;
			freq1[i]--;
			i--;
			cm--;
		}
	}

	t1="";
	REP(i,10)
		if(freq1[i])
			t1+=(i+'0');
	reverse(all(t1));
	t2="";
	REP(i,10)
		if(freq2[i])
			t2+=(i+'0');
	REP(i,cm)
		if(t1[i]<t2[i])
			cm--;

	REP(i,n)
		freq1[cad1[i]-'0']++,freq2[cad2[i]-'0']++;

	RFOR(i,9,0)
	{
		if(freq1[i])
		{
			FOR(j,i+1,10)
			{
				if(freq2[j])
				{
					freq1[i]--,freq2[j]--,cs++;
					i++;
					break;
				}
			}
		}
	}
	printf("%d\n%d\n",cm,cs);
	return 0;
}


#include <bits/stdc++.h>

#define REP(i, a) for( int i = 0; i < a; i++ )
#define RFOR(i,x,y) for(int i = x; i>=y; i--)
#define FOR(i,x,y) for (int i = x; i < y; i++)
#define ITFOR(it,A) for(typeof A.begin() it = A.begin(); it!=A.end(); it++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define VE vector <int>
#define mset(A,x) memset(A, x, sizeof A)
#define PB push_back
#define ones(x) __builtin_popcount(x)
#define cua(x) (x)*(x)
#define debug(x) cout <<#x << " = " << x << endl
#define adebug(x,n) cout <<#x<<endl; REP(i,n)cout<<x[i]<<char(i+1==n?10:32)
#define mdebug(x,m,n) cout <<#x<<endl; REP(i,m)REP(j,n)cout<<x[i][j]<<char(j+1==n?10:32)
#define LSOne(S) (S&(-S))

using namespace std;

#define ll long long
#define lli long long int
#define PI acos(-1.0)
#define ii pair<int,int>
#define inf_ll (((1LL<<62)-1)<<1)+1
#define inf_i (1<<30)-1

int main() {
	int n;
	string cad1,cad2;
	scanf("%d",&n);
	cin.ignore();
	getline(cin,cad1);
	getline(cin,cad2);
	int cm=n,cs=0,freq1[10]={0},freq2[10]={0};
	REP(i,n)
		freq1[cad1[i]-'0']++,freq2[cad2[i]-'0']++;
	REP(i,10)
	{
		if(freq2[i] && freq1[i])
		{
			freq2[i]--;
			freq1[i]--;
			cm--;
		}
	}
	RFOR(i,9,0)
	{
		if(freq1[i])
		{
			bool flag=false;
			RFOR(j,i-1,0)
			{
				if(freq2[j])
				{
					freq1[i]--,freq2[j]--;
					i++;
					flag=true;
					break;
				}
			}
			if(!flag)
				cm--;
		}
	}

	REP(i,n)
		freq1[cad1[i]-'0']++,freq2[cad2[i]-'0']++;
	RFOR(i,9,0)
	{
		if(freq1[i])
		{
			FOR(j,i+1,10)
			{
				if(freq2[j])
				{
					freq1[i]--,freq2[j]--,cs++;
					i++;
					break;
				}
			}
		}
	}
	printf("%d\n%d\n",cm,cs);
	return 0;
}


#include <bits/stdc++.h>

#define REP(i, a) for( int i = 0; i < a; i++ )
#define RFOR(i,x,y) for(int i = x; i>=y; i--)
#define FOR(i,x,y) for (int i = x; i < y; i++)
#define ITFOR(it,A) for(typeof A.begin() it = A.begin(); it!=A.end(); it++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define VE vector <int>
#define mset(A,x) memset(A, x, sizeof A)
#define PB push_back
#define ones(x) __builtin_popcount(x)
#define cua(x) (x)*(x)
#define debug(x) cout <<#x << " = " << x << endl
#define adebug(x,n) cout <<#x<<endl; REP(i,n)cout<<x[i]<<char(i+1==n?10:32)
#define mdebug(x,m,n) cout <<#x<<endl; REP(i,m)REP(j,n)cout<<x[i][j]<<char(j+1==n?10:32)
#define LSOne(S) (S&(-S))

using namespace std;

#define ll long long
#define lli long long int
#define PI acos(-1.0)
#define ii pair<int,int>
#define inf_ll (((1LL<<62)-1)<<1)+1
#define inf_i (1<<30)-1

int main() {
	int n;
	string cad1,cad2,t1,t2;
	scanf("%d",&n);
	cin.ignore();
	getline(cin,cad1);
	getline(cin,cad2);
	t1=cad1;t2=cad2;
	sort(all(t1));
	sort(all(t2));
	int cm=0;
	REP(i,n)
		if(t1[i]>t2[i])
			cm++;

	int cs=0,freq1[10]={0},freq2[10]={0};
	REP(i,n)
		freq1[cad1[i]-'0']++,freq2[cad2[i]-'0']++;
	RFOR(i,9,0)
	{
		if(freq1[i])
		{
			FOR(j,i+1,10)
			{
				if(freq2[j])
				{
					freq1[i]--,freq2[j]--,cs++;
					i++;
					break;
				}
			}
		}
	}
	printf("%d\n%d\n",cm,cs);
	return 0;
}


#include <bits/stdc++.h>

#define REP(i, a) for( int i = 0; i < a; i++ )
#define RFOR(i,x,y) for(int i = x; i>=y; i--)
#define FOR(i,x,y) for (int i = x; i < y; i++)
#define ITFOR(it,A) for(typeof A.begin() it = A.begin(); it!=A.end(); it++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define VE vector <int>
#define mset(A,x) memset(A, x, sizeof A)
#define PB push_back
#define ones(x) __builtin_popcount(x)
#define cua(x) (x)*(x)
#define debug(x) cout <<#x << " = " << x << endl
#define adebug(x,n) cout <<#x<<endl; REP(i,n)cout<<x[i]<<char(i+1==n?10:32)
#define mdebug(x,m,n) cout <<#x<<endl; REP(i,m)REP(j,n)cout<<x[i][j]<<char(j+1==n?10:32)
#define LSOne(S) (S&(-S))

using namespace std;

#define ll long long
#define lli long long int
#define PI acos(-1.0)
#define ii pair<int,int>
#define inf_ll (((1LL<<62)-1)<<1)+1
#define inf_i (1<<30)-1

int main() {
	int n;
	string cad1,cad2,t1,t2;
	scanf("%d",&n);
	cin.ignore();
	getline(cin,cad1);
	getline(cin,cad2);
	int cm=n,cs=0,freq1[10]={0},freq2[10]={0};
	REP(i,n)
		freq1[cad1[i]-'0']++,freq2[cad2[i]-'0']++;

	REP(i,10)
	{
		if(freq2[i] && freq1[i])
		{
			freq2[i]--;
			freq1[i]--;
			i--;
			cm--;
		}
	}

	t1="";
	REP(i,10)
		if(freq1[i])
			t1+=(i+'0'),i--;
	reverse(all(t1));
	t2="";
	REP(i,10)
		if(freq2[i])
			t2+=(i+'0'),i--;
	REP(i,cm)
		if(t1[i]<t2[i])
			cm--;

	REP(i,n)
		freq1[cad1[i]-'0']++,freq2[cad2[i]-'0']++;

	RFOR(i,9,0)
	{
		if(freq1[i])
		{
			FOR(j,i+1,10)
			{
				if(freq2[j])
				{
					freq1[i]--,freq2[j]--,cs++;
					i++;
					break;
				}
			}
		}
	}
	printf("%d\n%d\n",cm,cs);
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define PI 3.1415926535897932384626433832
#define MOD 1000000007
#define MOD2 1000000009
#define bas 29
#define bas2 19
using namespace std;
int s[11];
int t[11];
int main() {
    string sssss , ttttt;
    int n ;
    cin>>n;
    cin>>sssss>>ttttt;
    for(int i = 0 ; i < sssss.size() ; i++){
        s[sssss[i] - '0']++;
        t[ttttt[i] - '0']++;
    }
    int tt[11];
    int ss[11];
    for(int i = 0 ; i < 11 ; i++){
        ss[i] = s[i];
        tt[i] = t[i];
    }
    int maxhit= 0;
    int cou = 0;
    for(int i = 9 ; i >= 0 ; i--){
        if(tt[i] == 0)continue;
        bool found = false;
        for(int j = i - 1 ; j >= 0 ; j--){
            if(ss[j] > 0){
                found = true;
                ss[j]--;
                tt[i]--;
                cou++;
                maxhit++;
                break;
            }
        }
        if(found){
                if(tt[i] > 0)i++;
                continue;
        }
        for(int j = 9 ; j >= 0 ; j--){
            if(ss[j] > 0){
                ss[j]--;
                tt[i]--;
                cou++;
            }
        }
        if(tt[i] > 0)i++;
        if(cou == n)break;

    }
    for(int i = 0 ; i < 11 ; i++){
        ss[i] = s[i];
        tt[i] = t[i];
    }
    cou = 0;
    int minhit = 0;
    for(int i = 9 ; i >= 0 ; i--){
        if(tt[i] == 0)continue;
        bool found = false;
        for(int j = i ; j >= 0 ; j--){
            if(ss[j] > 0){
                found = true;
                ss[j]--;
                tt[i]--;
                cou++;
                break;
            }
        }
        if(found){
                if(tt[i] > 0)i++;
                continue;
        }else minhit++;
        for(int j = 9 ; j >= 0 ; j--){
            if(ss[j] > 0){
                ss[j]--;
                tt[i]--;
                cou++;
            }
        }
        if(tt[i] > 0)i++;
        if(cou == n)break;
    }
    cout<<minhit<<endl<<maxhit<<endl;
}


#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define PI 3.1415926535897932384626433832
#define MOD 1000000007
#define MOD2 1000000009
#define bas 29
#define bas2 19
using namespace std;
int s[11];
int t[11];
int main() {
    string sssss , ttttt;
    int n ;
    cin>>n;
    cin>>sssss>>ttttt;
    for(int i = 0 ; i < sssss.size() ; i++){
        s[sssss[i] - '0']++;
        t[ttttt[i] - '0']++;
    }
    int tt[11];
    int ss[11];
    for(int i = 0 ; i < 11 ; i++){
        ss[i] = s[i];
        tt[i] = t[i];
    }
    int maxhit= 0;
    int cou = 0;
    for(int i = 9 ; i >= 0 ; i--){
        if(tt[i] == 0)continue;
        bool found = false;
        for(int j = i - 1 ; j >= 0 ; j--){
            if(ss[j] > 0){
                found = true;
                ss[j]--;
                tt[i]--;
                cou++;
                maxhit++;
                break;
            }
        }
        if(found){
                if(tt[i] > 0)i++;
                continue;
        }
        for(int j = 9 ; j >= 0 ; j--){
            if(ss[j] > 0){
                ss[j]--;
                tt[i]--;
                cou++;
            }
        }
        if(tt[i] > 0)i++;
        if(cou == n)break;

    }
    for(int i = 0 ; i < 11 ; i++){
        ss[i] = s[i];
        tt[i] = t[i];
    }
    int minhit = 0;
    for(int i = 9 ; i >= 0 ; i--){
        if(tt[i] == 0)continue;
        bool found = false;
        for(int j = i ; j >= 0 ; j--){
            if(ss[j] > 0){
                found = true;
                ss[j]--;
                tt[i]--;
                break;
            }
        }
        if(found){
                if(tt[i] > 0)i++;
                continue;
        }else minhit++;
        for(int j = 9 ; j >= 0 ; j--){
            if(ss[j] > 0){
                ss[j]--;
                tt[i]--;
            }
        }
        if(tt[i] > 0)i++;
    }
    cout<<minhit<<endl<<maxhit<<endl;
}


#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define PI 3.1415926535897932384626433832
#define MOD 1000000007
#define MOD2 1000000009
#define bas 29
#define bas2 19
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n ;
    cin>>n;
    int ma = 0 ;
    int mi = 1000000000;
    string x , y ;
    cin>>x>>y;
    vector<int> a(1000,0);
    vector<int> b(1000,0);
    vector<int> aa(1000,0);
    vector<int> bb(1000,0);

    for(int i = 0 ;i < x.size() ; i++){
        a[x[i]]++;
        b[y[i]]++;

    }
    aa = a;
    bb = b;
    int mo = 0 ;
    int sh = 0 ;
    for(int i = 0 ;i < n ; i++){
        for(int j = '0' ; j <= '9' ; j++){
            if(b[j] > 0){

                for(int k = '0' ; k <='9' ; k++){
                    if(a[k] > 0){
                        if(k == j){
                            a[k]--;
                            break;
                        }
                        else if(j > k){
                            mo++;
                            a[k]--;
                        }else{
                            sh++;
                            a[k]--;
                        }
                        break;
                    }
                }
                b[j]--;
                break;

            }
        }

    }
    a = aa;
    b = bb;
    int sh1 = 0 ;
    int mo1 = 0 ;
    for(int i = 0 ;i < n ; i++){
        for(int j = '9' ; j >='0' ; j--){
            if(b[j] > 0){
            bool is = true;
                for(int k = j - 1 ; k >='0' ; k--){
                    if(a[k] > 0){

                        if(j > k){
                            mo1++;
                            a[k]--;
                            is = false;
                        }
                        break;
                    }
                }
                if(is){
                for(int k = j ; k <='9' ; k++){
                    if(a[k] > 0){
                        a[k]--;
                        break;
                    }
                }

                }

                b[j]--;
                break;

            }
        }

    }
    cout<<sh<<endl;
    cout<<mo1;
}


#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define PI 3.1415926535897932384626433832
#define MOD 1000000007
#define MOD2 1000000009
#define bas 29
#define bas2 19
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n ;
    cin>>n;
    int ma = 0 ;
    int mi = 1000000000;
    string x , y ;
    cin>>x>>y;
    vector<int> a(1000,0);
    vector<int> b(1000,0);
    vector<int> aa(1000,0);
    vector<int> bb(1000,0);

    for(int i = 0 ;i < x.size() ; i++){
        a[x[i]]++;
        b[y[i]]++;

    }
    aa = a;
    bb = b;
    int mo = 0 ;
    int sh = 0 ;
    for(int i = 0 ;i < n ; i++){
        for(int j = '0' ; j <= '9' ; j++){
            if(b[j] > 0){

                for(int k = '0' ; k <='9' ; k++){
                    if(a[k] > 0){
                        if(k == j){
                            a[k]--;
                            break;
                        }
                        else if(j > k){
                            mo++;
                            a[k]--;
                        }else{
                            sh++;
                            a[k]--;
                        }
                        break;
                    }
                }
                b[j]--;
                break;

            }
        }

    }
    a = aa;
    b = bb;
    int sh1 = 0 ;
    int mo1 = 0 ;
    for(int i = 0 ;i < n ; i++){
        for(int j = '9' ; j >='0' ; j--){
            if(b[j] > 0){
            bool is = true;
                for(int k = j - 1 ; k >='0' ; k--){
                    if(a[k] > 0){

                        if(j > k){
                            mo1++;
                            a[k]--;
                            is = false;
                        }
                        break;
                    }
                }
                if(is){
                for(int k = j ; j <='9' ; k++){
                    if(a[k] > 0){
                        a[k]--;
                        break;
                    }
                }

                }

                b[j]--;
                break;

            }
        }

    }
    cout<<sh<<endl;
    cout<<mo1;
}


#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define PI 3.1415926535897932384626433832
#define MOD 1000000007
#define MOD2 1000000009
#define bas 29
#define bas2 19
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n ;
    cin>>n;
    int ma = 0 ;
    int mi = 1000000000;
    string x , y ;
    cin>>x>>y;
    vector<int> a(1000,0);
    vector<int> b(1000,0);
    vector<int> aa(1000,0);
    vector<int> bb(1000,0);

    for(int i = 0 ;i < x.size() ; i++){
        a[x[i]]++;
        b[y[i]]++;

    }
    aa = a;
    bb = b;
    int mo = 0 ;
    int sh = 0 ;
    for(int i = 0 ;i < n ; i++){
        for(int j = '9' ; j >='0' ; j--){
            if(b[j] > 0){
                bool is = true;
                for(int k = j ; k >='0' ; k--){
                    if(a[k] > 0){
                        if(k == j){
                            a[k]--;
                            is = true;
                            break;
                        }
                        else if(j > k){
                            mo++;
                            a[k]--;
                            is = false;
                        }
                        break;
                    }
                }
                if(is){
                    for(int k = j + 1 ; k <='9' ; k++){
                        if(a[k]>0){
                            sh++;
                            a[k]--;
                            break;
                        }
                    }
                }
                b[j]--;
                break;

            }
        }

    }
    a = aa;
    b = bb;
    int sh1 = 0 ;
    int mo1 = 0 ;
    for(int i = 0 ;i < n ; i++){
        for(int j = '9' ; j >='0' ; j--){
            if(b[j] > 0){
            bool is = true;
                for(int k = j - 1 ; k >='0' ; k--){
                    if(a[k] > 0){

                        if(j > k){
                            mo1++;
                            a[k]--;
                            is = false;
                        }
                        break;
                    }
                }
                if(is){
                for(int k = j ; k <='9' ; k++){
                    if(a[k] > 0){
                        a[k]--;
                        break;
                    }
                }

                }

                b[j]--;
                break;

            }
        }

    }
    cout<<sh<<endl;
    cout<<mo1;
}


#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define PI 3.1415926535897932384626433832
#define MOD 1000000007
#define MOD2 1000000009
#define bas 29
#define bas2 19
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n ;
    cin>>n;
    int ma = 0 ;
    int mi = 1000000000;
    string x , y ;
    cin>>x>>y;
    vector<int> a(1000,0);
    vector<int> b(1000,0);
    vector<int> aa(1000,0);
    vector<int> bb(1000,0);

    for(int i = 0 ;i < x.size() ; i++){
        a[x[i]]++;
        b[y[i]]++;

    }
    aa = a;
    bb = b;
    int mo = 0 ;
    int sh = 0 ;
    for(int i = 0 ;i < n ; i++){
        for(int j = '0' ; j <= '9' ; j++){
            if(b[j] > 0){

                for(int k = '0' ; k <='9' ; k++){
                    if(a[k] > 0){
                        if(k == j){
                            a[k]--;
                            break;
                        }
                        else if(j > k){
                            mo++;
                            a[k]--;
                        }else{
                            sh++;
                            a[k]--;
                        }
                        break;
                    }
                }
                b[j]--;
                break;

            }
        }

    }
    a = aa;
    b = bb;
    int sh1 = 0 ;
    int mo1 = 0 ;
    for(int i = 0 ;i < n ; i++){
        for(int j = '9' ; j >='0' ; j--){
            if(b[j] > 0){
            bool is = true;
                for(int k = j - 1 ; k >='0' ; k--){
                    if(a[k] > 0){

                        if(j > k){
                            mo1++;
                            a[k]--;
                            is = false;
                        }else{
                            sh++;
                            a[k]--;
                        }
                        break;
                    }
                }
                if(is){
                for(int k = j ; j <='9' ; j++){
                    if(a[k] > 0){
                        a[k]--;
                        break;
                    }
                }

                }

                b[j]--;
                break;

            }
        }

    }
    cout<<sh<<endl;
    cout<<mo1;
}


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

int main(){
  int s;
  cin >> s;
  string sher,mori;
  cin >> sher >> mori;
  //int s=sher.length();
  //int m=mori.length();
  int sh[10],mo[10];
  memset(sh,0,sizeof(sh));
  memset(mo,0,sizeof(mo));
  for(int i=0;i<s;i++){
    sh[sher[i]-'0']++;
  }
  for(int i=0;i<s;i++){
    mo[mori[i]-'0']++;
  }
  int mor=0;
  for(int i=0;i<s;i++){
    int a=(int)(sher[i]-'0');
    for(int i=0;i<=9;i++){
      if(i>=a){
         if(mo[i]){
           mor++;
           mo[i]--;
         }
      }
    }
  }
  int ans1=s-mor;
  for(int i=0;i<s;i++){
    mo[mori[i]-'0']++;
  }
  mor=0;
  for(int i=0;i<s;i++){
    int a=(int)(sher[i]-'0');
    for(int i=0;i<=9;i++){
      if(i>a){
    //  cout << "h" << a << endl;
         if(mo[i]){
           mor++;
      //     cout << i << "and" << a << endl;
           mo[i]--;
           break;
         }
      }
    }
  }
  cout << ans1 << endl << mor << endl;
  return 0;
}


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

int main(){
  int s;
  cin >> s;
  string sher,mori;
  cin >> sher >> mori;
  //int s=sher.length();
  //int m=mori.length();
  int sh[10],mo[10];
  memset(sh,0,sizeof(sh));
  memset(mo,0,sizeof(mo));
  for(int i=0;i<s;i++){
    sh[sher[i]-'0']++;
  }
  for(int i=0;i<s;i++){
    mo[mori[i]-'0']++;
  }
  int mor=0;
  for(int i=0;i<s;i++){
    int a=(int)(sher[i]-'0');
    for(int i=0;i<=9;i++){
      if(i>=a){
         if(mo[i]){
           mor++;
           mo[i]--;
         }
      }
    }
  }
  int ans1=s-mor;
   for(int i=0;i<s;i++){
    mo[mori[i]-'0']++;
  }
  mor=0;
  for(int i=0;i<s;i++){
    int a=(int)(sher[i]-'0');
    for(int i=0;i<=9;i++){
      if(i>a){
         if(mo[i]){
           mor++;
           mo[i]--;
         }
      }
    }
  }
  cout << ans1 << endl << mor << endl;
  return 0;
}


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

int main(){
  int s;
  cin >> s;
  string sher,mori;
  cin >> sher >> mori;
  //int s=sher.length();
  //int m=mori.length();
  int sh[10],mo[10];
  memset(sh,0,sizeof(sh));
  memset(mo,0,sizeof(mo));
  for(int i=0;i<s;i++){
    sh[sher[i]-'0']++;
  }
  for(int i=0;i<s;i++){
    mo[mori[i]-'0']++;
  }
  int mor=0;
  for(int i=0;i<s;i++){
    int a=(int)(sher[i]-'0');
    for(int i=0;i<=9;i++){
      if(i>=a){
         if(mo[i]){
           mor++;
           mo[i]--;
           break;
         }
      }
    }
  }
  int ans1=s-mor;
  for(int i=0;i<s;i++){
    mo[mori[i]-'0']++;
  }
  mor=0;
  for(int i=0;i<s;i++){
    int a=(int)(sher[i]-'0');
    for(int i=0;i<=9;i++){
      if(i>a){
    //  cout << "h" << a << endl;
         if(mo[i]){
           mor++;
      //     cout << i << "and" << a << endl;
           mo[i]--;
           break;
         }
      }
    }
  }
  cout << ans1 << endl << mor << endl;
  return 0;
}


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

int main(){
  int s;
  cin >> s;
  string sher,mori;
  cin >> sher >> mori;
  //int s=sher.length();
  //int m=mori.length();
  int sh[10],mo[10];
  memset(sh,0,sizeof(sh));
  memset(mo,0,sizeof(mo));
  for(int i=0;i<s;i++){
    sh[sher[i]-'0']++;
  }
  for(int i=0;i<s;i++){
    mo[mori[i]-'0']++;
  }
  int mor=0;
  for(int i=0;i<s;i++){
    int a=(int)(sher[i]-'0');
    for(int i=0;i<=9;i++){
      if(i>=a){
         if(mo[i]){
           mor++;
           mo[i]--;
           break;
         }
      }
    }
  }
  int ans1=s-mor;
  memset(mo,0,sizeof(mo));
  for(int i=0;i<s;i++){
    mo[mori[i]-'0']++;
  }
  mor=0;
  for(int i=0;i<s;i++){
    int a=(int)(sher[i]-'0');
    for(int i=0;i<=9;i++){
      if(i>a){
         if(mo[i]){
           mor++;
           mo[i]--;
           break;
         }
      }
    }
  }
  cout << ans1 << endl << mor << endl;
  return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 1005
char S[maxn];
char M[maxn];

int cmp(int x,int y)
{
    return x > y;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        scanf("%s%s",S,M);
        sort(S,S+n,cmp);
        sort(M,M+n,cmp);
        int ans1 = 0,ans2 = 0;
        int k = n-1;
        for(int i = n-1; i >= 0; i--)
        {
            for(int j = k; j >= 0; j--)
            {
                if(S[j] <= M[i]) {ans1++;k=j-1;break;}
            }
        }

        k = 0;
        for(int i = 0; i < n; i++)
        {
            for(int j = k; j < n-1; j++)
            {
                if(M[i] <= S[j] && M[i] > S[j+1])
                {ans2++;k = j+1;break;}
            }
        }

        cout<<n-ans1<<endl;
        cout<<ans2<<endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 1005
char S[maxn];
char M[maxn];

int cmp(int x,int y)
{
    return x > y;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        scanf("%s%s",S,M);
        sort(S,S+n,cmp);
        sort(M,M+n,cmp);
        int ans1 = 0,ans2 = 0;
        int k = n-1;
        for(int i = n-1; i >= 0; i--)
        {
            for(int j = k; j >= 0; j--)
            {
                if(S[j] <= M[i])
                    {ans1++;k=j-1;break;}
            }
        }
        k = n-1;
        for(int i = n-1; i >= 0; i--)
        {
            for(int j = k; j >= 0; j--)
            {
                if(S[j] < M[i])
                    {ans2++;k=j-1;break;}
            }
        }

        cout<<n-ans1<<endl;
        cout<<ans2<<endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 1005
char S[maxn];
char M[maxn];

int cmp(int x,int y)
{
    return x > y;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        scanf("%s%s",S,M);
        sort(S,S+n,cmp);
        sort(M,M+n,cmp);
        int ans1 = 0,ans2 = 0;
        int k = n-1;
        for(int i = n-1; i >= 0; i--)
        {
            for(int j = k; j >= 0; j--)
            {
                if(S[i] <= M[j]) {ans1++;k=j-1;break;}
            }
        }

        k = 0;
        for(int i = 0; i < n; i++)
        {
            for(int j = k; j < n-1; j++)
            {
                if(M[i] <= S[j] && M[i] > S[j+1])
                {ans2++;k = j+1;break;}
            }
        }

        cout<<n-ans1<<endl;
        cout<<ans2<<endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 1005
char S[maxn];
char M[maxn];

int cmp(int x,int y)
{
    return x > y;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        scanf("%s%s",S,M);
        sort(S,S+n);
        sort(M,M+n);
        int ans1 = 0,ans2 = 0;
        for(int i = 0; i < n; i++)
        {
            if(M[i] < S[i]) ans1++;
        }
        sort(S,S+n,cmp);
        sort(M,M+n,cmp);

        for(int i = 0; i < n; i++)
            for(int j = 0; j < n-1; j++)
            {
                if(M[i] <= S[j] && M[i] > S[j+1])
                {ans2++;break;}
            }

        cout<<ans1<<endl;
        cout<<ans2<<endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 5000;
int main()
{
    int n, i, j;
    char s[N] = "", m[N] = "";
    int a[20] = {0};
    scanf("%d%*c%s%s", &n, s, m);
    for(i = 0; i < n; i++)
        a[m[i] - '0']++;
    int ans = 0, k = -1;
    for(i = 0; i < n; i++) {
        k = -1;
        for(j = 0; j < 10; j++) {
            int tmp = s[i] - '0';
            if(j >= tmp && a[j] > 0) {
                a[j]--;
                break;
            }
            if(k == -1 && a[j] > 0)
                k = j;
        }
        if(j == 10) {
            ans++;
            a[k]--;
        }
    }
    printf("%d\n", ans);
    for(i = 0; i < n; i++)
        a[m[i] - '0']++;
    ans = 0, k = -1;
    for(i = 0; i < n; i++) {
        k = -1;
        for(j = 0; j < 10; j++) {
            int tmp = s[i] - '0';
            if(j > tmp && a[j] > 0) {
                a[j]--;
                ans++;
                break;
            }
            if(k == -1 && a[j] > 0)
                k = j;
        }
        if(j == 10) {
            a[k]--;
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int const N = 1e4 + 5;

string s[N];
int f[N][30][30], n;

int main()
{
    //freopen("task.inp","r",stdin);

    scanf("%d", &n);

    for(int i = 1; i <= n; ++i)
        for(int j = 0; j < 26; ++j)
        for(int z = 0; z < 26; ++z)
        {
           f[i][j][z] = -2e9;
        }

    int res = 0;

    for(int i = 1; i <= n; ++i)
    {
        cin >> s[i];
        f[i][s[i].front() - 'a'][s[i].back() - 'a'] = s[i].size();

        int a = s[i].front() - 'a', b = s[i].back() - 'a';

        int t = s[i].size();
        if(a == b) res = max(res, t);
    }

    for(int i = 1; i <= n; ++i)
        for(int j = 0; j < 26; ++j)
        for(int z = 0; z < 26; ++z)
        {
            int leng = f[i][j][z];

            if(leng < 0) continue;

            if(i == n){
                    if(j == z)res = max(res, leng);
                    continue;
            }

            f[i + 1][j][z] = max(f[i + 1][j][z], leng);

            if(z != s[i + 1].front() - 'a') continue;

            int t = leng + s[i + 1].size();
		        f[i + 1][j][s[i + 1].back() - 'a'] = max(f[i + 1][j][s[i + 1].back() - 'a'], t);

        }

    printf("%d", res);
}


#include <bits/stdc++.h>
#define ft first
#define sc second

using namespace std;

typedef pair <int , int > II;
typedef pair <II, int > III;

int const N = 1e6 + 5;

III e[N];
int n, dd[N];
char s[1003][1003];
int f[30][30], cl[30][30];

int main()
{
    //freopen("task.inp","r",stdin);

    scanf("%d", &n);

    for(int i = 0; i < 26; ++i)
        for(int j = 0; j < 26; ++j)
        f[i][j] = 0;

    for(int i = 1; i <= n; ++i)
    {
        cin >> s[i];

        int m = strlen(s[i]);
        char a = s[i][0];
        char b = s[i][m - 1];

        e[i] = {{a - 'a', b - 'a'}, m};

        dd[a - 'a'] = 1;
    }

    int ans = 0;

    for(int i = 0; i < 26; ++i)
    {
        if(dd[i] == 0) continue;
        for(int j = 0; j < 26; ++j)
        {
            //cerr << f[i][j] << endl;
            for(int k = 1; k <= n; ++k)
            {
                int a = e[k].ft.ft;
                int b = e[k].ft.sc;
                int siz = e[k].sc;

                if(b == i && cl[i][a] == 1) f[i][j] = max(f[i][j], f[i][a] + siz), cl[i][j] = 1;

                 cl[a][b] = 1;
                 f[a][b] = siz;
            }

            if(cl[i][i] == 1) ans = max(ans, f[i][i]);
        }
    }

    printf("%d", ans);
}


#include <bits/stdc++.h>

using namespace std;

int const N = 1e4 + 5;

string s[N];
int f[N][30][30], n;

int main()
{
    //freopen("task.inp","r",stdin);

    scanf("%d", &n);

    for(int i = 1; i <= n; ++i)
        for(int j = 0; j < 26; ++j)
        for(int z = 0; z < 26; ++z)
        {
           f[i][j][z] = -2e9;
        }

    int res = 0;

    for(int i = 1; i <= n; ++i)
    {
        cin >> s[i];
        f[i][s[i].front() - 'a'][s[i].back() - 'a'] = s[i].size();

        int a = s[i].front() - 'a', b = s[i].back() - 'a';

        int t = s[i].size();
        if(a == b) res = max(res, t);
    }

    for(int i = 1; i <= n; ++i)
        for(int j = 0; j < 26; ++j)
        for(int z = 0; z < 26; ++z)
        {
            int leng = f[i][j][z];

            if(leng < 0) continue;

            if(j == z) res = max(res, leng);

            for(int k = i + 1; k <= n; ++k)
            {
            int t = leng + s[k].size();
            if (z == s[k].front() - 'a')
		        f[k][j][s[k].back() - 'a'] = max(f[k][j][s[k].back() - 'a'], t);
            }
        }

    printf("%d", res);
}


#include <bits/stdc++.h>

using namespace std;

int const N = 5e5 + 5;

string word[N];
int n, maxf[30][30], f[N][30];

int main()
{
    //freopen("task.inp","r",stdin);

    scanf("%d", &n);

    for(int i = 1; i <= n; ++i)
        for(int j = 0; j < 26; ++j)
        f[i][j] = -2e9;

    for(int i = 0; i < 26; ++i)
        for(int j = 0; j < 26; ++j)
        maxf[i][j] = -2e9;

    int res = 0;

    for(int i = 1; i <= n; ++i)
    {
        cin >> word[i];

        int a = word[i].front() - 'a';
        int b = word[i].back() - 'a';

        f[i][a] = max(f[i][a], int(word[i].size()));

        //maxf[a][b] = max(int(word[i].size()), maxf[a][b]);

        if(a == b) res = max(res, f[i][a]);
    }

    /**trau:
    for(int i = 1; i <= n; ++i)
        for(first = 0; first < 26; ++first)
        {
            for(int j = 1; j < i; ++j)
                if(word[j].back == word[i].front())
                f[i][fist] = max(f[j][first] + int(word[i].size()), f[i][first]);

            if(first == word[i].back() - 'a') res = max(res, f[i][first]);
        }**/

    /* f[i][first] - do dai lon nhat khi chon i vao xau baat dau bang first*/

    for(int i = 1; i <= n; ++i)
    {
        for(int first = 0; first < 26; ++first)
        {
            f[i][first] = max(f[i][first], maxf[word[i].front() - 'a'][first] + int(word[i].size()));

            if(first == word[i].back() - 'a') res = max(res, f[i][first]);
        }

        for(int first = 0; first < 26; ++first)
            maxf[word[i].back() - 'a'][first] = max(maxf[word[i].back() - 'a'][first], f[i][first]);
    }

    printf("%d", res);
}


#include <bits/stdc++.h>

using namespace std;

int const N = 1e5 + 5;

string s[N];
int f[N][30][30], n;

int main()
{
    //freopen("task.inp","r",stdin);

    scanf("%d", &n);

    for(int i = 1; i <= n; ++i)
        for(int j = 0; j < 26; ++j)
        for(int z = 0; z < 26; ++z)
        {
           f[i][j][z] = -2e9;
        }

    int res = 0;

    for(int i = 1; i <= n; ++i)
    {
        cin >> s[i];
        f[i][s[i].front() - 'a'][s[i].back() - 'a'] = s[i].size();

        int a = s[i].front() - 'a', b = s[i].back() - 'a';

        int t = s[i].size();
        if(a == b) res = max(res, t);
    }

    for(int i = 1; i <= n; ++i)
        for(int j = 0; j < 26; ++j)
        for(int z = 0; z < 26; ++z)
        {
            int leng = f[i][j][z];

            if(leng < 0) continue;

            if(i == n)
            {
                if(j == z) res = max(res, leng);
                continue;
            }

            for(int k = i + 1; k <= n; ++k)
            {
            int t = leng + s[k].size();
            if (z == s[k].front() - 'a')
		        f[k][j][s[k].back() - 'a'] = max(f[k][j][s[k].back() - 'a'], t);
            }
        }

    printf("%d", res);
}


#include <bits/stdc++.h>

using namespace std;

int const N = 5e5 + 5;

int n;
string word[N];

int f[N][30];

int main()
{
    //freopen("task.inp","r",stdin);

    scanf("%d", &n);

    for(int i = 1; i <= n; ++i)
        for(int j = 0; j < 16; ++j)
        f[i][j] = -2e9;

    int res = 0;

    for(int i = 1; i <= n; ++i)
    {
        cin >> word[i];

        int a = word[i].front() - 'a';
        int b = word[i].back() - 'a';

        f[i][a] = max(f[i][a], int(word[i].size()));

        if(a == b) res = max(res, f[i][a]);
    }

    // f[i][first] - do dai max khi chon i vao day co ki tu bat dau la first

    for(int i = 1; i <= n; ++i)
        for(int first = 0; first < 16; ++first)
        {
            if(f[i][first] <= -2e9) continue;

            char last = word[i].back();
            int leng = f[i][first];

            if(first == last - 'a')
                res = max(res, leng);

            for(int j = i + 1; j <= n; ++j)
                if(word[j].front() == last) f[j][first] = max(f[j][first], leng + int(word[j].size()));
        }

    printf("%d", res);
}


#include <bits/stdc++.h>

using namespace std;

int const N = 1e4 + 5;

string s[N];
int f[N][30][30], n;

int main()
{
    //freopen("task.inp","r",stdin);

    scanf("%d", &n);

    for(int i = 1; i <= n; ++i)
        for(int j = 0; j < 26; ++j)
        for(int z = 0; z < 26; ++z)
        {
           f[i][j][z] = -2e9;
        }

    int res = 0;

    for(int i = 1; i <= n; ++i)
    {
        cin >> s[i];
        f[i][s[i].front() - 'a'][s[i].back() - 'a'] = s[i].size();

        int a = s[i].front() - 'a', b = s[i].back() - 'a';

        int t = s[i].size();
        if(a == b) res = max(res, t);
    }

    for(int i = 1; i <= n; ++i)
        for(int j = 0; j < 26; ++j)
        for(int z = 0; z < 26; ++z)
        {
            int leng = f[i][j][z];

            if(leng < 0) continue;

            if(i == n)
            {
                if(j == z) res = max(res, leng);
                continue;
            }

            for(int k = i + 1; k <= n; ++k)
            {
            int t = leng + s[k].size();
            if (z == s[k].front() - 'a')
		        f[k][j][s[k].back() - 'a'] = max(f[k][j][s[k].back() - 'a'], t);
            }
        }

    printf("%d", res);
}


#include <bits/stdc++.h>

using namespace std;

int const N = 5e5 + 5;

string word[N];
int n, maxf[30][30], f[N][30];

int main()
{
    //freopen("task.inp","r",stdin);

    scanf("%d", &n);

    for(int i = 1; i <= n; ++i)
        for(int j = 0; j < 26; ++j)
        f[i][j] = -2e9;

    for(int i = 0; i < 26; ++i)
        for(int j = 0; j < 26; ++j)
        maxf[i][j] = 0;

    int res = 0;

    for(int i = 1; i <= n; ++i)
    {
        cin >> word[i];

        int a = word[i].front() - 'a';
        int b = word[i].back() - 'a';

        f[i][a] = max(f[i][a], int(word[i].size()));

        //maxf[a][b] = max(int(word[i].size()), maxf[a][b]);

        if(a == b) res = max(res, f[i][a]);
    }

    /**trau:
    for(int i = 1; i <= n; ++i)
        for(first = 0; first < 26; ++first)
        {
            for(int j = 1; j < i; ++j)
                if(word[j].back == word[i].front())
                f[i][fist] = max(f[j][first] + int(word[i].size()), f[i][first]);

            if(first == word[i].back() - 'a') res = max(res, f[i][first]);
        }**/

    /* f[i][first] - do dai lon nhat khi chon i vao xau baat dau bang first*/

    for(int i = 1; i <= n; ++i)
    {
        for(int first = 0; first < 26; ++first)
        {
            f[i][first] = max(f[i][first], maxf[word[i].front() - 'a'][first] + int(word[i].size()));

            if(first == word[i].back() - 'a') res = max(res, f[i][first]);
        }

        for(int first = 0; first < 26; ++first)
            maxf[word[i].back() - 'a'][first] = max(maxf[word[i].back() - 'a'][first], f[i][first]);
    }

    printf("%d", res);
}


#include <bits/stdc++.h>
#define ft first
#define sc second

using namespace std;

typedef pair <int , int > II;
typedef pair <II, int > III;

int const N = 1e6 + 5;

III e[N];
int n, dd[N];
char s[1003][1003];
int f[30][30];

int main()
{
    //freopen("task.inp","r",stdin);

    scanf("%d", &n);

    for(int i = 0; i < 26; ++i)
        for(int j = 0; j < 26; ++j)
        f[i][j] = 0;

    for(int i = 1; i <= n; ++i)
    {
        cin >> (s[i] + 1);

        int m = strlen(s[i] + 1);
        char a = s[i][1];
        char b = s[i][m];

        e[i] = {{a - 'a', b - 'a'}, m};

        dd[a - 'a'] = 1;
    }

    int ans = 0;

    for(int i = 0; i < 26; ++i)
    {
        for(int j = 0; j < 26; ++j)
        {
            //cerr << f[i][j] << endl;
            for(int k = 1; k <= n; ++k)
            {
                int a = e[k].ft.ft;
                int b = e[k].ft.sc;
                int siz = e[k].sc;

                if(b == i) f[i][j] = max(f[i][j], f[i][a] + siz);

            }
        }
    }

    for(int i = 0; i < 26; ++i)
        if(dd[i] == 1) ans = max(ans, f[i][i]);

    printf("%d", ans);
}


#include <bits/stdc++.h>
#define ft first
#define sc second

using namespace std;

typedef pair <int , int > II;
typedef pair <II, int > III;

int const N = 1e6 + 5;

III e[N];
int n, dd[N];
char s[1003][1003];
int f[30][30], cl[30][30];

int main()
{
    //freopen("task.inp","r",stdin);

    scanf("%d", &n);

    for(int i = 0; i < 26; ++i)
        for(int j = 0; j < 26; ++j)
        f[i][j] = 0;

    for(int i = 1; i <= n; ++i)
    {
        cin >> s[i];

        int m = strlen(s[i]);
        char a = s[i][0];
        char b = s[i][m - 1];

        e[i] = {{a - 'a', b - 'a'}, m};

        dd[a - 'a'] = 1;
        cl[a - 'a'][b - 'a'] = 1;
    }

    int ans = 0;

    for(int i = 0; i < 26; ++i)
    {
        for(int j = 0; j < 26; ++j)
        {
            //cerr << f[i][j] << endl;
            for(int k = 1; k <= n; ++k)
            {
                int a = e[k].ft.ft;
                int b = e[k].ft.sc;
                int siz = e[k].sc;

                if(b == i && cl[i][a] == 1) f[i][j] = max(f[i][j], f[i][a] + siz), cl[i][j] = 1;

            }
        }
    }

    for(int i = 0; i < 26; ++i)
        if(dd[i] == 1) ans = max(ans, f[i][i]);

    printf("%d", ans);
}


#include <bits/stdc++.h>
#define ft first
#define sc second

using namespace std;

typedef pair <int , int > II;
typedef pair <II, int > III;

int const N = 1e6 + 5;

III e[N];
int n;
char s[1003][1003];
int f[30][30];

int main()
{
    //freopen("task.inp","r",stdin);

    scanf("%d", &n);

    for(int i = 0; i < 26; ++i)
        for(int j = 0; j < 26; ++j)
        f[i][j] = 0;

    for(int i = 1; i <= n; ++i)
    {
        cin >> (s[i] + 1);

        int m = strlen(s[i] + 1);
        char a = s[i][1];
        char b = s[i][m];

        e[i] = {{a - 'a', b - 'a'}, m};
    }

    int ans = 0;

    for(int i = 0; i < 26; ++i)
    {
        for(int j = 0; j < 26; ++j)
        {
            //cerr << f[i][j] << endl;
            for(int k = 1; k <= n; ++k)
            {
                int a = e[k].ft.ft;
                int b = e[k].ft.sc;
                int siz = e[k].sc;

                if(b == j) f[i][j] = max(f[i][j], f[i][a] + siz);
            }

            ans = max(ans, f[i][j]);
        }

        for(int j = 0; j < 26; ++j) f[i][j] = 0;

            for(int k = 1; k <= n; ++k)
            {
                int a = e[k].ft.ft;
                int b = e[k].ft.sc;
                int siz = e[k].sc;

                f[a][b] = max(f[a][b], siz);
            }
    }

    printf("%d", ans);
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
#include <functional>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1e9 + 5;
const int MAXN = 100000007;
const int MOD = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1.0);
LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a%b); }

int DP[30][30];

int main()
{
	char  s[20];
	int n;
	int ans;
	ans = 0;
	scanf("%d", &n);
	memset(DP, 0, sizeof DP);
	for (int i = 1; i <= n; i++)
	{
		scanf("%s", s);
		int len = strlen(s);
		/*s[0]-'a',s[s.length()-1]-'a'*/
		for (int i = 0; i < 26; i++)
		{
			if(DP[i][s[0] - 'a']!=0)
				DP[i][s[len - 1] - 'a'] = max(DP[i][s[len - 1] - 'a'],DP[i][s[0] - 'a'] + (int)len);
		}
		DP[s[0] - 'a'][s[len - 1] - 'a'] = max(len, DP[s[0] - 'a'][s[len - 1] - 'a']);
	}
	for (int i = 0; i < 26; i++)
	{
		ans = max(ans, DP[i][i]);
	}
	printf("%d\n", ans);
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
#include <functional>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1e9 + 5;
const int MAXN = 100000007;
const int MOD = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1.0);
LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a%b); }

int DP[30][30];

int main()
{
	char s[20];
	int n;
	int ans;
	ans = 0;
	scanf("%d", &n);
	memset(DP, 0, sizeof DP);
	for (int i = 1; i <= n; i++)
	{
		scanf("%s", s);
		int len = strlen(s);
		/*s[0]-'a',s[s.length()-1]-'a'*/
		for (int i = 0; i < 26; i++)
		{
			if(DP[i][s[0] - 'a']!=0)
				DP[i][s[len - 1] - 'a'] = max(DP[i][s[len - 1] - 'a'],DP[i][s[0] - 'a'] + (int)len);
		}
		DP[s[0] - 'a'][s[len - 1] - 'a'] = max(len, DP[s[0] - 'a'][s[len - 1] - 'a']);
	}
	for (int i = 0; i < 26; i++)
	{
		ans = max(ans, DP[i][i]);
	}
	printf("%d\n", ans);
}


#include <bits/stdc++.h>
#include <unistd.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including seg_tree_order_statistics_node_update
#include <stdio.h>
#include <cassert>
using namespace std;
//using namespace __gnu_pbds;
typedef long long lo;
typedef long double ld;
#include <ctime>
typedef pair<lo, lo> ll; //pair
typedef vector<lo> vl;   //vector of long
typedef vector<ll> vll;  //vector of pair
typedef priority_queue<lo> p_q;
typedef vector<vl> vvl; //vector of vectors
#define X first
#define Y second
#define mp(a, b) make_pair((a), (b))
#define REP(a, b) for (lo i = (a); i < (lo)b; i++) //no need to declare variable i
#define REPE(a, b, c, d) \
    REP(a, b)            \
    for (lo j = (c); j < (lo)d; j++)                    //no need to declare vaiables i,j
#define REPV(a, b, c) for (lo(a) = b; (a) < (c); (a)++) //a is the variable
#define IREP(a, b) for (lo i = (a); i >= (b); i--)
#define IREPV(a, b, c) for (lo(a) = b; (a) >= (c); (a)--)
#define correct(x, y, n, m) (0 <= (x) && (x) < (n) && 0 <= (y) && (y) < (m))
#define all(v) (v).begin(), (v).end()
#define TRV(a) for (auto &it : a)
#define INF 500010
#define MOD 1000000007
#define MOD2 1000000009
#define BLOCK 300
#define CHECK_BIT(var, pos) ((var) & (1 << (pos)))
#define pb(a) push_back((a))
#define eps 1e-2
#define PI acos(-1.0)

#if _DEBUG
#define debug(x) cout << #x << "=" << x << endl
#define debug2(x, y) cout << #x << "=" << x << " " << #y << "=" << y << endl;
#define debug3(x, y, z) cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << endl;
#define debug4(x, y, z, w) cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << " " << #w << "=" << w << endl;
#define debug5(x, y, z, w, t)      \
    cout << #x << "=" << x << " "; \
    debug4(y, z, w, t);
#define debug6(p, x, y, z, w, t)                            \
    cout << #p << " " << p << " " << #x << "=" << x << " "; \
    debug4(y, z, w, t);
#define debug7(o, p, x, y, z, w, t) \
    cout << #o << " " << o << " ";  \
    debug6(p, x, y, z, w, t);
#define wait  \
    int xxxx; \
    cin >> xxxx;
#define derr(x) cerr << #x << "=" << x << endl;
#define derr2(x, y) cerr << #x << "=" << x << " " << #y << "=" << y << endl;
#define derr3(x, y, z) cerr << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << endl;
#define derr4(x, y, z, w) cerr << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << " " << #w << "=" << w << endl;
#define derr5(x, y, z, w, t)       \
    cerr << #x << "=" << x << " "; \
    derr4(y, z, w, t);
#define derr6(p, x, y, z, w, t)                             \
    cerr << #p << " " << p << " " << #x << "=" << x << " "; \
    derr4(y, z, w, t);
#define derr7(o, p, x, y, z, w, t) \
    cerr << #o << " " << o << " "; \
    derr6(p, x, y, z, w, t);
lo checkpoint_counter = 0;
#define checkpoint cerr << "At checkpoint : " << checkpoint_counter++ << endl;

#else
#define debug(x) ;
#define debug2(x, y) ;
#define debug3(x, y, z) ;
#define debug4(x, y, z, q) ;
#define debug5(x, y, z, r, t) ;
#define debug6(x, y, z, r, t, s) ;
#define debug7(x, y, z, r, t, s, u) ;
#define wait ;
#define derr(x) ;
#define derr2(x, y) ;
#define derr3(x, y, z) ;
#define derr4(x, y, z, q) ;
#define derr5(x, y, z, r, t) ;
#define derr6(x, y, z, r, t, s) ;
#define derr7(x, y, z, r, t, f, u) ;
#define checkpoint ;
#endif

#define print_matrix(a, n, m) \
    REPE(0, n, 0, m) { cout << (a)[i][j] << ((j == m - 1) ? '\n' : ' '); }
#define present(container, element) (container.find(element) != container.end())
#define endl "\n"
template <typename T>
ostream &operator<<(ostream &o, vector<T> v)
{
    if (v.size() > 0)
        o << v[0];
    for (unsigned i = 1; i < v.size(); i++)
        o << " " << v[i];
    return o << " ";
}
template <typename U, typename V>
ostream &operator<<(ostream &o, pair<U, V> p)
{
    return o << "(" << p.first << ", " << p.second << ") ";
}
template <typename T>
istream &operator>>(istream &in, vector<T> &v)
{
    for (unsigned i = 0; i < v.size(); i++)
        in >> v[i];
    return in;
}
template <typename T>
istream &operator>>(istream &in, pair<T, T> &p)
{
    in >> p.X;
    in >> p.Y;
    return in;
}
template <typename T>
ostream &operator<<(ostream &o, set<T> v)
{
    TRV(v)
    o << it << " ";
    return o << endl;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, map<T, U> v)
{
    TRV(v)
    o << it << " ";
    return o << endl;
}
template <typename T>
T &&vmin(T &&val)
{
    return std::forward<T>(val);
}

template <typename T0, typename T1, typename... Ts>
auto vmin(T0 &&val1, T1 &&val2, Ts &&... vs)
{
    return (val1 < val2) ? vmin(val1, std::forward<Ts>(vs)...) : vmin(val2, std::forward<Ts>(vs)...);
}
template <typename T>
T &&vmax(T &&val)
{
    return std::forward<T>(val);
}

template <typename T0, typename T1, typename... Ts>
auto vmax(T0 &&val1, T1 &&val2, Ts &&... vs)
{
    return (val1 > val2) ? vmax(val1, std::forward<Ts>(vs)...) : vmax(val2, std::forward<Ts>(vs)...);
}
struct custom_hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
int main(int argc, char *argv[])
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(20);
    lo n;
    cin >> n;
    lo dp[2][26][26];
    memset(dp, -1LL, sizeof(dp));
    REP(0, n)
    {
        lo turn = i % 2;
        lo other = turn ^ 1;
        string s;
        cin >> s;
        lo x = s[0] - 'a';
        lo y = s.back() - 'a';
        REPV(j, 0, 26)
        if (dp[other][j][x] != -1) dp[turn][j][y] = max(dp[turn][j][y], dp[other][j][x] + lo(s.length()));
        dp[turn][x][y] = max(dp[turn][x][y], lo(s.length()));
        REPV(j, 0, 26)
        dp[other][j][y] = dp[turn][j][y];
    }
    lo ans = 0;
    // print_matrix(dp[0], n, n);
    REP(0, n)
    {
        ans = max(ans, dp[n & 1][i][i]);
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#include <unistd.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including seg_tree_order_statistics_node_update
#include <stdio.h>
#include <cassert>
using namespace std;
//using namespace __gnu_pbds;
typedef long long lo;
typedef long double ld;
#include <ctime>
typedef pair<lo, lo> ll; //pair
typedef vector<lo> vl;   //vector of long
typedef vector<ll> vll;  //vector of pair
typedef priority_queue<lo> p_q;
typedef vector<vl> vvl; //vector of vectors
#define X first
#define Y second
#define mp(a, b) make_pair((a), (b))
#define REP(a, b) for (lo i = (a); i < (lo)b; i++) //no need to declare variable i
#define REPE(a, b, c, d) \
    REP(a, b)            \
    for (lo j = (c); j < (lo)d; j++)                    //no need to declare vaiables i,j
#define REPV(a, b, c) for (lo(a) = b; (a) < (c); (a)++) //a is the variable
#define IREP(a, b) for (lo i = (a); i >= (b); i--)
#define IREPV(a, b, c) for (lo(a) = b; (a) >= (c); (a)--)
#define correct(x, y, n, m) (0 <= (x) && (x) < (n) && 0 <= (y) && (y) < (m))
#define all(v) (v).begin(), (v).end()
#define TRV(a) for (auto &it : a)
#define INF 500010
#define MOD 1000000007
#define MOD2 1000000009
#define BLOCK 300
#define CHECK_BIT(var, pos) ((var) & (1 << (pos)))
#define pb(a) push_back((a))
#define eps 1e-2
#define PI acos(-1.0)

#if _DEBUG
#define debug(x) cout << #x << "=" << x << endl
#define debug2(x, y) cout << #x << "=" << x << " " << #y << "=" << y << endl;
#define debug3(x, y, z) cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << endl;
#define debug4(x, y, z, w) cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << " " << #w << "=" << w << endl;
#define debug5(x, y, z, w, t)      \
    cout << #x << "=" << x << " "; \
    debug4(y, z, w, t);
#define debug6(p, x, y, z, w, t)                            \
    cout << #p << " " << p << " " << #x << "=" << x << " "; \
    debug4(y, z, w, t);
#define debug7(o, p, x, y, z, w, t) \
    cout << #o << " " << o << " ";  \
    debug6(p, x, y, z, w, t);
#define wait  \
    int xxxx; \
    cin >> xxxx;
#define derr(x) cerr << #x << "=" << x << endl;
#define derr2(x, y) cerr << #x << "=" << x << " " << #y << "=" << y << endl;
#define derr3(x, y, z) cerr << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << endl;
#define derr4(x, y, z, w) cerr << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << " " << #w << "=" << w << endl;
#define derr5(x, y, z, w, t)       \
    cerr << #x << "=" << x << " "; \
    derr4(y, z, w, t);
#define derr6(p, x, y, z, w, t)                             \
    cerr << #p << " " << p << " " << #x << "=" << x << " "; \
    derr4(y, z, w, t);
#define derr7(o, p, x, y, z, w, t) \
    cerr << #o << " " << o << " "; \
    derr6(p, x, y, z, w, t);
lo checkpoint_counter = 0;
#define checkpoint cerr << "At checkpoint : " << checkpoint_counter++ << endl;

#else
#define debug(x) ;
#define debug2(x, y) ;
#define debug3(x, y, z) ;
#define debug4(x, y, z, q) ;
#define debug5(x, y, z, r, t) ;
#define debug6(x, y, z, r, t, s) ;
#define debug7(x, y, z, r, t, s, u) ;
#define wait ;
#define derr(x) ;
#define derr2(x, y) ;
#define derr3(x, y, z) ;
#define derr4(x, y, z, q) ;
#define derr5(x, y, z, r, t) ;
#define derr6(x, y, z, r, t, s) ;
#define derr7(x, y, z, r, t, f, u) ;
#define checkpoint ;
#endif

#define print_matrix(a, n, m) \
    REPE(0, n, 0, m) { cout << (a)[i][j] << ((j == m - 1) ? '\n' : ' '); }
#define present(container, element) (container.find(element) != container.end())
#define endl "\n"
template <typename T>
ostream &operator<<(ostream &o, vector<T> v)
{
    if (v.size() > 0)
        o << v[0];
    for (unsigned i = 1; i < v.size(); i++)
        o << " " << v[i];
    return o << " ";
}
template <typename U, typename V>
ostream &operator<<(ostream &o, pair<U, V> p)
{
    return o << "(" << p.first << ", " << p.second << ") ";
}
template <typename T>
istream &operator>>(istream &in, vector<T> &v)
{
    for (unsigned i = 0; i < v.size(); i++)
        in >> v[i];
    return in;
}
template <typename T>
istream &operator>>(istream &in, pair<T, T> &p)
{
    in >> p.X;
    in >> p.Y;
    return in;
}
template <typename T>
ostream &operator<<(ostream &o, set<T> v)
{
    TRV(v)
    o << it << " ";
    return o << endl;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, map<T, U> v)
{
    TRV(v)
    o << it << " ";
    return o << endl;
}
template <typename T>
T &&vmin(T &&val)
{
    return std::forward<T>(val);
}

template <typename T0, typename T1, typename... Ts>
auto vmin(T0 &&val1, T1 &&val2, Ts &&... vs)
{
    return (val1 < val2) ? vmin(val1, std::forward<Ts>(vs)...) : vmin(val2, std::forward<Ts>(vs)...);
}
template <typename T>
T &&vmax(T &&val)
{
    return std::forward<T>(val);
}

template <typename T0, typename T1, typename... Ts>
auto vmax(T0 &&val1, T1 &&val2, Ts &&... vs)
{
    return (val1 > val2) ? vmax(val1, std::forward<Ts>(vs)...) : vmax(val2, std::forward<Ts>(vs)...);
}
struct custom_hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
int main(int argc, char *argv[])
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(20);
    lo n;
    cin >> n;
    lo dp[2][26][26];
    memset(dp, -1LL, sizeof(dp));
    REP(0, n)
    {
        lo turn = i % 2;
        lo other = turn ^ 1;
        string s;
        cin >> s;
        lo x = s[0] - 'a';
        lo y = s.back() - 'a';
        REPV(j, 0, 26)
        if (dp[other][j][x] != -1)
            dp[turn][j][y] = max(dp[turn][j][y], dp[other][j][x] + lo(s.length()));
        dp[turn][x][y] = max(dp[turn][x][y], lo(s.length()));
        REPV(j, 0, 26)
        dp[other][j][y] = dp[turn][j][y];
        dp[other][x][y] = dp[turn][x][y];
    }
    lo ans = 0;
    // print_matrix(dp[0], n, n);
    REP(0, n)
    {
        ans = max(ans, dp[n & 1][i][i]);
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#include <unistd.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including seg_tree_order_statistics_node_update
#include <stdio.h>
#include <cassert>
using namespace std;
//using namespace __gnu_pbds;
typedef long long lo;
typedef long double ld;
#include <ctime>
typedef pair<lo, lo> ll; //pair
typedef vector<lo> vl;   //vector of long
typedef vector<ll> vll;  //vector of pair
typedef priority_queue<lo> p_q;
typedef vector<vl> vvl; //vector of vectors
#define X first
#define Y second
#define mp(a, b) make_pair((a), (b))
#define REP(a, b) for (lo i = (a); i < (lo)b; i++) //no need to declare variable i
#define REPE(a, b, c, d) \
    REP(a, b)            \
    for (lo j = (c); j < (lo)d; j++)                    //no need to declare vaiables i,j
#define REPV(a, b, c) for (lo(a) = b; (a) < (c); (a)++) //a is the variable
#define IREP(a, b) for (lo i = (a); i >= (b); i--)
#define IREPV(a, b, c) for (lo(a) = b; (a) >= (c); (a)--)
#define correct(x, y, n, m) (0 <= (x) && (x) < (n) && 0 <= (y) && (y) < (m))
#define all(v) (v).begin(), (v).end()
#define TRV(a) for (auto &it : a)
#define INF 500010
#define MOD 1000000007
#define MOD2 1000000009
#define BLOCK 300
#define CHECK_BIT(var, pos) ((var) & (1 << (pos)))
#define pb(a) push_back((a))
#define eps 1e-2
#define PI acos(-1.0)

#if _DEBUG
#define debug(x) cout << #x << "=" << x << endl
#define debug2(x, y) cout << #x << "=" << x << " " << #y << "=" << y << endl;
#define debug3(x, y, z) cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << endl;
#define debug4(x, y, z, w) cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << " " << #w << "=" << w << endl;
#define debug5(x, y, z, w, t)      \
    cout << #x << "=" << x << " "; \
    debug4(y, z, w, t);
#define debug6(p, x, y, z, w, t)                            \
    cout << #p << " " << p << " " << #x << "=" << x << " "; \
    debug4(y, z, w, t);
#define debug7(o, p, x, y, z, w, t) \
    cout << #o << " " << o << " ";  \
    debug6(p, x, y, z, w, t);
#define wait  \
    int xxxx; \
    cin >> xxxx;
#define derr(x) cerr << #x << "=" << x << endl;
#define derr2(x, y) cerr << #x << "=" << x << " " << #y << "=" << y << endl;
#define derr3(x, y, z) cerr << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << endl;
#define derr4(x, y, z, w) cerr << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << " " << #w << "=" << w << endl;
#define derr5(x, y, z, w, t)       \
    cerr << #x << "=" << x << " "; \
    derr4(y, z, w, t);
#define derr6(p, x, y, z, w, t)                             \
    cerr << #p << " " << p << " " << #x << "=" << x << " "; \
    derr4(y, z, w, t);
#define derr7(o, p, x, y, z, w, t) \
    cerr << #o << " " << o << " "; \
    derr6(p, x, y, z, w, t);
lo checkpoint_counter = 0;
#define checkpoint cerr << "At checkpoint : " << checkpoint_counter++ << endl;

#else
#define debug(x) ;
#define debug2(x, y) ;
#define debug3(x, y, z) ;
#define debug4(x, y, z, q) ;
#define debug5(x, y, z, r, t) ;
#define debug6(x, y, z, r, t, s) ;
#define debug7(x, y, z, r, t, s, u) ;
#define wait ;
#define derr(x) ;
#define derr2(x, y) ;
#define derr3(x, y, z) ;
#define derr4(x, y, z, q) ;
#define derr5(x, y, z, r, t) ;
#define derr6(x, y, z, r, t, s) ;
#define derr7(x, y, z, r, t, f, u) ;
#define checkpoint ;
#endif

#define print_matrix(a, n, m) \
    REPE(0, n, 0, m) { cout << (a)[i][j] << ((j == m - 1) ? '\n' : ' '); }
#define present(container, element) (container.find(element) != container.end())
#define endl "\n"
template <typename T>
ostream &operator<<(ostream &o, vector<T> v)
{
    if (v.size() > 0)
        o << v[0];
    for (unsigned i = 1; i < v.size(); i++)
        o << " " << v[i];
    return o << " ";
}
template <typename U, typename V>
ostream &operator<<(ostream &o, pair<U, V> p)
{
    return o << "(" << p.first << ", " << p.second << ") ";
}
template <typename T>
istream &operator>>(istream &in, vector<T> &v)
{
    for (unsigned i = 0; i < v.size(); i++)
        in >> v[i];
    return in;
}
template <typename T>
istream &operator>>(istream &in, pair<T, T> &p)
{
    in >> p.X;
    in >> p.Y;
    return in;
}
template <typename T>
ostream &operator<<(ostream &o, set<T> v)
{
    TRV(v)
    o << it << " ";
    return o << endl;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, map<T, U> v)
{
    TRV(v)
    o << it << " ";
    return o << endl;
}
template <typename T>
T &&vmin(T &&val)
{
    return std::forward<T>(val);
}

template <typename T0, typename T1, typename... Ts>
auto vmin(T0 &&val1, T1 &&val2, Ts &&... vs)
{
    return (val1 < val2) ? vmin(val1, std::forward<Ts>(vs)...) : vmin(val2, std::forward<Ts>(vs)...);
}
template <typename T>
T &&vmax(T &&val)
{
    return std::forward<T>(val);
}

template <typename T0, typename T1, typename... Ts>
auto vmax(T0 &&val1, T1 &&val2, Ts &&... vs)
{
    return (val1 > val2) ? vmax(val1, std::forward<Ts>(vs)...) : vmax(val2, std::forward<Ts>(vs)...);
}
struct custom_hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
int main(int argc, char *argv[])
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(20);
    lo n;
    cin >> n;
    lo dp[2][26][26];
    memset(dp, -1LL, sizeof(dp));
    REP(0, n)
    {
        lo turn = i % 2;
        lo other = turn ^ 1;
        string s;
        cin >> s;
        lo x = s[0] - 'a';
        lo y = s.back() - 'a';
        REPV(j, 0, 26)
        if (dp[other][j][x] != -1)
            dp[turn][j][y] = max(dp[turn][j][y], dp[other][j][x] + lo(s.length()));
        dp[turn][x][y] = max(dp[turn][x][y], lo(s.length()));
        REPV(j, 0, 26)
        dp[other][j][y] = dp[turn][j][y];
        dp[other][x][y] = dp[turn][x][y];
    }
    lo ans = 0;
    // print_matrix(dp[1], 26, 26);
    REP(0, 26)
    {
        ans = max(ans, dp[n & 1][i][i]);
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#include <unistd.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including seg_tree_order_statistics_node_update
#include <stdio.h>
#include <cassert>
using namespace std;
//using namespace __gnu_pbds;
typedef long long lo;
typedef long double ld;
#include <ctime>
typedef pair<lo, lo> ll; //pair
typedef vector<lo> vl;   //vector of long
typedef vector<ll> vll;  //vector of pair
typedef priority_queue<lo> p_q;
typedef vector<vl> vvl; //vector of vectors
#define X first
#define Y second
#define mp(a, b) make_pair((a), (b))
#define REP(a, b) for (lo i = (a); i < (lo)b; i++) //no need to declare variable i
#define REPE(a, b, c, d) REP(a, b) \
for (lo j = (c); j < (lo)d; j++)                        //no need to declare vaiables i,j
#define REPV(a, b, c) for (lo(a) = b; (a) < (c); (a)++) //a is the variable
#define IREP(a, b) for (lo i = (a); i >= (b); i--)
#define IREPV(a, b, c) for (lo(a) = b; (a) >= (c); (a)--)
#define correct(x, y, n, m) (0 <= (x) && (x) < (n) && 0 <= (y) && (y) < (m))
#define all(v) (v).begin(), (v).end()
#define TRV(a) for (auto &it : a)
#define INF 500010
#define MOD 1000000007
#define MOD2 1000000009
#define BLOCK 300
#define CHECK_BIT(var, pos) ((var) & (1 << (pos)))
#define pb(a) push_back((a))
#define eps 1e-2
#define PI acos(-1.0)

#if _DEBUG
#define debug(x) cout << #x << "=" << x << endl
#define debug2(x, y) cout << #x << "=" << x << " " << #y << "=" << y << endl;
#define debug3(x, y, z) cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << endl;
#define debug4(x, y, z, w) cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << " " << #w << "=" << w << endl;
#define debug5(x, y, z, w, t)      \
    cout << #x << "=" << x << " "; \
    debug4(y, z, w, t);
#define debug6(p, x, y, z, w, t)                            \
    cout << #p << " " << p << " " << #x << "=" << x << " "; \
    debug4(y, z, w, t);
#define debug7(o, p, x, y, z, w, t) \
    cout << #o << " " << o << " ";  \
    debug6(p, x, y, z, w, t);
#define wait  \
    int xxxx; \
    cin >> xxxx;
#define derr(x) cerr << #x << "=" << x << endl;
#define derr2(x, y) cerr << #x << "=" << x << " " << #y << "=" << y << endl;
#define derr3(x, y, z) cerr << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << endl;
#define derr4(x, y, z, w) cerr << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << " " << #w << "=" << w << endl;
#define derr5(x, y, z, w, t)       \
    cerr << #x << "=" << x << " "; \
    derr4(y, z, w, t);
#define derr6(p, x, y, z, w, t)                             \
    cerr << #p << " " << p << " " << #x << "=" << x << " "; \
    derr4(y, z, w, t);
#define derr7(o, p, x, y, z, w, t) \
    cerr << #o << " " << o << " "; \
    derr6(p, x, y, z, w, t);
lo checkpoint_counter=0;
#define checkpoint cerr << "At checkpoint : " << checkpoint_counter++ << endl;

#else
#define debug(x) ;
#define debug2(x, y) ;
#define debug3(x, y, z) ;
#define debug4(x, y, z, q) ;
#define debug5(x, y, z, r, t) ;
#define debug6(x, y, z, r, t, s) ;
#define debug7(x, y, z, r, t, s, u) ;
#define wait ;
#define derr(x) ;
#define derr2(x, y) ;
#define derr3(x, y, z) ;
#define derr4(x, y, z, q) ;
#define derr5(x, y, z, r, t) ;
#define derr6(x, y, z, r, t, s) ;
#define derr7(x, y, z, r, t, f, u) ;
#define checkpoint ;
#endif

#define print_matrix(a, n, m) \
    REPE(0, n, 0, m) { cout << (a)[i][j] << ((j == m - 1) ? '\n' : ' '); }
#define present(container, element) (container.find(element) != container.end())
#define endl "\n"
template <typename T>
ostream &operator<<(ostream &o, vector<T> v)
{
    if (v.size() > 0)
        o << v[0];
    for (unsigned i = 1; i < v.size(); i++)
        o << " " << v[i];
    return o << " ";
}
template <typename U, typename V>
ostream &operator<<(ostream &o, pair<U, V> p)
{
    return o << "(" << p.first << ", " << p.second << ") ";
}
template <typename T>
istream &operator>>(istream &in, vector<T> &v)
{
    for (unsigned i = 0; i < v.size(); i++)
        in >> v[i];
    return in;
}
template <typename T>
istream &operator>>(istream &in, pair<T, T> &p)
{
    in >> p.X;
    in >> p.Y;
    return in;
}
template <typename T>
ostream &operator<<(ostream &o, set<T> v)
{
    TRV(v)
        o << it << " ";
    return o << endl;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, map<T, U> v)
{
    TRV(v)
        o << it << " ";
    return o << endl;
}
template <typename T>
T &&vmin(T &&val)
{
    return std::forward<T>(val);
}

template <typename T0, typename T1, typename... Ts>
auto vmin(T0 &&val1, T1 &&val2, Ts &&... vs)
{
    return (val1 < val2) ? vmin(val1, std::forward<Ts>(vs)...) : vmin(val2, std::forward<Ts>(vs)...);
}
template <typename T>
T &&vmax(T &&val)
{
    return std::forward<T>(val);
}

template <typename T0, typename T1, typename... Ts>
auto vmax(T0 &&val1, T1 &&val2, Ts &&... vs)
{
    return (val1 > val2) ? vmax(val1, std::forward<Ts>(vs)...) : vmax(val2, std::forward<Ts>(vs)...);
}
struct custom_hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
int main(int argc, char *argv[])
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(20);
    lo n;
    cin>>n;
    lo dp[2][26][26];
    REP(0, n){
        lo turn = i%2;
        lo other = turn ^ 1;
        string s;
        cin>>s;
        lo x = s[0] - 'a';
        lo y = s.back() - 'a';
        REPV(j, 0, 26)dp[turn][j][y] = max(dp[turn][j][y], dp[other][j][x] + lo(s.length()));
        dp[turn][x][y] = max(dp[turn][x][y], lo(s.length()));
        REPV(j, 0, 26)dp[other][j][y] = dp[turn][j][y];
    }
    lo ans = 0;
    REP(0, n){
        ans = max(ans, dp[n&1][i][i]);
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
#include <unistd.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including seg_tree_order_statistics_node_update
#include <stdio.h>
#include <cassert>
using namespace std;
//using namespace __gnu_pbds;
typedef long long lo;
typedef long double ld;
#include <ctime>
typedef pair<lo, lo> ll; //pair
typedef vector<lo> vl;   //vector of long
typedef vector<ll> vll;  //vector of pair
typedef priority_queue<lo> p_q;
typedef vector<vl> vvl; //vector of vectors
#define X first
#define Y second
#define mp(a, b) make_pair((a), (b))
#define REP(a, b) for (lo i = (a); i < (lo)b; i++) //no need to declare variable i
#define REPE(a, b, c, d) \
    REP(a, b)            \
    for (lo j = (c); j < (lo)d; j++)                    //no need to declare vaiables i,j
#define REPV(a, b, c) for (lo(a) = b; (a) < (c); (a)++) //a is the variable
#define IREP(a, b) for (lo i = (a); i >= (b); i--)
#define IREPV(a, b, c) for (lo(a) = b; (a) >= (c); (a)--)
#define correct(x, y, n, m) (0 <= (x) && (x) < (n) && 0 <= (y) && (y) < (m))
#define all(v) (v).begin(), (v).end()
#define TRV(a) for (auto &it : a)
#define INF 500010
#define MOD 1000000007
#define MOD2 1000000009
#define BLOCK 300
#define CHECK_BIT(var, pos) ((var) & (1 << (pos)))
#define pb(a) push_back((a))
#define eps 1e-2
#define PI acos(-1.0)

#if _DEBUG
#define debug(x) cout << #x << "=" << x << endl
#define debug2(x, y) cout << #x << "=" << x << " " << #y << "=" << y << endl;
#define debug3(x, y, z) cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << endl;
#define debug4(x, y, z, w) cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << " " << #w << "=" << w << endl;
#define debug5(x, y, z, w, t)      \
    cout << #x << "=" << x << " "; \
    debug4(y, z, w, t);
#define debug6(p, x, y, z, w, t)                            \
    cout << #p << " " << p << " " << #x << "=" << x << " "; \
    debug4(y, z, w, t);
#define debug7(o, p, x, y, z, w, t) \
    cout << #o << " " << o << " ";  \
    debug6(p, x, y, z, w, t);
#define wait  \
    int xxxx; \
    cin >> xxxx;
#define derr(x) cerr << #x << "=" << x << endl;
#define derr2(x, y) cerr << #x << "=" << x << " " << #y << "=" << y << endl;
#define derr3(x, y, z) cerr << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << endl;
#define derr4(x, y, z, w) cerr << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << " " << #w << "=" << w << endl;
#define derr5(x, y, z, w, t)       \
    cerr << #x << "=" << x << " "; \
    derr4(y, z, w, t);
#define derr6(p, x, y, z, w, t)                             \
    cerr << #p << " " << p << " " << #x << "=" << x << " "; \
    derr4(y, z, w, t);
#define derr7(o, p, x, y, z, w, t) \
    cerr << #o << " " << o << " "; \
    derr6(p, x, y, z, w, t);
lo checkpoint_counter = 0;
#define checkpoint cerr << "At checkpoint : " << checkpoint_counter++ << endl;

#else
#define debug(x) ;
#define debug2(x, y) ;
#define debug3(x, y, z) ;
#define debug4(x, y, z, q) ;
#define debug5(x, y, z, r, t) ;
#define debug6(x, y, z, r, t, s) ;
#define debug7(x, y, z, r, t, s, u) ;
#define wait ;
#define derr(x) ;
#define derr2(x, y) ;
#define derr3(x, y, z) ;
#define derr4(x, y, z, q) ;
#define derr5(x, y, z, r, t) ;
#define derr6(x, y, z, r, t, s) ;
#define derr7(x, y, z, r, t, f, u) ;
#define checkpoint ;
#endif

#define print_matrix(a, n, m) \
    REPE(0, n, 0, m) { cout << (a)[i][j] << ((j == m - 1) ? '\n' : ' '); }
#define present(container, element) (container.find(element) != container.end())
#define endl "\n"
template <typename T>
ostream &operator<<(ostream &o, vector<T> v)
{
    if (v.size() > 0)
        o << v[0];
    for (unsigned i = 1; i < v.size(); i++)
        o << " " << v[i];
    return o << " ";
}
template <typename U, typename V>
ostream &operator<<(ostream &o, pair<U, V> p)
{
    return o << "(" << p.first << ", " << p.second << ") ";
}
template <typename T>
istream &operator>>(istream &in, vector<T> &v)
{
    for (unsigned i = 0; i < v.size(); i++)
        in >> v[i];
    return in;
}
template <typename T>
istream &operator>>(istream &in, pair<T, T> &p)
{
    in >> p.X;
    in >> p.Y;
    return in;
}
template <typename T>
ostream &operator<<(ostream &o, set<T> v)
{
    TRV(v)
    o << it << " ";
    return o << endl;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, map<T, U> v)
{
    TRV(v)
    o << it << " ";
    return o << endl;
}
template <typename T>
T &&vmin(T &&val)
{
    return std::forward<T>(val);
}

template <typename T0, typename T1, typename... Ts>
auto vmin(T0 &&val1, T1 &&val2, Ts &&... vs)
{
    return (val1 < val2) ? vmin(val1, std::forward<Ts>(vs)...) : vmin(val2, std::forward<Ts>(vs)...);
}
template <typename T>
T &&vmax(T &&val)
{
    return std::forward<T>(val);
}

template <typename T0, typename T1, typename... Ts>
auto vmax(T0 &&val1, T1 &&val2, Ts &&... vs)
{
    return (val1 > val2) ? vmax(val1, std::forward<Ts>(vs)...) : vmax(val2, std::forward<Ts>(vs)...);
}
struct custom_hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
int main(int argc, char *argv[])
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(20);
    lo n;
    cin >> n;
    lo dp[2][26][26];
    memset(dp, -1LL, sizeof(dp));
    REP(0, n)
    {
        lo turn = i % 2;
        lo other = turn ^ 1;
        string s;
        cin >> s;
        lo x = s[0] - 'a';
        lo y = s.back() - 'a';
        REPV(j, 0, 26)
        if (dp[other][j][x] != -1)
            dp[turn][j][y] = max(dp[turn][j][y], dp[other][j][x] + lo(s.length()));
        dp[turn][x][y] = max(dp[turn][x][y], lo(s.length()));
        REPV(j, 0, 26)
        dp[other][j][y] = dp[turn][j][y];
        dp[other][x][y] = dp[turn][x][y];
    }
    lo ans = 0;
    print_matrix(dp[0], n, n);
    REP(0, n)
    {
        ans = max(ans, dp[n & 1][i][i]);
    }
    cout << ans << endl;
    return 0;
}


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
#include <cassert>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MEM(a,b) memset((a),(b),sizeof(a))
const LL INF = 1e9 + 7;
const int N = 3e5 + 10;
int dp[2][26][26];

int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n;
	int o = 0;
	cin >> n;
	while (n--)
	{
		o ^= 1;
		memcpy(dp[o], dp[o ^ 1], sizeof(dp[o]));
		string str;
		cin >> str;
		int x = str.front() - 'a';
		int y = str.back() - 'a';
		int len = str.length();
		dp[o][x][y] = max(dp[o][x][y], len);
		for (int i = 0; i < 26; i++)
		{
			if (dp[o ^ 1][i][x])
			{
				dp[o][i][y] = max(dp[o][i][y], dp[o ^ 1][i][x] + len);
			}
		}
	}
	int ans = 0;
	for (int i = 0; i < 26; i++) ans = max(ans, dp[o][i][i]);
	cout << ans << endl;
	return 0;
}


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
#include <cassert>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MEM(a,b) memset((a),(b),sizeof(a))
const LL INF = 1e9 + 7;
const int N = 3e5 + 10;
int dp[2][26][26];
char str[N];
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n;
	int o = 0;
	scanf("%d", &n);
	while (n--)
	{
		o ^= 1;
		memcpy(dp[o], dp[o ^ 1], sizeof(dp[o]));
		scanf("%s", str);
		int len = strlen(str);
		int x = str[0] - 'a';
		int y = str[len - 1] - 'a';
		dp[o][x][y] = max(dp[o][x][y], len);
		for (int i = 0; i < 26; i++)
		{
			if (dp[o ^ 1][i][x])
			{
				dp[o][i][y] = max(dp[o][i][y], dp[o ^ 1][i][x] + len);
			}
		}
	}
	int ans = 0;
	for (int i = 0; i < 26; i++) ans = max(ans, dp[o][i][i]);
	printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int dp[500003][26];
int main()
{
    int n;
    cin >> n;
    string f[n];
    for(int i =0;i<n;i++) cin >> f[i];
    for(int i =1;i<=n;i++){
        for(int j =0;j<26;j++){
            if( f[i-1][(int) f[i-1].length()-1] -'a' == j) {
                    int x = f[i-1].length();
                 dp[i][j] = max(dp[i-1][(int)f[i-1][0]-'a' ] +x,dp[i-1][j]);
            }
            else {
                dp[i][j] = dp[i-1][j];
            }
        }
    }
    int mx =0;
    for(int i =0;i<n;i++){
       if(f[i][0] == f[i][f[i].length()-1]) mx = max(mx,1);

       mx = max(mx,dp[n][f[i][0]-'a'] - dp[i][f[i][0]-'a']);
    }
    cout << mx <<endl;

    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int dp[500003][26];
int cfmx[26];
int main()
{
    int n;
    cin >> n;
    string f[n];
    for(int i =0;i<n;i++) cin >> f[i];
    for(int i =1;i<=n;i++){
          int x = f[i-1].length();
          int lst = f[i-1][(int) f[i-1].length()-1] -'a';
          int frst = (int)f[i-1][0]-'a';
        for(int j =0;j<26;j++){
            if( lst == j) {
                if(dp[i-1][frst]==0 && cfmx[frst]!=0) dp[i][j] = x+cfmx[frst];
                else if(dp[i-1][frst]!=0) dp[i][j] = max(dp[i-1][frst]+x,dp[i-1][j]);
            }
            else {
                dp[i][j] = dp[i-1][j];
            }
        }
        cfmx[lst] = max(cfmx[lst],x);
    }
    int mx =0;
    for(int i =0;i<n;i++){
        int z = f[i].length();
       if(f[i][0] == f[i][f[i].length()-1]) mx = max(mx,z);
       if(i!=n-1)mx = max(mx,dp[n][f[i][0]-'a'] - dp[i][f[i][0]-'a']);
    }

    cout << mx <<endl;

    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int dp[26][26];
string d[500002];
int main()
{

    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    for(int i =0;i<n;i++){
       cin >> d[i];
    }
    int ans = 0;
    memset(dp,-1,sizeof dp);
    int sz = d[0].length();
    dp[d[0][0]-'a'][d[0][sz-1]-'a'] = sz;
    for(int i =1;i<n;i++){
        int f = d[i][0]-'a';
        int sz = d[i].length();
        int l = d[i][sz-1]-'a';
        dp[f][l]=max(0,dp[f][l]);
        for(int j=0;j<26;j++){
            if(dp[j][f]!=-1) {
                    dp[j][l] = max(dp[j][l],dp[j][f]+sz);
            }
        }
        dp[f][l] = max(dp[f][l],sz);
    }
    for(int i=0;i<26;i++) ans = max(ans,dp[i][i]);
    cout << ans;

    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int dp[500003][26];
int main()
{
    int n;
    cin >> n;
    string f[n];
    for(int i =0;i<n;i++) cin >> f[i];
    for(int i =1;i<=n;i++){
        for(int j =0;j<26;j++){
            if( f[i-1][(int) f[i-1].length()-1] -'a' == j) {
                    int x = f[i-1].length();
                 dp[i][j] = max(dp[i-1][(int)f[i-1][0]-'a' ] +x,dp[i-1][j]);
            }
            else {
                dp[i][j] = dp[i-1][j];
            }
            cout << dp[i][j] << " " ;
        }
        cout << endl;
    }
    int mx =0;
    for(int i =0;i<n;i++){
       if(f[i][0] == f[i][f[i].length()-1]) mx = max(mx,1);

       mx = max(mx,dp[n][f[i][0]-'a'] - dp[i][f[i][0]-'a']);
    }
    cout << mx <<endl;

    return 0;
}


#include <iostream>
#include <string>
#include <cstring>

int dp[26][26];

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    memset(dp, -1, sizeof(dp));

    int N;  std::cin>>N;
    while (N--) {
        std::string name;  std::cin>>name;
        int len = name.length();
        int i = name.front() - 'a';
        int j = name.back() - 'a';
        for (int k = 0; k < 26; ++k) {
            if (dp[k][i] == -1)
                continue;
            dp[k][j] = std::max(dp[k][j], dp[k][i] + len);
        }
        dp[i][j] = std::max(dp[i][j], len);
    }

    int ans = 0;
    for (int k = 0; k < 26; ++k)
        ans = std::max(ans, dp[k][k]);
    std::cout<<ans<<"\n";

    return 0;
}


#include <iostream>
#include <string>
#include <cstring>

int dp[26][26];

int main() {
    memset(dp, -1, sizeof(dp));

    int N;  std::cin>>N;
    while (N--) {
        std::string name;  std::cin>>name;
        int len = name.length();
        int i = name.front() - 'a';
        int j = name.back() - 'a';
        for (int k = 0; k < 26; ++k) {
            if (dp[k][i] == -1)
                continue;
            dp[k][j] = std::max(dp[k][j], dp[k][i] + len);
        }
        dp[i][j] = std::max(dp[i][j], len);
    }

    int ans = 0;
    for (int k = 0; k < 26; ++k)
        ans = std::max(ans, dp[k][k]);
    std::cout<<ans<<"\n";

    return 0;
}


//Mir_123
//@Shashank Keshav
//Chutiya_Coder
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pinf INT_MAX
#define minf INT_MIN
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define bin binary_search
#define vmin *min_element
#define vmax *max_element
#define pqd priority_queue<ll>
#define pqa priority_queue <ll, vector<ll>, greater<ll> >
#define in insert
#define mod 1000000007
#define pi pair<ll,ll>
#define println(arr,a,b) for(ll i=a;i<b;i++) cout<<arr[i]<<" "; cout<<endl;
#define print(arr,a,b)  for(ll i=a;i<b;i++) cout<<arr[i]<<endl;
#define mir ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)

//Your Code starts from here

int main()
{
	mir;
	ll n;
	cin>>n;
	vector<string>king(n);
	vector<vector<ll> >dyn(26,vector<ll>(26,0));
	for(ll i=0;i<n;i++)
	{
		cin>>king[i];
	}
	for(ll i=0;i<n;i++)
	{
		ll nl=king[i].size();
		nl--;
		for(ll j=0;j<26;j++)
		{
			if(dyn[j][king[i][0]-'a']==0)
				continue;
			dyn[j][king[i][nl]-'a']=max(dyn[j][king[i][nl]-'a'],dyn[j][king[i][0]-'a']+nl+1);
		}
		dyn[king[i][0]-'a'][king[i][nl]-'a']=max(dyn[king[i][0]-'a'][king[i][nl]-'a'],nl+1);
	}
	ll ans=0;
	for(ll i=0;i<26;i++)
	{
		ans=max(ans,dyn[i][i]);
	}
	cout<<ans;
	return 0;
}


//Mir_123
//@Shashank Keshav
#include<bits/stdc++.h>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<iterator>
using namespace std;
#define ll long long int
#define ld long double
#define inf INT_MAX
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define bin binary_search
#define vmin *min_element
#define vmax *max_element
#define mod 1000000007
#define pi pair<ll,ll>
#define print1(arr,a,b) for(ll i=a;i<b;i++) cout<<arr[i]<<" "; cout<<endl;
#define print(arr,a,b)  for(ll i=a;i<b;i++) cout<<arr[i]<<endl;

/*----------------------------------------------------------------------------------------------------------*/

int main(void)
{
	ll n;
	cin>>n;
	vector<string>name(n);
	for(ll i=0;i<n;i++)
	{
		cin>>name[i];
	}
	vector<vector<ll> >dp(26,vector<ll>(26,0));
	ll ans=0;
	for(ll i=0;i<n;i++)
	{
		ll l=name[i].size();
		for(ll j=0;j<26;j++)
		{
			if(dp[name[i][0]-'a'][j]==0)
				continue;
			dp[name[i][l-1]-'a'][j]=max(dp[name[i][l-1]-'a'][j],l+dp[name[i][0]-'a'][j]);
		}
		dp[name[i][l-1]-'a'][name[i][0]-'a']=max(dp[name[i][l-1]-'a'][name[i][0]-'a'],l);
	}
	for(ll i=0;i<26;i++)
	{
		ans=max(ans,dp[i][i]);
	}
	cout<<ans;
	return 0;
}


// #pragma GCC optimize("O2")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,avx2,fma,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void boost() {
	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
}

int main() {
	boost();
	int n;
	cin>>n;
	vector<string > a;
	for(int i=0;i<n;i++){
		string s;
		cin>>s;
		a.push_back(s);
	}
	long dp[26][26]={};
	for(int i=0;i<n;i++){
		int start=a[i][0]-'a';
		int flag=1;
		int end=a[i][a[i].size()-1]-'a';
		for(int i=0;i<26;i++){
			if(dp[i][start]){
				dp[i][end]=max(dp[i][end],dp[i][start]+(long)a[i].size());
				flag=0;
			}
		}
		if(flag)
		dp[start][end]=max(dp[start][end],(long)a[i].size());
	}
	long mx=0;
	for(int i=0;i<26;i++){
		for(int j=0;j<26;j++){
			mx=max(mx,dp[i][j]);
		}
	}
	cout<<mx;

}


// #pragma GCC optimize("O2")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,avx2,fma,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void boost() {
	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
}

int main() {
	boost();
	int n;
	cin>>n;
	vector<string > a;
	for(int i=0;i<n;i++){
		string s;
		cin>>s;
		a.push_back(s);
	}
	long dp[26][26]={};
	for(int i=0;i<n;i++){
		int start=a[i][0]-'a';
		// int flag=1;
		int end=a[i][a[i].size()-1]-'a';
		for(int j=0;j<26;j++){
			if(dp[j][start]){
				dp[j][end]=max(dp[j][end],dp[j][start]+(long)a[i].size());
				// flag=0;
			}
		}
		// if(flag)
		dp[start][end]=max(dp[start][end],(long)a[i].size());
	}
	long mx=0;
	for(int i=0;i<26;i++){
		// for(int j=0;j<26;j++){
			mx=max(mx,dp[i][i]);
		// }
	}
	cout<<mx;

}


/*****Enigma27*****/
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define    endl        '\n'
#define pll         pair<ll int,ll int>
#define vll          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)    for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define ios        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,i,j,k,l,sum=0,flag=0,t,ans=0,dp[26][26];
string s;
int main()
{
    ios
    cin>>n;
    for(i=0;i<n;i++){
        ll len,start,end;
        cin>>s;
        len=s.size();
        start=s[0]-'a';
        end=s[len-1]-'a';
        dp[start][end]=max(dp[start][end],len+(dp[start][end]*(start==end)));
        for(j=0;j<26;j++){
            if(dp[j][start]==0||j==start) continue;
            dp[j][end]=max(dp[j][end],dp[j][start]+len);
        }
    }
    for(i=0;i<26;i++) ans=max(ans,dp[i][i]);
    cout<<ans<<endl;
    return 0;
}


/*****Enigma27*****/
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define    endl        '\n'
#define pll         pair<ll int,ll int>
#define vll          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)    for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define ios        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,i,j,k,l,sum=0,flag=0,t,ans=0,dp[26][26];
string s;
int main()
{
    ios
    cin>>n;
    for(i=0;i<n;i++){
        ll len,start,end;
        cin>>s;
        len=s.size();
        start=s[0]-'a';
        end=s[len-1]-'a';
        for(j=0;j<26;j++){
            if(dp[j][start]==0) continue;
            dp[j][end]=max(dp[j][end],dp[j][start]+len);
        }
        dp[start][end]=max(dp[start][end],len);
    }
    for(i=0;i<26;i++) ans=max(ans,dp[i][i]);
    cout<<ans<<endl;
    return 0;
}


/*****Enigma27*****/
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define    endl        '\n'
#define pll         pair<ll int,ll int>
#define vll          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)    for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define ios        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,i,j,k,l,sum=0,flag=0,t,ans=0,dp[26][26];
string s;
int main()
{
    ios
    cin>>n;
    for(i=0;i<n;i++){
        ll len,start,end;
        cin>>s;
        len=s.size();
        start=s[0]-'a';
        end=s[len-1]-'a';
        dp[start][end]=max(dp[start][end],len);
        for(j=0;j<26;j++){
            if(dp[j][start]==0||j==start) continue;
            dp[j][end]=max(dp[j][end],dp[j][start]+len);
        }
    }
    cout<<ans<<endl;
    return 0;
}


/*****Enigma27*****/
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define    endl        '\n'
#define pll         pair<ll int,ll int>
#define vll          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)    for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define gcd(a,b)    __gcd((a),(b))
#define lcm(a,b)    ((a)*(b)) / gcd((a),(b))
#define ios        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,i,j,k,l,sum=0,flag=0,t,ans=0,dp[26][26];
string s;
int main()
{
    ios
    cin>>n;
    for(i=0;i<n;i++){
        ll len,start,end;
        cin>>s;
        len=s.size();
        start=s[0]-'a';
        end=s[len-1]-'a';
        dp[start][end]=max(dp[start][end],len);
        for(j=0;j<26;j++){
            if(dp[j][start]==0||j==start) continue;
            dp[j][end]=max(dp[j][end],dp[j][start]+len);
        }
    }
    for(i=0;i<26;i++) ans=max(ans,dp[i][i]);
    cout<<ans<<endl;
    return 0;
}


// ~/BAU/ACM-ICPC/Teams/A++/BlackBurn95
// ~/sudo apt-get Accpeted

#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long ll;

const int N = 500000, M = 27,inf = (int)1e9;
int s[N + 1], e[N + 1], len[N + 1];
int dp[N][M + 1][M + 1];
int n;
char tmp[11];

int calc(int i, int f, int l) {
	if (i == n) {
		if (f == l) return 0;
		return -inf;
	}

	int &ret = dp[i][f][l];

	if (ret != -1)
		return ret;

	ret = calc(i + 1, f, l);
	if (s[i] == l || l == 0)
		ret = max(ret, calc(i + 1, (l == 0 ? s[i] : f), e[i]) + len[i]);

	return ret;
}

int main() {
	scanf("%d", &n);

	for (int i = 0; i < n; i++) {
		scanf("%s", tmp);
		s[i] = tmp[0] - 'a' + 1;
		e[i] = s[i];
		len[i] = 0;
		for (; tmp[len[i]]; len[i]++)
			e[i] = tmp[len[i]] - 'a' + 1;

		//cout << s[i] << " , " << e[i] << " : " << len[i] << endl;
	}

	memset(dp, -1, sizeof(dp));
	printf("%d\n", max(calc(0, 0, 0),0));

	//system("pause");
	return 0;
}


// ~/BAU/ACM-ICPC/Teams/A++/BlackBurn95
// ~/sudo apt-get Accpeted

#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long ll;

const int N = 500000, M = 27,inf = (int)1e9;
int s[N + 1], e[N + 1], len[N + 1];
int dp[M+1];
int n,f;
char tmp[11];

int main() {
	scanf("%d", &n);

	for (int i = 0; i < n; i++) {
		scanf("%s", tmp);
		s[i] = tmp[0] - 'a' + 1;
		e[i] = s[i];
		len[i] = 0;
		for (; tmp[len[i]]; len[i]++)
			e[i] = tmp[len[i]] - 'a' + 1;

		//cout << s[i] << " , " << e[i] << " : " << len[i] << endl;
	}

	int ans = 0;

	for (int c = 1; c <= 27; c++) {
		for (int i = 0; i <= 27; i++) dp[i] = -inf;
		dp[c] = 0;

		//cout << "***** " << c << endl;
		for (int i = 0; i < n; i++) {
			//cout << s[i] << " , " <<dp[s[i]]<<" \ " << e[i]<<" , "<<dp[e[i]] << " : ";
			dp[e[i]] = max(dp[e[i]], dp[s[i]] + len[i]);
			//cout << dp[c] << endl;
		}

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

	printf("%d\n", ans);

	return 0;
}


// ~/BAU/ACM-ICPC/Teams/A++/BlackBurn95
// ~/sudo apt-get Accpeted

#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long ll;

const int N = 500000, M = 27,inf = (int)1e9;
int s[N + 1], e[N + 1], len[N + 1];
int dp[N][M + 1];
int n,f;
char tmp[11];

int calc(int i,int c) {
	if (i == n) {
		if (f == c) return 0;
		return -inf;
	}

	int &ret = dp[i][c];

	if (ret != -1)
		return ret;

	ret = calc(i + 1, c);
	if (s[i] == c)
		ret = max(ret, calc(i + 1, e[i]) + len[i]);

	return ret;
}

int main() {
	scanf("%d", &n);

	for (int i = 0; i < n; i++) {
		scanf("%s", tmp);
		s[i] = tmp[0] - 'a' + 1;
		e[i] = s[i];
		len[i] = 0;
		for (; tmp[len[i]]; len[i]++)
			e[i] = tmp[len[i]] - 'a' + 1;

		//cout << s[i] << " , " << e[i] << " : " << len[i] << endl;
	}

	int ans = 0;
	for (int i = 1; i <= 27; i++) {
		memset(dp, -1, sizeof(dp));
		f = i;
		ans = max(ans, calc(0, i));
	}

	printf("%d\n", ans);

	//system("pause");
	return 0;
}


// ~/BAU/ACM-ICPC/Teams/A++/BlackBurn95
// ~/sudo apt-get Accpeted

#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long ll;

const int N = 500000, M = 27,inf = (int)1e9;
int s[N + 1], e[N + 1], len[N + 1];
int dp[N][M + 1][M + 1];
int n;
char tmp[11];

int calc(int i, int f, int l) {
	if (i == n) {
		if (f == l) return 0;
		return -inf;
	}

	int &ret = dp[i][f][l];

	if (ret != -1)
		return ret;

	ret = calc(i + 1, f, l);
	if (s[i] == l || l == 0)
		ret = max(ret, calc(i + 1, (l == 0 ? s[i] : f), e[i]) + len[i]);

	return ret;
}

int main() {
	scanf("%d", &n);

	for (int i = 0; i < n; i++) {
		scanf("%s", tmp);
		s[i] = tmp[0] - 'a' + 1;
		e[i] = s[i];
		len[i] = 0;
		for (; tmp[len[i]]; len[i]++)
			e[i] = tmp[len[i]] - 'a' + 1;

		//cout << s[i] << " , " << e[i] << " : " << len[i] << endl;
	}

	memset(dp, -1, sizeof(dp));
	printf("%d\n", max(calc(0, 0, 0),0));

	system("pause");
	return 0;
}


#include <iostream>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <utility>
#include <cmath>
#include <algorithm>
#include <unordered_map>
#include <queue>

#define lli long long int

using namespace std;

int main()
{
  lli i, n, len, possible=0, a[2][26] = {0}, m = -1;
  cin >> n;
  string s;

  for(i=0;i<n;i++)
  {
    cin >> s;
    len = s.size();

    a[0][s[len-1]-97] = ((a[0][s[len-1]-97]) > (a[0][s[0]-97]+len)) ? (a[0][s[len-1]-97]) : (a[0][s[0]-97]+len);

    if(a[0][s[0]-97]>0  ||  a[0][s[0]-97]==a[0][s[len-1]-97])
    {
      a[1][s[len-1]-97] = 1;
      possible = 1;
    }
  }

  if(possible)
  {
    for(i=0;i<26;i++)
    {
      if(a[1][i]==1)
        if(a[0][i]>m)
          m=a[0][i];
    }

    cout << m << endl;
  }
  else
    cout << 0 << endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <utility>
#include <cmath>
#include <algorithm>
#include <unordered_map>
#include <queue>

#define lli long long int

using namespace std;

int main()
{
  lli i, j, maxim, n, len, possible=0, a[26][26] = {0}, m = -1, t1, t2;
  cin >> n;
  string s;
  char f, l;
  for(i=0;i<n;i++)
  {
    cin >> s;
    len = s.size();

    f = s[0];
    l = s[len-1];

    t1 = a[f-97][f-97];
    t2 = a[f-97][l-97];

    for(j=0;j<26;j++)
    {
      if(a[j][f-97]>0)
      {
        a[j][l-97] = max(a[j][l-97], a[j][f-97]+len);
        //cout << j << " " << l-97 << " " << a[j][l-97] <<  " .. " << j << " " << f-97 << " " << a[j][f-97] << "\n";
      }
    }

    //if(f!=l)
    a[f-97][l-97] = max(t1+len, max(len, t2));

  }

  for(i=0;i<26;i++)
  {
    if(a[i][i]>m)
      m=a[i][i];
  }

  if(m>-1)
    cout << m << endl;
  else
    cout << 0 << endl;

  return 0;
}


#include <iostream>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <utility>
#include <cmath>
#include <algorithm>
#include <unordered_map>
#include <queue>

#define lli long long int

using namespace std;

int main()
{
  lli i, j, maxim, n, len, possible=0, a[26][26] = {0}, m = -1, t1, t2;
  cin >> n;
  string s;
  char f, l;
  for(i=0;i<n;i++)
  {
    cin >> s;
    len = s.size();

    f = s[0];
    l = s[len-1];

    //t1 =
    for(j=0;j<26;j++)
    {
      if(f!=l &&  a[j][f-97]>0)
      {
        a[j][l-97] = max(a[j][l-97], a[j][f-97]+len);
        //cout << j << " " << l-97 << " " << a[j][l-97] <<  " .. " << j << " " << f-97 << " " << a[j][f-97] << "\n";
      }
    }

    a[f-97][l-97] = max(a[f-97][f-97]+len, max(len, a[f-97][l-97]));

  }

  for(i=0;i<26;i++)
  {
    if(a[i][i]>m)
      m=a[i][i];
  }

  if(m>-1)
    cout << m << endl;
  else
    cout << 0 << endl;

  return 0;
}


#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>
using namespace std;
#define ll long long int
int dp[30][30];
int main()
{
    int n;
    cin>>n;
    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;
}