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

int main() {
	int n;
	cin >> n;
	vector<string> s;
	for(int i = 0; i < n; i++) {
		string x;
		cin >> x;
		auto y = x;
		sort(y.begin(), y.end());
		if(x == y) s.push_back(x);
	}
	sort(s.begin(), s.end());
	vector res(26, 0);
	for(auto &v : s) {
		int f = v.front() - 'a';
		int b = v.back() - 'a';
		int tmp = 0;
		for(int i = 0; i <= f; i++) tmp = max(tmp, res[i]);
		res[b] = max(res[b], tmp + (int)v.size());
	}
	cout << *max_element(res.begin(), res.end()) << '\n';
	return 0;
}