#!/usr/bin/env python3 # from typing import * import sys import io import math import collections import decimal import itertools import bisect import heapq def input(): return sys.stdin.readline()[:-1] # sys.setrecursionlimit(1000000) # _INPUT = """4 # abc # ab # bbccdd # de # """ # sys.stdin = io.StringIO(_INPUT) N = int(input()) S = [input() for _ in range(N)] L = [] for s in S: ok = True for i in range(len(s)-1): if s[i] > s[i+1]: ok = False break if ok: L.append((ord(s[0])-ord('a'), ord(s[-1])-ord('a'), len(s))) # L.sort(key=lambda x: (x[0], x[1], -x[2])) dp = [0] * 26 for k1, k2, l in L: dp1 = [0] * 26 for k in range(26): dp1[k] = max(dp1[k], dp[k]) if k <= k1: dp1[k2] = max(dp1[k2], dp[k]+l) dp = dp1 ans = max(dp) print(ans)