def main(): import sys input = sys.stdin.read().split() idx = 0 N = int(input[idx]) idx += 1 good_strings = [] for _ in range(N): s = input[idx] idx += 1 valid = True for i in range(1, len(s)): if s[i] < s[i-1]: valid = False break if valid: start = s[0] end = s[-1] s_idx = ord(start) - ord('a') e_idx = ord(end) - ord('a') good_strings.append((s_idx, e_idx, len(s))) dp = [0] * 26 max_dp = [0] * 26 for s_idx, e_idx, l in good_strings: current_max = max_dp[s_idx] candidate = current_max + l if candidate > dp[e_idx]: dp[e_idx] = candidate # Update max_dp 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[-1]) if __name__ == '__main__': main()