#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <ios>
#include <iomanip>
#include <queue>
#include <numeric>
#include <map>
#include <set>
#include <sstream>
#include <bitset>
#include <climits>
#include <stack>
#include <regex>
#include <functional>
#include <random>
#ifdef _WIN64
#  include <atcoder/all>
#endif
#ifdef _MSC_VER
#  include <intrin.h>
#  define __builtin_popcount __popcnt
#  define __builtin_popcountl __popcnt64
#endif

using namespace std;

#define ll long long
#define rep(i, init, n) for(ll i = init; i < (ll)n; i++)
#define rrep(i, init, n) for(ll i = init; i >= (ll)n; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll)(x.size())
#define Out(x) cout << x << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define Ans cout << ans << endl
#define PI 3.14159265358979
#define MOD 998244353

const int inf32 = INT_MAX / 2;
const ll inf64 = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }

// -------------------------------------------------------------------------------------------------

int main()
{
	int n; cin >> n;
	vector<string> s(n);
	rep(i, 0, n) cin >> s[i];

	sort(all(s), [](string x, string y)
	{
		if (x.back() <= y[0] && y.back() <= x[0]) return x < y;
		else return x.back() <= y[0];
	});
	s.insert(s.begin(), "");

	vector dp(n + 1, vector<int>(26, -inf32));
	dp[0][0] = 0;

	rep(i, 1, n + 1)
	{
		string str = s[i];
		sort(all(str));
		bool ok = s[i] == str;

		rep(j, 0, 26)
		{
			chmax(dp[i][j], dp[i - 1][j]);
			if (ok && j <= (s[i][0] - 'a')) chmax(dp[i][s[i].back() - 'a'], dp[i - 1][j] + (int)s[i].length());
		}
	}

	int ans = -inf32;
	rep(i, 0, 26) chmax(ans, dp[n][i]);

	Ans;

	return 0;
}