import sys input = sys.stdin.readline print = sys.stdout.write N = int(input()) tanghuru = list(map(int, input().split())) fruit_count = {} start = end = 0 maxLen = 0 while True : if len(fruit_count) <= 2 : maxLen = max(maxLen, end - start) if end == N : break fruit_count[tanghuru[end]] = fruit_count.get(tanghuru[end],0) + 1 end += 1 else : fruit_count[tanghuru[start]] -= 1 if fruit_count[tanghuru[start]] == 0 : fruit_count.pop(tanghuru[start]) start += 1 print(str(maxLen)+'\n')