def is_good(s): for i in range(len(s) - 1): if s[i] > s[i + 1]: return False return True n = int(input()) good = [] for _ in range(n): s = input().strip() if is_good(s): first = s[0] last = s[-1] length = len(s) good.append((first, last, length)) # Sort the good strings by their starting character good.sort(key=lambda x: x[0]) dp = [0] * 26 max_dp = [0] * 26 for first_char, last_char, length in good: s_start = ord(first_char) - ord('a') s_end = ord(last_char) - ord('a') current_max_prev = max_dp[s_start] new_val = current_max_prev + length if new_val > dp[s_end]: dp[s_end] = new_val # Update max_dp array current_max = 0 for i in range(26): if dp[i] > current_max: current_max = dp[i] max_dp[i] = current_max print(max(dp))