N = int( input() )
A = [ int(x) for x in input().split() ]
from collections import defaultdict
inside = defaultdict(int)
answer = 1
r = 0
for l in range(N):
if l > 0:
inside[ A[l-1] ] -= 1
if inside[ A[l-1] ] == 0: del inside[ A[l-1] ]
while r < N and (len(inside) < 2 or A[r] in inside):
inside[ A[r] ] += 1
r += 1
answer = max( answer, r-l )
print(answer)
TiA9IGludCggaW5wdXQoKSApCkEgPSBbIGludCh4KSBmb3IgeCBpbiBpbnB1dCgpLnNwbGl0KCkgXQoKZnJvbSBjb2xsZWN0aW9ucyBpbXBvcnQgZGVmYXVsdGRpY3QKaW5zaWRlID0gZGVmYXVsdGRpY3QoaW50KQoKYW5zd2VyID0gMQpyID0gMApmb3IgbCBpbiByYW5nZShOKToKICAgIGlmIGwgPiAwOgogICAgICAgIGluc2lkZVsgQVtsLTFdIF0gLT0gMQogICAgICAgIGlmIGluc2lkZVsgQVtsLTFdIF0gPT0gMDogZGVsIGluc2lkZVsgQVtsLTFdIF0KICAgIHdoaWxlIHIgPCBOIGFuZCAobGVuKGluc2lkZSkgPCAyIG9yIEFbcl0gaW4gaW5zaWRlKToKICAgICAgICBpbnNpZGVbIEFbcl0gXSArPSAxCiAgICAgICAgciArPSAxCiAgICBhbnN3ZXIgPSBtYXgoIGFuc3dlciwgci1sICkKCnByaW50KGFuc3dlcik=