import sys from collections import defaultdict def main(): n = int(sys.stdin.readline()) A = list(map(int, sys.stdin.readline().split())) if n < 3: print(0) return max_length = 0 single = defaultdict(int) pair = defaultdict(int) for c in A: new_pair = defaultdict(int) new_single = defaultdict(int) # Process existing pairs for (a, b), length in pair.items(): if a != b and b != c and a != c: if (b > a and b > c) or (b < a and b < c): key = (b, c) if length + 1 > new_pair.get(key, 0): new_pair[key] = length + 1 # Process existing singles to form new pairs for a, length in single.items(): key = (a, c) if 2 > new_pair.get(key, 0): new_pair[key] = 2 # Update max_length from new_pair for key, length in new_pair.items(): if length > max_length: max_length = length # Update pair and single for key, val in new_pair.items(): if val > pair.get(key, 0): pair[key] = val for key, val in new_single.items(): if val > single.get(key, 0): single[key] = val # Update single for current c single[c] = max(single.get(c, 0), 1) # Check if max_length is at least 3 print(max_length if max_length >= 3 else 0) if __name__ == "__main__": main()