n = int(input()) strings = [] for _ in range(n): s = input().strip() is_good = True for i in range(len(s) - 1): if s[i] > s[i + 1]: is_good = False break if is_good: s_start = s[0] s_end = s[-1] length = len(s) strings.append((s_start, s_end, length)) strings.sort(key=lambda x: x[0]) dp = [0] * 26 prefix_max = [0] * 26 for s_start, s_end, length in strings: s_start_ord = ord(s_start) - ord('a') max_prev = prefix_max[s_start_ord] current_len = max_prev + length s_end_ord = ord(s_end) - ord('a') if current_len > dp[s_end_ord]: dp[s_end_ord] = current_len current_max = 0 for i in range(26): if dp[i] > current_max: current_max = dp[i] prefix_max[i] = current_max print(max(dp))