from sys import stdin def main(): n = int(stdin.readline()) valid_strings = [] for _ in range(n): s = stdin.readline().strip() good = True for i in range(1, len(s)): if s[i] < s[i-1]: good = False break if good: start = s[0] end = s[-1] length = len(s) valid_strings.append((start, end, length)) dp = [0] * 26 max_dp = [0] * 26 dp_prev = 0 # Represents the initial state (no characters chosen) # Initialize max_dp based on initial dp values current_max = 0 for i in range(26): current_max = max(current_max, dp[i]) max_dp[i] = current_max for start, end, length in valid_strings: a = ord(start) - ord('a') b = ord(end) - ord('a') # Calculate the maximum possible value before appending this string max_before = max(dp_prev, max_dp[a]) candidate = max_before + length # Update dp[b] if candidate is larger if candidate > dp[b]: dp[b] = candidate # Recalculate the max_dp array after updating dp current_max = 0 new_max_dp = [] for i in range(26): current_max = max(current_max, dp[i]) new_max_dp.append(current_max) max_dp = new_max_dp # The answer is the maximum value between the initial state and the best in max_dp print(max(max_dp[-1], dp_prev)) if __name__ == "__main__": main()