def prime(n):
    pr=[True for i in range(n + 1)]
    p=2
    while(p * p <= n):
        if (pr[p]==True):
            for i in range(p*2,n+1,p):
                pr[i]=False
        p+=1
    pr[0],pr[1]=False,False
    if pr[n]==True:
        return "YES"
    else:
        return "NO"
for n in range(int(input())):
    i=int(input())
    if i==1:
        print(0)
    elif i%2==0:
        if i==2:
            print(1)
        else:
            print(2)
    elif i%2!=0:
        if i==3:
            print(2)
        elif prime(i)=="YES" and i!=3:
            print(i-1)
        elif i%3==0:
            print(3)
        else:
            print(4)


def prime(n):
    pr=[True for i in range(n + 1)]
    p=2
    while(p * p <= n):
        if (pr[p]==True):
            for i in range(p*2,n+1,p):
                pr[i]=False
        p+=1
    pr[0],pr[1]=False,False
    if pr[n]==True:
        return "YES"
    else:
        return "NO"
for n in range(int(input())):
    i=int(input())
    if i==1:
        print(0)
    elif i%2==0:
        print(2)
    elif i%2!=0:
        if i==3:
            print(2)
        elif prime(i)=="YES" and i!=3:
            print(i)
        else:
            print(3)


for n in range(int(input())):
    i=int(input())
    if i==1:
        print(0)
    elif i==2:
        print(1)
    elif i==3:
        print(2)
    elif i%2==0:
        print(2)
    else:
        print(3)


def prime(n):
    pr=[True for i in range(n + 1)]
    p=2
    while(p * p <= n):
        if (pr[p]==True):
            for i in range(p*2,n+1,p):
                pr[i]=False
        p+=1
    pr[0],pr[1]=False,False
    if pr[n]==True:
        return "YES"
    else:
        return "NO"
for n in range(int(input())):
    i=int(input())
    if i==1:
        print(0)
    elif i%2==0:
        if i==2:
            print(1)
        else:
            print(2)
    elif i%2!=0:
        if i==3:
            print(2)
        elif prime(i)=="YES" and i!=3:
            print(i)
        else:
            print(3)


def prime(n):
    pr=[True for i in range(n + 1)]
    p=2
    while(p * p <= n):
        if (pr[p]==True):
            for i in range(p*2,n+1,p):
                pr[i]=False
        p+=1
    pr[0],pr[1]=False,False
    if pr[n]==True:
        return "YES"
    else:
        return "NO"
for n in range(int(input())):
    i=int(input())
    if i==1:
        print(0)
    elif i%2==0:
        if i==2:
            print(1)
        else:
            print(2)
    elif i%2!=0:
        if i==3:
            print(2)
        elif prime(i)=="YES" and i!=3:
            print(i-1)
        else:
            print(3)


def prime(n):
    pr=[True for i in range(n + 1)]
    p=2
    while(p * p <= n):
        if (pr[p]==True):
            for i in range(p*2,n+1,p):
                pr[i]=False
        p+=1
    pr[0],pr[1]=False,False
    if pr[n]==True:
        return "YES"
    else:
        return "NO"
for n in range(int(input())):
    i=int(input())
    if i==1:
        print(0)
    elif i%2==0:
        if i==2:
            print(1)
        else:
            print(2)
    elif i%2!=0:
        if i==3:
            print(2)
        elif prime(i)=="YES" and i!=3:
            print(i-1)
        elif i%3==0
            print(3)
        else:
            print(4)


def factor(n):
    count=0
    while n > 1:
        if n > 3:
            for i in range((n//2),1,-1):
                if n%i == 0:
                    n=n//i
                    count+=1
                    break
        elif n == 3:
            count+=2
            n-=2
        else:
            count+=1
            n-=1
    return count

if __name__=="__main__":
    t= int(input())
    for j in range(t):
        n = int(input())
        f = factor(n)
        print(f)


def factor(n):
    count=0
    while n > 1:
        if n > 3:
            for i in range((n//2),1,-1):
                if n%i == 0:
                    n=n//i
                    count+=1
                    break
        elif n == 3:
            count+=2
            n-=2
        else:
            count+=1
            n-=1
    return count

if __name__=="__main__":
    t= int(input())
    print(3//2)
    for j in range(t):
        n = int(input())
        f = factor(n)
        print(f)


def factor(n):
    count = 0
    if n > 3:
        if n%2 == 0:
            return 2
        else:
            return 3
    if n == 3:
        return 2
    if n == 2:
        return 1
    return count

if __name__=="__main__":
    t= int(input())
    for j in range(t):
        n = int(input())
        f = factor(n)
        print(f)


def factor(n,count):
    if n > 3:
        for i in range((n//2),1,-1):
            if n%i == 0:
                n=n/i
                count+=1
                factor(n,count)
    if n == 3:
        count+=2
        n-=2
    if n == 2:
        count+=1
        n-=1
    return count

if __name__=="__main__":
    t= int(input())
    for j in range(t):
        n = int(input())
        count=0
        f = factor(n,count)
        print(f)


from math import sqrt

casos = int(input())

def get_res(n):
    print(n)
    if n == 1:
        return 0
    for i in range(2, int(sqrt(n))+1):
        if n % i == 0:
            return min(get_res(i), get_res(n-1)) + 1
    return get_res(n-1) + 1

for h in range(casos):
    n = int(input())
    print(get_res(n))


from math import sqrt

casos = int(input())

def get_res(n):
    if n == 1:
        return 0
    for i in range(2, int(sqrt(n))+1):
        if n % i == 0:
            return min(get_res(i), get_res(n-1)) + 1
    return get_res(n-1) + 1

for h in range(casos):
    n = int(input())
    print(get_res(n))


from math import sqrt

casos = int(input())

def get_res(n):
    if n <= 1:
        return 0
    for i in range(2, int(sqrt(n))+1):
        if n % i == 0:
            return get_res(i) + 1
    return get_res(n-1) + 1

for h in range(casos):
    n = int(input())
    print(min(get_res(n-1)+1, get_res(n) ) )


from math import sqrt

casos = int(input())

for h in range(casos):
    n = int(input())
    steps = 0
    while n != 1:
        p = -1
        for i in range(int(sqrt(n)), 1, -1):
            if n % i == 0:
                p = i
                break
        if p == -1:
            n = n-1
        else:
            n = p
        steps = steps + 1
    print(steps)


from math import sqrt

casos = int(input())

for h in range(casos):
    n = int(input())
    steps = 0
    while n != 1:
        p = -1
        for i in range(2, int(sqrt(n))+1):
            if n % i == 0:
                p = i
                break
        if p == -1:
            n = n-1
        else:
            n = p
        steps = steps + 1
    print(steps)


def sol(n):
    return ("2" if n%2==0 else "3")
for i in range(int(input())):
    print(sol(int(input())))


def sol(n)
    return ("2" if n%2==0 else "3")
for i in range(int(input())):
    x=int(input())
    if x>3:
        print(sol(x))
    else:
        print(x-1)


def sol(n)
    return ("2" if n%2==0 else "3")
for i in range(int(input())):
    print(sol(int(input())))


def sol(n):
    return ("2" if n%2==0 else "3")
for i in range(int(input())):
    x=int(input())
    if x>3:
        print(sol(x))
    else:
        print(x-1)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 19 20:40:21 2020

@author: mrinalini
"""

t=int(input())
for _ in range(t):
    n=int(input())
    if(n==1):
        print(0)
    elif(n==2):
        print(1)
    elif(n==3):
        print(2)
    else:
        if(n%2==0):
            print(2)
        else:
            print(3)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 19 20:40:21 2020

@author: mrinalini
"""

import math

t=int(input())
for _ in range(t):
    n=int(input())
    print(round(n**0.5))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 19 20:40:21 2020

@author: mrinalini
"""

import math

t=int(input())
for _ in range(t):
    n=int(input())
    if(n==1):
        print(0)
    else:

        print(round(n**0.5))


q = int(input())
for _ in range(q):
	n = int(input())
	i = 2
	pier = 1
	while i**2 <= n:
		if n%i == 0:
			pier = i
			break
		i += 1
	if pier == 1:
		print(n-1)
	else:
		print(pier)


q = int(input())
for _ in range(q):
	n = int(input())
	if n == 1:
		print(0)
	else:
		if n == 2:
			print(1)
		else:
			if n%2 == 0:
				print(2)
			else:
				print(3)


q = int(input())
for _ in range(q):
	n = int(input())
	i = 2
	pier = 1
	while i**2 <= n:
		if n%i == 0:
			pier = i
			break
	if pier == 1:
		print(n-1)
	else:
		print(pier)


q = int(input())
for _ in range(q):
	n = int(input())
	if n == 1:
		print(0)
	else:
		if n == 2:
			print(1)
		else:
			if n%2 == 0:
				print(2)
			else:
				if n == 3:
					print(2)
				else:
					print(3)


#!/usr/bin/env python3

from collections import *
from heapq import *
from itertools import *
from functools import *
from random import *
import sys

INF = float('Inf')

def genTokens(lines):
    for line in lines:
        for token in line.split():
            yield token
def nextFn(fn):
    return lambda it: fn(it.__next__())
nint = nextFn(int)

def genCases():
    it = genTokens(sys.stdin)
    T = nint(it)
    for _ in range(T):
        N = nint(it)
        yield N

primes = [True for _ in range(100000)]
primeSet = set()
primesSorted = []

def sieve():
    global primes
    global primeSet
    global primesSorted
    primes[0] = False
    primes[1] = False
    for p in range(2, 100000):
        if not primes[p]:
            continue
        primeSet.add(p)
        #print('%d,' % p)
        cursor = 2*p
        while cursor < 100000:
            primes[cursor] = False
            cursor += p
    primesSorted = [i for i, p in enumerate(primes) if p]

def main():
    sieve()
    for case in genCases():
        print(solve(case))

def solve(N):
    if N == 1:
        return 0
    if N in primeSet:
        return N-1
    else:
        for p in primesSorted:
            if N % p == 0:
                return p

main()


#!/usr/bin/env python3

from collections import *
from heapq import *
from itertools import *
from functools import *
from random import *
import sys

INF = float('Inf')

def genTokens(lines):
    for line in lines:
        for token in line.split():
            yield token
def nextFn(fn):
    return lambda it: fn(it.__next__())
nint = nextFn(int)

def genCases():
    it = genTokens(sys.stdin)
    T = nint(it)
    for _ in range(T):
        N = nint(it)
        yield N

primes = [True for _ in range(100000)]
primeSet = set()
primesSorted = []

def sieve():
    global primes
    global primeSet
    global primesSorted
    primes[0] = False
    primes[1] = False
    for p in range(2, 100000):
        if not primes[p]:
            continue
        primeSet.add(p)
        #print('%d,' % p)
        cursor = 2*p
        while cursor < 100000:
            primes[cursor] = False
            cursor += p
    primesSorted = [i for i, p in enumerate(primes) if p]

def main():
    sieve()
    for case in genCases():
        print(solve(case))

@lru_cache(None)
def solve(N):
    if N == 1:
        return 0
    if N in primeSet:
        return solve(N-1)+1
    else:
        for p in primesSorted:
            if N % p == 0:
                return solve(p)+1
    return solve(N-1)+1

main()


#!/usr/bin/env python3

from collections import *
from heapq import *
from itertools import *
from functools import *
from random import *
import sys

INF = float('Inf')

def genTokens(lines):
    for line in lines:
        for token in line.split():
            yield token
def nextFn(fn):
    return lambda it: fn(it.__next__())
nint = nextFn(int)

def genCases():
    it = genTokens(sys.stdin)
    T = nint(it)
    for _ in range(T):
        N = nint(it)
        yield N

primes = [True for _ in range(100000)]
primeSet = set()
primesSorted = []

def sieve():
    global primes
    global primeSet
    global primesSorted
    primes[0] = False
    primes[1] = False
    for p in range(2, 100000):
        if not primes[p]:
            continue
        primeSet.add(p)
        #print('%d,' % p)
        cursor = 2*p
        while cursor < 100000:
            primes[cursor] = False
            cursor += p
    primesSorted = [i for i, p in enumerate(primes) if p]

def main():
    sieve()
    for case in genCases():
        print(solve(case))

def solve(N):
    if N == 1:
        return 0
    if N in primeSet:
        return N-1
    else:
        for p in primesSorted:
            if N % p == 0:
                return p
    return N-1

main()


#!/usr/bin/env python3

from collections import *
from heapq import *
from itertools import *
from functools import *
from random import *
import sys

INF = float('Inf')

def genTokens(lines):
    for line in lines:
        for token in line.split():
            yield token
def nextFn(fn):
    return lambda it: fn(it.__next__())
nint = nextFn(int)

def genCases():
    it = genTokens(sys.stdin)
    T = nint(it)
    for _ in range(T):
        N = nint(it)
        yield N

primes = [True for _ in range(100000)]
primeSet = set()
primesSorted = []

def sieve():
    global primes
    global primeSet
    global primesSorted
    primes[0] = False
    primes[1] = False
    for p in range(2, 100000):
        if not primes[p]:
            continue
        primeSet.add(p)
        #print('%d,' % p)
        cursor = 2*p
        while cursor < 100000:
            primes[cursor] = False
            cursor += p
    primesSorted = sorted(primeSet)

def main():
    sieve()
    for case in genCases():
        print(solve(case))

@lru_cache(None)
def solve(N):
    if N == 1:
        return 0
    if N in primeSet:
        return solve(N-1)+1
    else:
        for p in primesSorted:
            if p >= N:
                break
            if N % p == 0:
                return solve(p)+1
    return solve(N-1)+1

main()


#!/usr/bin/env python3

from collections import *
from heapq import *
from itertools import *
from functools import *
from random import *
import sys

INF = float('Inf')

def genTokens(lines):
    for line in lines:
        for token in line.split():
            yield token
def nextFn(fn):
    return lambda it: fn(it.__next__())
nint = nextFn(int)

def genCases():
    it = genTokens(sys.stdin)
    T = nint(it)
    for _ in range(T):
        N = nint(it)
        yield N

#primes = [True for _ in range(100000)]
#primeSet = set()
#primesSorted = []
#
#def sieve():
#    global primes
#    global primeSet
#    global primesSorted
#    primes[0] = False
#    primes[1] = False
#    for p in range(2, 100000):
#        if not primes[p]:
#            continue
#        primeSet.add(p)
#        #print('%d,' % p)
#        cursor = 2*p
#        while cursor < 100000:
#            primes[cursor] = False
#            cursor += p
#    primesSorted = sorted(primeSet)

def main():
    # sieve()
    for case in genCases():
        print(solve(case))

def solve(N):
    if N == 1:
        return 0
    if N == 2:
        return 1
    if N == 3:
        return 2
    if N % 2 == 0:
        return 2
    else: # N % 2 == 1
        return 3

main()


import sys
input = sys.stdin.readline
import math

for _ in range(int(input())):
    n = int(input())
    i = 2
    count = 0
    while n>1:
        while i*i<=n:
            if n%i==0 and i!=n:
                count+=1
                n=i
                i=1
            i+=1
        n-=1
        count+=1
        i = 2
    print(count)


import sys
input = sys.stdin.readline
import math

def factors(n):
    i = 2
    factors = []
    while i * i <= n:
        if n % i:
            i += 1
        else:
            n //= i
            factors.append(i)
    if n > 1:
        factors.append(n)
    return factors
for _ in range(int(input())):
    n = int(input())
    i = math.ceil(math.sqrt(n))
    count = 0
    while n>1:
        while i>1:
            if n%i==0 and i!=n:
                count+=1
                n=n//i
                i=math.ceil(math.sqrt(n))+1
            i-=1
        n-=1
        count+=1
        i = math.ceil(math.sqrt(n))
    print(count)


import sys
input = sys.stdin.readline
import math

for _ in range(int(input())):
    n = int(input())
    if n==1:
        print(0)
    elif n==2:
        print(1)
    elif n==3:
        print(2)
    elif n&1:
        print(3)
    else:
        print(2)


t=int(input())

p=[]
se=[]

ls=10**5

for i in range(0,ls):
    se.append(0)

i=2
while i<ls:
    if se[i]==0:
        p.append(i)
        j=i*i
        while j<ls:
            se[j]=1
            j+=i
    i+=1

for test in range(0,t):
    n=int(input())
    c=0
    while n!=1:
        c+=1
        if n in p:
            n-=1
        else:
            for e in p:
                if n%e==0:
                    n=e
                    break
    print(c)


t=int(input())

for test in range(0,t):
    n=int(input())
    c=0
    if n==1:
        print(0)
    elif n==2:
        print(1)
    elif n%2==0 or n==3:
        print(2)
    else:
        print(3)


t=int(input())

for test in range(0,t):
    n=int(input())
    c=0
    if n==1:
        print(0)
    elif n==2:
        print(1)
    elif n%2==0:
        print(2)
    else:
        print(3)


def substract_or_devide():
    for _ in range(int(input())):
        x = int(input())
        if x == 1:
            print(0)
        elif x == 2:
            print(1)
        elif x == 3:
            print(2)
        elif (x % 2) == 0:
            print(2)
        else:
            print(3)

if __name__ == "__main__":
    substract_or_devide()


def gen_primes():
    out = [2]
    aux = [0] * (10 ** 5)
    i = 3
    while i * i < 10 ** 5:
        for j in range(i * i, 10 ** 5, i):
            aux[j] = 1
        i += 2
    for i in range(3, 10 ** 5, 2):
        if aux[i] == 0:
            out += [i]
    return out

def substract_or_devide():
    primes = gen_primes()
    # print(primes)
    for _ in range(int(input())):
        x = int(input())
        if x == 1:
            print(0)
            continue
        for prime in primes:
            if x == prime:
                print(prime - 1)
                break
            elif (x % prime) == 0:
                print(prime)
                break

if __name__ == "__main__":
    substract_or_devide()


def gen_primes():
    out = [2]
    aux = [0] * (10 ** 5)
    i = 3
    while i * i < 10 ** 5:
        for j in range(i * i, 10 ** 5, i):
            aux[j] = 1
        i += 2
    for i in range(3, 10 ** 5, 2):
        if aux[i] == 0:
            out += [i]
    return out

def substract_or_devide():
    primes = gen_primes()
    # print(primes)
    for _ in range(int(input())):
        x = int(input())
        found = False
        if x == 1:
            print(0)
            continue
        for prime in primes:
            if x == prime:
                print(prime - 1)
                found = True
                break
            elif (x % prime) == 0:
                print(prime)
                found = True
                break
        if not found:
            print(x - 1)

if __name__ == "__main__":
    substract_or_devide()


def substract_or_devide():
    for _ in range(int(input())):
        x = int(input())
        if x == 1:
            print(0)
        elif x == 2:
            print(1)
        elif x == 3:
            print(2)
        elif (x % 2) == 0:
            print(2)
        elif (x % 3) == 0:
            print(3)

if __name__ == "__main__":
    substract_or_devide()


import math;a=int(input())
def f(n):
    for i in range(2,int(math.sqrt(n))+1):
        if n%i==0:return n//i
    return 0
for i in range(a):
    b=int(input());c=0
    while b!=1:
        q=f(b)
        if q==0:b-=1
        else:b//=f(b)
        c+=1
    print(c)


a=int(input())
for i in range(a):
    b=int(input())
    if b==1:
        print(0)
    else:
        if b%2==0:
            if b==2:print(1)
            else:print(2)
        else:
            b-=1
            if b==2:print(2)
            else:print(3)


a=int(input())
for i in range(a):
    b,c=map(int,input().split());d=input();e=input();mi=300;ma=0;mi1=300;ma1=0;l=[0]*26;t=[0]*26;zq=0;gq=0
    for g in range(b):
        qr=ord(d[g]);qt=ord(e[g])
        if l[qr-97]==1:zq=1
        else:l[qr-97]+=1
        if l[qt-97]==1:gq=1
        else:l[qt-97]+=1
        if mi>qr:mi=qr
        if ma1<qt:ma1=qt
    if d==e:print("YES")
    elif zq==0 or gq==0:print("NO")
    elif ma1<mi:print("NO")
    else:print("YES")


t=int(input())
while(t!=0):
    t=t-1
    n=int(input())
    if(n==1):
        print('0')
    elif(n==2):
        print('1')
    elif(n%2==0 or n==3):
        print('2')
    else:
        print('3')


t=int(input())
while(t!=0):
    t=t-1
    n=int(input())
    c=0
    flag=0
    i=2
    while(True):
        while(n!=1 and i<=int(sqrt(n))):
            if(n%i==0):
                if(flag==0):
                    c=c+1
                    flag=1
                n=n//i
            else:
                i=i+1
        if(n!=1):
            n=n-1
            i=2
            flag=0
            c=c+1
        else:
            break
    print(c)


from math import *
t=int(input())
while(t!=0):
    t=t-1
    n=int(input())
    c=0
    flag=-1
    i=n-1
    while(True):
        while(n!=1 and i>=int(ceil(sqrt(n)))):
            if(n%i==0):
                if(flag!=1):
                    c=c+1
                    flag=1
                n=n//i
                i=n-1
            else:
                i=i-1
        if(n!=1):
            if(flag!=0):
                n=n-1
                i=n-1
                flag=0
                c=c+1
            else:
                n=n-1
                i=n-1
                flag=0
                c=c+1
        else:
            break
    print(c)


from math import *
t=int(input())
while(t!=0):
    t=t-1
    n=int(input())
    i=2
    c=0
    flag=0
    while(1):
        while(n!=1 and i<=int(sqrt())):
            if(n%i==0):
                n=n//i
                if(flag==0):
                    flag=1
                    c=c+1
            else:
                i=i+1
        if(n==1):
            break
        else:
            n=n-1
            flag=0
            c=c+1
    print(c)


from math import *
t=int(input())
while(t!=0):
    t=t-1
    n=int(input())
    c=0
    flag=0
    i=2
    while(True):
        while(n!=1 and i<=int(sqrt(n))):
            if(n%i==0):
                if(flag==0):
                    c=c+1
                    flag=1
                n=n//i
            else:
                i=i+1
        if(n!=1):
            if(flag!=0):
                n=n-1
                i=2
                flag=0
                c=c+1
        else:
            break
    print(c)


import sys

t = int(input())
for i in range(t):
    n = int(input())
    k = 1
    if n == 1:
        print(0)
        continue
    for j in range(2, int(n ** 0.5) + 1):
        if n % j == 0:
            k = 0
            print(j)
            break
    if k:
        print(n - 1)


import sys

t = int(input())
for i in range(t):
    n = int(input())
    k = 0
    while n != 1:
        f = 0
        for j in range(2, int(n ** 0.5) + 1):
            if n % j == 0:
                n //= n // j
                f = 1
                break
        if not f:
            n -= 1
        k += 1
    print(k)


t = int(input())
for i in range(t):
    n = int(input())
    if n == 1:
        print(0)
    elif n == 2:
        print(1)
    elif n == 3:
        print(2)
    elif n % 2 == 0:
        print(2)
    else:
        print(3)


for _ in range(int(input())):

    n = int(input())

    if n <= 1:
        print(0)
    if n == 2:
        print(1)

    if n % 2 == 0:
        print(2)
    else:
        print(3)


for _ in range(int(input())):

    n = int(input())

    if n <= 1:
        print(0)
    elif n == 2:
        print(1)
    elif n == 3:
        print(2)
    elif n % 2 == 0:
        print(2)
    else:
        print(3)


for _ in range(int(input())):

    n = int(input())

    if n <= 1:
        print(0)
    if n == 2:
        print(1)
    if n == 3:
        print(2)
    if n % 2 == 0:
        print(2)
    else:
        print(3)


'''
Light temp.
'''
from sys import stdin
from math import ceil,floor,log2,sqrt

def u():
    return [int(i) for i in next(stdin).split()]

for _ in range(int(input())):

    n = int(input())
    count = 0

    while n > 1:
        for i in range(n-1,1,-1):
            if n % i == 0:
                if n / i < n -1:
                    n = n //i
                    count += 1
                else:
                    n -= 1
                    count += 1
                break
        n -= 1
        count += 1
    print(count)


#include <bits/stdc++.h>
#define INF 123456789

using namespace std;

int v[4040], n;

int main ()
{
	set <int> st;
	map <int, int> mp;
	map <int, int>::iterator it;
	scanf ("%d", &n);

	for (int i = 0; i < n; i++)
	{
		scanf ("%d", &v[i]);
	}

	v[n++] = -500;

	int res = 0, bigger = 0;

	for (int i = 0; i < n; i++)
	{
		mp[v[i]]++;
	}

	for (it = mp.begin (); it != mp.end (); ++it)
	{
		if (it->second > bigger)
		{
			bigger = it->second;
		}
	}

	res = bigger;

	for (int i = 0; i < n; i++)
	{
		mp.clear ();
		for (int j = i + 1; j < n; j++)
		{
			st.clear ();
			while (j < n && v[j] != v[i])
			{
				if (st.find (v[j]) == st.end ())
				{
					mp[v[j]]++;
					st.insert (v[j]);
				}
				j++;
			}
		}
		int val, bigger = 0;
		for (it = mp.begin (); it != mp.end (); ++it)
		{
			if (it->second > bigger)
			{
				bigger = it->second;
				val = it->first;
			}
			else if (it->second == bigger && st.find (it->first) == st.end ())
			{
				bigger = it->second;
				val = it->first;
			}
		}

		int aux = bigger * 2;
		if (st.find (val) == st.end ()) aux++;
		res = max (res, aux);
	}

	if (n == 2) printf ("1\n");
	else printf ("%d\n", res);
}


#include <bits/stdc++.h>

using namespace std;

int main ()
{
	vector <int> v;
	int n, val;

	scanf ("%d", &n);

	for (int i = 0; i < n; i++)
	{
		scanf ("%d", &val);
		v.push_back (val);
	}

	if (n == 1)
	{
		printf ("1\n");
		return 0;
	}

	int res = 0;

	for (int i = 0; i < n; i++)
	{
		for (int j = i + 1; j < n; j++)
		{
			int cont = 2, k = j + 1;
			while (true)
			{
				int pos = lower_bound (v.begin () + k, v.end (), v[i]) - v.begin ();
				if (v[pos] == v[i]) cont++;
				else break;
				k = pos + 1;
				pos = lower_bound (v.begin () + k, v.end (), v[j]) - v.begin ();
				if (v[pos] == v[j]) cont++;
				else break;
				k = pos + 1;
			}
			res = max (res, cont);
			if (res > n - j + 2) break;
		}
		if (res > n - i + 2) break;
	}

	printf ("%d\n", res);
}


#include <bits/stdc++.h>
#define INF 123456789

using namespace std;

int v[4040], n, qtd[4040], mp[1001000];

int main ()
{
	vector <int> q;
	bitset <1000100> vis, vet;
	vet.set ();
	set <int> st;
	map <int, int> m;
	map <int, int>::iterator it;
	scanf ("%d", &n);

	for (int i = 0; i < n; i++)
	{
		scanf ("%d", &v[i]);
		m[v[i]]++;
	}

	v[n++] = 1000101;

	int res = 0, bigger = 0;

	for (it = m.begin (); it != m.end (); ++it)
	{
		if (it->second > bigger)
		{
			bigger = it->second;
		}
	}
	int cont = 0;
	res = bigger;

	for (int i = 0; i < n; i++)
	{
		if (vet[v[i]])
		{
			vet[v[i]] = 0;
			memset (qtd, 0, sizeof qtd);
			memset (mp, 0, sizeof mp);
			q.clear ();
			cont = 1;

			for (int j = i + 1; j < n; j++)
			{
				vis.set ();
				while (j < n && v[j] != v[i])
				{
					if (vis[v[j]])
					{
						if (qtd[mp[v[j]]] == 0)
						{
							mp[v[j]] = cont++;
							q.push_back (v[j]);
						}
						qtd[mp[v[j]]]++;
						vis[v[j]] = 0;
					}
					j++;
				}
			}
			int val, bigger = 0;
			for (int j = 0; j < (int)q.size (); j++)
			{
				if (qtd[mp[q[j]]] > bigger)
				{
					bigger = qtd[mp[q[j]]];
					val = q[j];
				}
				else if (qtd[mp[q[j]]] == bigger && vis[q[j]])
				{
					bigger = qtd[mp[q[j]]];
					val = q[j];
				}
			}

			int aux = bigger * 2;
			if (vis[val]) aux++;
			res = max (res, aux);
		}
	}

	if (n == 2) printf ("1\n");
	else printf ("%d\n", res);
}


#include <bits/stdc++.h>

using namespace std;

int main ()
{
	int n, v[4100];

	scanf ("%d", &n);

	for (int i = 0; i < n; i++)
	{
		scanf ("%d", &v[i]);
	}

	int res = 0;

	for (int i = 0; i < n - 1; i++)
	{
		for (int j = i + 1; j < n; j++)
		{
			int cont = 2;
			for (int k = j + 1; k < n; k++)
			{
				if (cont % 2 == 0)
				{
					if (v[k] == v[i]) cont++;
				}
				else if (v[k] == v[j]) cont++;
			}
			res = max (res, cont);
			//if (res > n - j + 2) break;
		}
		//if (res > n - i + 2) break;
	}

	printf ("%d\n", res);
}


#include <bits/stdc++.h>

using namespace std;

int main ()
{
	int n, v[4100];

	scanf ("%d", &n);

	for (int i = 0; i < n; i++)
	{
		scanf ("%d", &v[i]);
	}

	int res = 0;

	for (int i = 0; i < n - 1; i++)
	{
		for (int j = i + 1; j < n; j++)
		{
			int cont = 2;
			for (int k = j + 1; k < n; k++)
			{
				if (cont % 2 == 0)
				{
					if (v[k] == v[i]) cont++;
				}
				else if (v[k] == v[j]) cont++;
			}
			res = max (res, cont);
			if (res > n - j + 2) break;
		}
		if (res > n - i + 2) break;
	}

	printf ("%d\n", res);
}


#include <bits/stdc++.h>
#define INF 123456789

using namespace std;

int v[4040], n;

int main ()
{
	set <int> st;
	map <int, int> mp;
	map <int, int>::iterator it;
	scanf ("%d", &n);

	for (int i = 0; i < n; i++)
	{
		scanf ("%d", &v[i]);
		mp[v[i]]++;
	}

	v[n++] = -500;

	int res = 0, bigger = 0;

	for (it = mp.begin (); it != mp.end (); ++it)
	{
		if (it->second > bigger)
		{
			bigger = it->second;
		}
	}

	res = bigger;

	if (res > n / 2)
	{
		printf ("%d\n", res);
		return 0;
	}

	for (int i = 0; i < n; i++)
	{
		mp.clear ();
		for (int j = i + 1; j < n; j++)
		{
			st.clear ();
			while (j < n && v[j] != v[i])
			{
				if (st.find (v[j]) == st.end ())
				{
					mp[v[j]]++;
					st.insert (v[j]);
				}
				j++;
			}
		}
		int val, bigger = 0;
		for (it = mp.begin (); it != mp.end (); ++it)
		{
			if (it->second > bigger)
			{
				bigger = it->second;
				val = it->first;
			}
			else if (it->second == bigger && st.find (it->first) == st.end ())
			{
				bigger = it->second;
				val = it->first;
			}
		}

		int aux = bigger * 2;
		if (st.find (val) == st.end ()) aux++;
		res = max (res, aux);
	}

	if (n == 2) printf ("1\n");
	else printf ("%d\n", res);
}


#include <bits/stdc++.h>

using namespace std;

int main ()
{
	int n, v[4100];

	scanf ("%d", &n);

	for (int i = 0; i < n; i++)
	{
		scanf ("%d", &v[i]);
	}

	int res = 0;

	for (int i = 0; i < n; i++)
	{
		int cont = 0;
		for (int j = i; j < n; j++, cont++)
		{
			if (cont % 2 == 0)
			{
				if (v[j] != v[i]) break;
			}
			else if (v[j] != v[i + 1]) break;
		}

		res = max (res, cont);
	}

	printf ("%d\n", res);
}


#include <bits/stdc++.h>
#define INF 123456789

using namespace std;

int v[4040], n;
bool vis[1010101];

int main ()
{
	set <int> st;
	map <int, int> mp;
	map <int, int>::iterator it;
	scanf ("%d", &n);

	for (int i = 0; i < n; i++)
	{
		scanf ("%d", &v[i]);
		mp[v[i]]++;
	}

	v[n++] = 1000101;

	int res = 0, bigger = 0;

	for (it = mp.begin (); it != mp.end (); ++it)
	{
		if (it->second > bigger)
		{
			bigger = it->second;
		}
	}

	res = bigger;

	for (int i = 0; i < n; i++)
	{
		if (!vis[v[i]])
		{
			vis[v[i]] = true;
			mp.clear ();
			for (int j = i + 1; j < n; j++)
			{
				st.clear ();
				while (j < n && v[j] != v[i])
				{
					if (st.find (v[j]) == st.end ())
					{
						mp[v[j]]++;
						st.insert (v[j]);
					}
					j++;
				}
			}
			int val, bigger = 0;
			for (it = mp.begin (); it != mp.end (); ++it)
			{
				if (it->second > bigger)
				{
					bigger = it->second;
					val = it->first;
				}
				else if (it->second == bigger && st.find (it->first) == st.end ())
				{
					bigger = it->second;
					val = it->first;
				}
			}

			int aux = bigger * 2;
			if (st.find (val) == st.end ()) aux++;
			res = max (res, aux);
		}
	}

	if (n == 2) printf ("1\n");
	else printf ("%d\n", res);
}


#include <bits/stdc++.h>
#define INF 123456789

using namespace std;

int v[4040], n, qtd[4040], mp[1001000];

int main ()
{
	vector <int> q;
	bitset <1000100> vis, vet;
	vet.set ();
	set <int> st;
	map <int, int> m;
	map <int, int>::iterator it;
	scanf ("%d", &n);

	for (int i = 0; i < n; i++)
	{
		scanf ("%d", &v[i]);
		m[v[i]]++;
	}

	v[n++] = 1000101;

	int res = 0, bigger = 0;

	for (it = m.begin (); it != m.end (); ++it)
	{
		if (it->second > bigger)
		{
			bigger = it->second;
		}
	}
	int cont = 0;
	res = bigger;

	for (int i = 0; i < n; i++)
	{
		if (vet[v[i]])
		{
			vet[v[i]] = 0;
			memset (qtd, 0, sizeof qtd);
			q.clear ();
			cont = 0;

			for (int j = i + 1; j < n; j++)
			{
				vis.set ();
				while (j < n && v[j] != v[i])
				{
					if (vis[v[j]])
					{
						mp[v[j]] = cont++;
						qtd[mp[v[j]]]++;
						q.push_back (v[j]);
						vis[v[j]] = 0;
					}
					j++;
				}
			}
			int val, bigger = 0;
			for (int j = 0; j < (int)q.size (); j++)
			{
				if (qtd[mp[q[j]]] > bigger)
				{
					bigger = qtd[mp[q[j]]];
					val = q[j];
				}
				else if (qtd[mp[q[j]]] == bigger && vis[q[j]])
				{
					bigger = qtd[mp[q[j]]];
					val = q[j];
				}
			}

			int aux = bigger * 2;
			if (vis[val]) aux++;
			res = max (res, aux);
		}
	}

	if (n == 2) printf ("1\n");
	else printf ("%d\n", res);
}


#include <bits/stdc++.h>
#define INF 123456789

using namespace std;

int v[4040], n;

int main ()
{
	set <int> st;
	map <int, int> mp;
	map <int, int>::iterator it;
	scanf ("%d", &n);

	for (int i = 0; i < n; i++)
	{
		scanf ("%d", &v[i]);
	}

	v[n++] = -500;

	int res = 0;

	for (int i = 0; i < n; i++)
	{
		mp.clear ();
		for (int j = i + 1; j < n; j++)
		{
			st.clear ();
			while (j < n && v[j] != v[i])
			{
				if (st.find (v[j]) == st.end ())
				{
					mp[v[j]]++;
					st.insert (v[j]);
				}
				j++;
			}
		}
		int val, bigger = 0;
		for (it = mp.begin (); it != mp.end (); ++it)
		{
			if (it->second > bigger)
			{
				bigger = it->second;
				val = it->first;
			}
			else if (it->second == bigger && st.find (it->first) == st.end ())
			{
				bigger = it->second;
				val = it->first;
			}
		}

		int aux = bigger * 2;
		if (st.find (val) == st.end ()) aux++;
		res = max (res, aux);
	}

	if (n == 2) printf ("1\n");
	else printf ("%d\n", res);
}


#include <bits/stdc++.h>
#define INF 123456789

using namespace std;

int v[4040], n;

int main ()
{
	bitset <1000100> vis;
	set <int> st;
	map <int, int> mp;
	map <int, int>::iterator it;
	scanf ("%d", &n);

	for (int i = 0; i < n; i++)
	{
		scanf ("%d", &v[i]);
		mp[v[i]]++;
	}

	v[n++] = 1000101;

	int res = 0, bigger = 0;

	for (it = mp.begin (); it != mp.end (); ++it)
	{
		if (it->second > bigger)
		{
			bigger = it->second;
		}
	}

	res = bigger;

	for (int i = 0; i < n; i++)
	{
		mp.clear ();
		for (int j = i + 1; j < n; j++)
		{
			vis.set ();
			while (j < n && v[j] != v[i])
			{
				if (vis[v[j]])
				{
					mp[v[j]]++;
					vis[v[j]] = 0;
				}
				j++;
			}
		}
		int val, bigger = 0;
		for (it = mp.begin (); it != mp.end (); ++it)
		{
			if (it->second > bigger)
			{
				bigger = it->second;
				val = it->first;
			}
			else if (it->second == bigger && vis[it->first])
			{
				bigger = it->second;
				val = it->first;
			}
		}

		int aux = bigger * 2;
		if (vis[val]) aux++;
		res = max (res, aux);
	}

	if (n == 2) printf ("1\n");
	else printf ("%d\n", res);
}


#include <bits/stdc++.h>

using namespace std;

int main ()
{
	int n, v[4100];

	scanf ("%d", &n);

	for (int i = 0; i < n; i++)
	{
		scanf ("%d", &v[i]);
	}

	if (n == 1)
	{
		printf ("1\n");
		return 0;
	}

	int res = 0;

	for (int i = 0; i < n; i++)
	{
		for (int j = i + 1; j < n; j++)
		{
			int cont = 2;
			for (int k = j + 1; k < n; k++)
			{
				if (cont % 2 == 0)
				{
					if (v[k] == v[i]) cont++;
				}
				else if (v[k] == v[j]) cont++;
			}
			res = max (res, cont);
			if (res > n - j + 2) break;
		}
		if (res > n - i + 2) break;
	}

	printf ("%d\n", res);
}


#include <bits/stdc++.h>
#define INF 123456789

using namespace std;

int v[4040], n;

int main ()
{
	bitset <1000100> vis, vet;
	vet.set ();
	set <int> st;
	map <int, int> mp;
	map <int, int>::iterator it;
	scanf ("%d", &n);

	for (int i = 0; i < n; i++)
	{
		scanf ("%d", &v[i]);
		mp[v[i]]++;
	}

	v[n++] = 1000101;

	int res = 0, bigger = 0;

	for (it = mp.begin (); it != mp.end (); ++it)
	{
		if (it->second > bigger)
		{
			bigger = it->second;
		}
	}

	res = bigger;

	for (int i = 0; i < n; i++)
	{
		if (vet[v[i]])
		{
			vet[v[i]] = 0;
			mp.clear ();
			for (int j = i + 1; j < n; j++)
			{
				vis.set ();
				while (j < n && v[j] != v[i])
				{
					if (vis[v[j]])
					{
						mp[v[j]]++;
						vis[v[j]] = 0;
					}
					j++;
				}
			}
			int val, bigger = 0;
			for (it = mp.begin (); it != mp.end (); ++it)
			{
				if (it->second > bigger)
				{
					bigger = it->second;
					val = it->first;
				}
				else if (it->second == bigger && vis[it->first])
				{
					bigger = it->second;
					val = it->first;
				}
			}

			int aux = bigger * 2;
			if (vis[val]) aux++;
			res = max (res, aux);
		}
	}

	if (n == 2) printf ("1\n");
	else printf ("%d\n", res);
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

ll dp[4007][4007];

int main(){
//	freopen("input.inp","r",stdin);
//	freopen("output.txt","w",stdout);

	ll n;
	cin>>n;
	vector<ll> a(n);
	for (auto &it :a) cin>>it;

	vector<ll> hs=a;
	sort(hs.begin(),hs.end());

	hs.resize(distance(hs.begin(),unique(hs.begin(),hs.end())));

//	for (auto it: hs) cout<<it<<" ";
//	cout<<"\n";

	for (auto &it :a) it=lower_bound(hs.begin(),hs.end(),it)-hs.begin();

/*	for (int i=0;i<n;++i) {
		for (int j=0;j<n;++j) dp[i][j][1]=dp[i][j][2]=1;
	}

	for (int i=0;i<n;++i){
		for (int j=0;j<i;++j){
			if (a[j]<a[i])	dp[i][a[i]-a[j]][2]=max(dp[i][a[i]-a[j]][2],dp[j][a[i]-a[j]][1]+1);

			else dp[i][a[j]-a[j]][1]=max(dp[i][a[j]-a[i]][1],dp[j][a[j]-a[i]][2]+1);
		}
	}

	ll max1=0;

	for (int i=0;i<n;++i) {
		for (int j=0;j<n;++j) max1=max(max1,max(dp[i][j][1],dp[i][j][2]));
	}

	cout<<max1<<"\n";*/
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int nax=2e5+7;

ll dp[4444][4444][3];

int main(){
//	freopen("input.inp","r",stdin);
//	freopen("output.txt","w",stdout);

	ll n;
	cin>>n;
	vector<ll> a(n);
	for (auto &it :a) cin>>it;

	vector<ll> hs=a;
	sort(hs.begin(),hs.end());

	hs.resize(distance(hs.begin(),unique(hs.begin(),hs.end())));

//	for (auto it: hs) cout<<it<<" ";
//	cout<<"\n";

	for (auto &it :a) it=lower_bound(hs.begin(),hs.end(),it)-hs.begin();

	for (int i=0;i<n;++i) {
		for (int j=0;j<n;++j) dp[i][j][1]=dp[i][j][2]=1;
	}

	for (int i=0;i<n;++i){
		for (int j=0;j<i;++j){
			if (a[j]<a[i])	dp[i][a[i]-a[j]][2]=max(dp[i][a[i]-a[j]][2],dp[j][a[i]-a[j]][1]+1);

			else dp[i][a[j]-a[j]][1]=max(dp[i][a[j]-a[i]][1],dp[j][a[j]-a[i]][2]+1);
		}
	}

	ll max1=0;

	for (int i=0;i<n;++i) {
		for (int j=0;j<n;++j) max1=max(max1,max(dp[i][j][1],dp[i][j][2]));
	}

	cout<<max1<<"\n";
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

ll dp[4007][4007][3];

int main(){
//	freopen("input.inp","r",stdin);
//	freopen("output.txt","w",stdout);

	ll n;
	cin>>n;
	vector<ll> a(n);
	for (auto &it :a) cin>>it;

	vector<ll> hs=a;
	sort(hs.begin(),hs.end());

	hs.resize(distance(hs.begin(),unique(hs.begin(),hs.end())));

//	for (auto it: hs) cout<<it<<" ";
//	cout<<"\n";

	for (auto &it :a) it=lower_bound(hs.begin(),hs.end(),it)-hs.begin();

	for (int i=0;i<n;++i) {
		for (int j=0;j<n;++j) dp[i][j][1]=dp[i][j][2]=1;
	}

	for (int i=0;i<n;++i){
		for (int j=0;j<i;++j){
			if (a[j]<a[i])	dp[i][a[i]-a[j]][2]=max(dp[i][a[i]-a[j]][2],dp[j][a[i]-a[j]][1]+1);

			else dp[i][a[j]-a[j]][1]=max(dp[i][a[j]-a[i]][1],dp[j][a[j]-a[i]][2]+1);
		}
	}

	ll max1=0;

	for (int i=0;i<n;++i) {
		for (int j=0;j<n;++j) max1=max(max1,max(dp[i][j][1],dp[i][j][2]));
	}

	cout<<max1<<"\n";
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

ll dp[4007][4007][2];

int main(){
//	freopen("input.inp","r",stdin);
//	freopen("output.txt","w",stdout);

	ll n;
	cin>>n;
	vector<ll> a(n);
	for (auto &it :a) cin>>it;

	vector<ll> hs=a;
	sort(hs.begin(),hs.end());

	hs.resize(distance(hs.begin(),unique(hs.begin(),hs.end())));

//	for (auto it: hs) cout<<it<<" ";
//	cout<<"\n";

	for (auto &it :a) it=lower_bound(hs.begin(),hs.end(),it)-hs.begin();

	for (int i=0;i<n;++i) {
		for (int j=0;j<n;++j) dp[i][j][0]=dp[i][j][1]=1;
	}

	for (int i=0;i<n;++i){
		for (int j=0;j<i;++j){
			if (a[j]<a[i])	dp[i][a[i]-a[j]][1]=max(dp[i][a[i]-a[j]][1],dp[j][a[i]-a[j]][0]+1);

			else dp[i][a[j]-a[i]][0]=max(dp[i][a[j]-a[i]][0],dp[j][a[j]-a[i]][1]+1);
		}
	}

//	for (int i=0;i<n;++i) {
//		for (int j=0;j<n;++j) cout<<"{"<<dp[i][j][0]<<","<<dp[i][j][1]<<"} ";
//		cout<<"\n";
//	}

	ll max1=0;

	for (int i=0;i<n;++i) {
		for (int j=0;j<n;++j) max1=max(max1,max(dp[i][j][0],dp[i][j][1]));
	}

	cout<<max1<<"\n";
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

ll dp[4007][4007][2];

int main(){
//	freopen("input.inp","r",stdin);
//	freopen("output.txt","w",stdout);

	ll n;
	cin>>n;
	vector<ll> a(n);
	for (auto &it :a) cin>>it;

	vector<ll> hs=a;
	sort(hs.begin(),hs.end());

	hs.resize(distance(hs.begin(),unique(hs.begin(),hs.end())));

//	for (auto it: hs) cout<<it<<" ";
//	cout<<"\n";

	for (auto &it :a) it=lower_bound(hs.begin(),hs.end(),it)-hs.begin();

	for (int i=0;i<n;++i) {
		for (int j=0;j<n;++j) dp[i][j][0]=dp[i][j][1]=1;
	}

	vector<ll> cnt(n);
	for (auto it: a){
		cnt[it]++;
	}

	for (int i=0;i<n;++i){
		for (int j=0;j<i;++j){
			if (a[j]<a[i])	dp[i][a[i]-a[j]][1]=max(dp[i][a[i]-a[j]][1],dp[j][a[i]-a[j]][0]+1);

			else if (a[j]>a[i]) dp[i][a[j]-a[i]][0]=max(dp[i][a[j]-a[i]][0],dp[j][a[j]-a[i]][1]+1);
		}
	}

//	for (int i=0;i<n;++i) {
//		for (int j=0;j<n;++j) cout<<"{"<<dp[i][j][0]<<","<<dp[i][j][1]<<"} ";
//		cout<<"\n";
//	}

	ll max1=0;
	max1=*max_element(cnt.begin(),cnt.end());

	for (int i=0;i<n;++i) {
		for (int j=0;j<n;++j) max1=max(max1,max(dp[i][j][0],dp[i][j][1]));
	}

	cout<<max1<<"\n";
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

ll dp[4007][4007][2];

int main(){
//	freopen("input.inp","r",stdin);
//	freopen("output.txt","w",stdout);

	ll n;
	cin>>n;
	vector<ll> a(n);
	for (auto &it :a) cin>>it;

	vector<ll> hs=a;
	sort(hs.begin(),hs.end());

	hs.resize(distance(hs.begin(),unique(hs.begin(),hs.end())));

//	for (auto it: hs) cout<<it<<" ";
//	cout<<"\n";

	for (auto &it :a) it=lower_bound(hs.begin(),hs.end(),it)-hs.begin();

	for (int i=0;i<n;++i) {
		for (int j=0;j<n;++j) dp[i][j][0]=dp[i][j][1]=1;
	}

	for (int i=0;i<n;++i){
		for (int j=0;j<i;++j){
			if (a[j]<a[i])	dp[i][a[i]-a[j]][1]=max(dp[i][a[i]-a[j]][1],dp[j][a[i]-a[j]][0]+1);

			else dp[i][a[j]-a[j]][0]=max(dp[i][a[j]-a[i]][0],dp[j][a[j]-a[i]][1]+1);
		}
	}

	ll max1=0;

	for (int i=0;i<n;++i) {
		for (int j=0;j<n;++j) max1=max(max1,max(dp[i][j][0],dp[i][j][1]));
	}

	cout<<max1<<"\n";
}


#include<bits/stdc++.h>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x)  (long long)(x).size()
#define sqr(x) (x)*(x)
#define sof sizeof
#define ins insert
#define lowerB lower_bound
#define upperB upper_bound
#define mkp make_pair
#define mod 1000000007
#define mod1 998244353
#define N 100003
#define inf 1e18
#define minf -1e18
#define min3(a,b,c) min(a,min(b,c));
#define max3(a,b,c) max(a,max(b,c));
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION std::cout.unsetf ( std::ios::floatfield );std::cout.precision(15)
#define PI M_PI
#define fi first
#define se second
#define here cout<<"RSSB"<<endl
#define showzero cout<<std::showpoint
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define br "\n"
using namespace  std ;
typedef double db;
typedef long long ll ;
typedef vector<int>vi ;
//typedef vector<l>vl ;
typedef vector<ll>vll ;
typedef vector<vector<ll> >vvl ;
typedef pair<int,int> pii ;
typedef pair<ll,ll> pll ;
typedef vector<pll>vpll;
typedef vector<string>vstr;
typedef vector<bool>vbool ;
/*vbool isprime(N,true);
vll spf(N,0);
vll prime;
void sieve()
{
    isprime[0] = isprime[1] = false;
    for(ll i = 2 ; i<N ; i++)
    {
        if(isprime[i])
        {
            for(ll j=2*i ; j<N ; j+=i)
            {
                isprime[j] = false;
                if(spf[j]==0)
                {
                    spf[j] = i;
                }
            }
            prime.pb(i);
        }
    }
}*/
ll nck(ll n , ll k ,ll m=1e18+7)
{
    ll i , j,b=1,c=1 ;
    if(n-k<k)
    {
        k = n-k ;
    }
    while(k)
    {
        b*=n ;
        c*=k ;
        ll g = __gcd(b,c) ;
        b/=g ;
        c/=g ;
        n--;
        k-- ;
    }
    return(b) ;
}
long long power(long long k , long long n , long long m=mod)
{
    long long res = 1;
    while(n)
    {
        if(n%2!=0)
        {
            res = (res*k)%m ;
        }
        k = (k*k)%m ;
        n = n/2 ;
    }
    return(res) ;
}
void READ(vll &v, ll n)
{
    ll a ;
    for(ll i=0 ; i<n ; i=i+1)
    {
        cin>>a ;
        v.pb(a) ;
    }
}
void PRINT(vll &v , ll a = 0)
{
    //ll a ;
    for(ll i=a ; i<v.size() ; i=i+1)
    {
        v[i] = max((ll)0,v[i]);
        cout<<v[i]<<" " ;
    }
    cout<<"\n" ;
}
ll n,m,k;
bool ok = true;
map<pll,ll>mp;
vll v;
ll solve(ll i , ll j , bool to)
{
    pll p(i,j);
    if(i>=n)
    {
        return(0);
    }
    if(mp.count(p))
    {
        return(mp[p]);
    }
    ll ans = 0;
    if(to)
    {
        for(ll k = i+1 ; k<n ; k++)
        {
            ll d = v[k] - v[i];
            ans = max(solve(k,-d,false)+1,ans);
            ans = max(ans,solve(k,0,true));
        }
    }
    else
    {
        for(ll k= i+1 ; k<n ; k++)
        {
            if(v[k]-v[i]==j)
            {
                ans = max(ans,solve(k,-j,false)+1);
            }
        }
    }
    //ans++;
    //ans = max(ans,solve(i+1,0,true));
    mp[p] = ans;
    return(ans);
}
int main()
{
    FastIO ;
    PRECISION ;
    ll a,b,c,d,e,i,j,t,x,y,z,x1,y1,z1,f;
    //double p,q,r,u,w ;
    char ch,chr ;
    string str,st,str2,str3;
    //ll a1=0,a2=0,b1=0,b2=0,c1=0 ;
    //cin>>str>>str1;
    cin>>n;
    READ(v,n);
    if(n<=2)
    {
        cout<<n;
        exit(0);
    }
    x = solve(0,0,true);
    cout<<x+1;
}


#include<bits/stdc++.h>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x)  (long long)(x).size()
#define sqr(x) (x)*(x)
#define sof sizeof
#define ins insert
#define lowerB lower_bound
#define upperB upper_bound
#define mkp make_pair
#define mod 1000000007
#define mod1 998244353
#define N 1000003
#define inf 1e18
#define minf -1e18
#define min3(a,b,c) min(a,min(b,c));
#define max3(a,b,c) max(a,max(b,c));
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION std::cout.unsetf ( std::ios::floatfield );std::cout.precision(15)
#define PI M_PI
#define fi first
#define se second
#define here cout<<"RSSB"<<endl
#define showzero cout<<std::showpoint
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define br "\n"
using namespace  std ;
typedef double db;
typedef long long ll ;
typedef vector<int>vi ;
//typedef vector<l>vl ;
typedef vector<ll>vll ;
typedef vector<vector<ll> >vvl ;
typedef pair<int,int> pii ;
typedef pair<ll,ll> pll ;
typedef vector<pll>vpll;
typedef vector<string>vstr;
typedef vector<bool>vbool ;
/*vbool isprime(N,true);
vll spf(N,0);
vll prime;
void sieve()
{
    isprime[0] = isprime[1] = false;
    for(ll i = 2 ; i<N ; i++)
    {
        if(isprime[i])
        {
            for(ll j=2*i ; j<N ; j+=i)
            {
                isprime[j] = false;
                if(spf[j]==0)
                {
                    spf[j] = i;
                }
            }
            prime.pb(i);
        }
    }
}*/
ll nck(ll n , ll k ,ll m=1e18+7)
{
    ll i , j,b=1,c=1 ;
    if(n-k<k)
    {
        k = n-k ;
    }
    while(k)
    {
        b*=n ;
        c*=k ;
        ll g = __gcd(b,c) ;
        b/=g ;
        c/=g ;
        n--;
        k-- ;
    }
    return(b) ;
}
long long power(long long k , long long n , long long m=mod)
{
    long long res = 1;
    while(n)
    {
        if(n%2!=0)
        {
            res = (res*k)%m ;
        }
        k = (k*k)%m ;
        n = n/2 ;
    }
    return(res) ;
}
void READ(vll &v, ll n)
{
    ll a ;
    for(ll i=0 ; i<n ; i=i+1)
    {
        cin>>a ;
        v.pb(a) ;
    }
}
void PRINT(vll &v , ll a = 0)
{
    //ll a ;
    for(ll i=a ; i<v.size() ; i=i+1)
    {
        v[i] = max((ll)0,v[i]);
        cout<<v[i]<<" " ;
    }
    cout<<"\n" ;
}
ll n,m,k;
bool ok = true;
int main()
{
    FastIO ;
    PRECISION ;
    ll a,b,c,d,e,i,j,t,x,y,z,x1,y1,z1,f;
    //double p,q,r,u,w ;
    char ch,chr ;
    string str,st,str2,str3;
    //ll a1=0,a2=0,b1=0,b2=0,c1=0 ;
    //cin>>str>>str1;
    cin>>n;
    vll v;
    READ(v,n);
    if(n<=2)
    {
        cout<<n;
        exit(0);
    }
    vector<map<ll,ll> >mp(4003);
    map<ll,ll>freq;
    k = 0;
    x = 2;
    for(i=0 ; i<n ; i++)
    {
        map<ll,ll>f1;
        ok = false;
        for(j=i+1 ; j<n ; j++)
        {
            if(v[i]==v[j])
            {
                ok = true;
                break;
            }
            f1[v[j]-v[i]]++;
        }
        y = 0;
        if(freq.count(v[i]))
        {
            y = 1;
            d = freq[v[i]];
        }
        else
        {
            d = k;
            freq[v[i]] = k++;
            mp[d] = f1;
        }
        for(auto j : mp[d])
        {
            if(f1.count(j.fi))
            {
                mp[d][j.fi]++;
                if(y==0)
                {
                    mp[d][j.fi] = 1;
                }
                x = max(x,mp[d][j.fi]*2 + (ok==true));
            }
            else
            {
                mp[d][j.fi] = 0;
            }
        }
    }
    cout<<x;
}


#include<bits/stdc++.h>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x)  (long long)(x).size()
#define sqr(x) (x)*(x)
#define sof sizeof
#define ins insert
#define lowerB lower_bound
#define upperB upper_bound
#define mkp make_pair
#define mod 1000000007
#define mod1 998244353
#define N 100003
#define inf 1e18
#define minf -1e18
#define min3(a,b,c) min(a,min(b,c));
#define max3(a,b,c) max(a,max(b,c));
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION std::cout.unsetf ( std::ios::floatfield );std::cout.precision(15)
#define PI M_PI
#define fi first
#define se second
#define here cout<<"RSSB"<<endl
#define showzero cout<<std::showpoint
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define br "\n"
using namespace  std ;
typedef double db;
typedef long long ll ;
typedef vector<int>vi ;
//typedef vector<l>vl ;
typedef vector<ll>vll ;
typedef vector<vector<ll> >vvl ;
typedef pair<int,int> pii ;
typedef pair<ll,ll> pll ;
typedef vector<pll>vpll;
typedef vector<string>vstr;
typedef vector<bool>vbool ;
/*vbool isprime(N,true);
vll spf(N,0);
vll prime;
void sieve()
{
    isprime[0] = isprime[1] = false;
    for(ll i = 2 ; i<N ; i++)
    {
        if(isprime[i])
        {
            for(ll j=2*i ; j<N ; j+=i)
            {
                isprime[j] = false;
                if(spf[j]==0)
                {
                    spf[j] = i;
                }
            }
            prime.pb(i);
        }
    }
}*/
ll nck(ll n , ll k ,ll m=1e18+7)
{
    ll i , j,b=1,c=1 ;
    if(n-k<k)
    {
        k = n-k ;
    }
    while(k)
    {
        b*=n ;
        c*=k ;
        ll g = __gcd(b,c) ;
        b/=g ;
        c/=g ;
        n--;
        k-- ;
    }
    return(b) ;
}
long long power(long long k , long long n , long long m=mod)
{
    long long res = 1;
    while(n)
    {
        if(n%2!=0)
        {
            res = (res*k)%m ;
        }
        k = (k*k)%m ;
        n = n/2 ;
    }
    return(res) ;
}
void READ(vll &v, ll n)
{
    ll a ;
    for(ll i=0 ; i<n ; i=i+1)
    {
        cin>>a ;
        v.pb(a) ;
    }
}
void PRINT(vll &v , ll a = 0)
{
    //ll a ;
    for(ll i=a ; i<v.size() ; i=i+1)
    {
        v[i] = max((ll)0,v[i]);
        cout<<v[i]<<" " ;
    }
    cout<<"\n" ;
}
ll n,m,k;
bool ok = true;
vll v;
int main()
{
    FastIO ;
    PRECISION ;
    ll a,b,c,d,e,i,j,t,x,y,z,x1,y1,z1,f;
    //double p,q,r,u,w ;
    char ch,chr ;
    string str,st,str2,str3;
    //ll a1=0,a2=0,b1=0,b2=0,c1=0 ;
    //cin>>str>>str1;
    cin>>n;
    v.pb(0);
    READ(v,n);
    if(n<=2)
    {
        cout<<n;
        exit(0);
    }
    ll dp[n+2][n+2];
    memset(dp,0,sizeof(dp));
    x = 2;
    for(i=1 ; i<=n ; i++)
    {
        ll p = 0;
        for(j=0 ; j<i ; j++)
        {
            dp[j][i] = 1 + dp[p][j];
            x = max(x,dp[j][i]);
            if(v[i]==v[j])
            {
                p = j;
            }

        }
    }
    cout<<x;
}


#include<bits/stdc++.h>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x)  (long long)(x).size()
#define sqr(x) (x)*(x)
#define sof sizeof
#define ins insert
#define lowerB lower_bound
#define upperB upper_bound
#define mkp make_pair
#define mod 1000000007
#define mod1 998244353
#define N 100003
#define inf 1e18
#define minf -1e18
#define min3(a,b,c) min(a,min(b,c));
#define max3(a,b,c) max(a,max(b,c));
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION std::cout.unsetf ( std::ios::floatfield );std::cout.precision(15)
#define PI M_PI
#define fi first
#define se second
#define here cout<<"RSSB"<<endl
#define showzero cout<<std::showpoint
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define br "\n"
using namespace  std ;
typedef double db;
typedef long long ll ;
typedef vector<int>vi ;
//typedef vector<l>vl ;
typedef vector<ll>vll ;
typedef vector<vector<ll> >vvl ;
typedef pair<int,int> pii ;
typedef pair<ll,ll> pll ;
typedef vector<pll>vpll;
typedef vector<string>vstr;
typedef vector<bool>vbool ;
/*vbool isprime(N,true);
vll spf(N,0);
vll prime;
void sieve()
{
    isprime[0] = isprime[1] = false;
    for(ll i = 2 ; i<N ; i++)
    {
        if(isprime[i])
        {
            for(ll j=2*i ; j<N ; j+=i)
            {
                isprime[j] = false;
                if(spf[j]==0)
                {
                    spf[j] = i;
                }
            }
            prime.pb(i);
        }
    }
}*/
ll nck(ll n , ll k ,ll m=1e18+7)
{
    ll i , j,b=1,c=1 ;
    if(n-k<k)
    {
        k = n-k ;
    }
    while(k)
    {
        b*=n ;
        c*=k ;
        ll g = __gcd(b,c) ;
        b/=g ;
        c/=g ;
        n--;
        k-- ;
    }
    return(b) ;
}
long long power(long long k , long long n , long long m=mod)
{
    long long res = 1;
    while(n)
    {
        if(n%2!=0)
        {
            res = (res*k)%m ;
        }
        k = (k*k)%m ;
        n = n/2 ;
    }
    return(res) ;
}
void READ(vll &v, ll n)
{
    ll a ;
    for(ll i=0 ; i<n ; i=i+1)
    {
        cin>>a ;
        v.pb(a) ;
    }
}
void PRINT(vll &v , ll a = 0)
{
    //ll a ;
    for(ll i=a ; i<v.size() ; i=i+1)
    {
        v[i] = max((ll)0,v[i]);
        cout<<v[i]<<" " ;
    }
    cout<<"\n" ;
}
ll n,m,k;
bool ok = true;
map<pair<bool,pll>,ll>mp;
vll v;
ll solve(ll i , ll j , bool to)
{
    pair<bool,pll>p(to,mkp(i,j));
    if(i>=n)
    {
        return(0);
    }
    if(mp.count(p))
    {
        return(mp[p]);
    }
    ll ans = 0;
    if(to)
    {
        for(ll k = i+1 ; k<n ; k++)
        {
            ll d = v[k] - v[i];
            ans = max(solve(k,-d,false)+1,ans);
            ans = max(ans,solve(k,0,true));
        }
    }
    else
    {
        for(ll k= i+1 ; k<n ; k++)
        {
            if(v[k]-v[i]==j)
            {
                ans = max(ans,solve(k,-j,false)+1);
            }
        }
    }
    //ans++;
    //ans = max(ans,solve(i+1,0,true));
    mp[p] = ans;
    return(ans);
}
int main()
{
    FastIO ;
    PRECISION ;
    ll a,b,c,d,e,i,j,t,x,y,z,x1,y1,z1,f;
    //double p,q,r,u,w ;
    char ch,chr ;
    string str,st,str2,str3;
    //ll a1=0,a2=0,b1=0,b2=0,c1=0 ;
    //cin>>str>>str1;
    cin>>n;
    READ(v,n);
    if(n<=2)
    {
        cout<<n;
        exit(0);
    }
    x = solve(0,0,true);
    cout<<x+1;
}


#include<bits/stdc++.h>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x)  (long long)(x).size()
#define sqr(x) (x)*(x)
#define sof sizeof
#define ins insert
#define lowerB lower_bound
#define upperB upper_bound
#define mkp make_pair
#define mod 1000000007
#define mod1 998244353
#define N 100003
#define inf 1e18
#define minf -1e18
#define min3(a,b,c) min(a,min(b,c));
#define max3(a,b,c) max(a,max(b,c));
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION std::cout.unsetf ( std::ios::floatfield );std::cout.precision(15)
#define PI M_PI
#define fi first
#define se second
#define here cout<<"RSSB"<<endl
#define showzero cout<<std::showpoint
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define br "\n"
using namespace  std ;
typedef double db;
typedef long long ll ;
typedef vector<int>vi ;
//typedef vector<l>vl ;
typedef vector<ll>vll ;
typedef vector<vector<ll> >vvl ;
typedef pair<int,int> pii ;
typedef pair<ll,ll> pll ;
typedef vector<pll>vpll;
typedef vector<string>vstr;
typedef vector<bool>vbool ;
/*vbool isprime(N,true);
vll spf(N,0);
vll prime;
void sieve()
{
    isprime[0] = isprime[1] = false;
    for(ll i = 2 ; i<N ; i++)
    {
        if(isprime[i])
        {
            for(ll j=2*i ; j<N ; j+=i)
            {
                isprime[j] = false;
                if(spf[j]==0)
                {
                    spf[j] = i;
                }
            }
            prime.pb(i);
        }
    }
}*/
ll nck(ll n , ll k ,ll m=1e18+7)
{
    ll i , j,b=1,c=1 ;
    if(n-k<k)
    {
        k = n-k ;
    }
    while(k)
    {
        b*=n ;
        c*=k ;
        ll g = __gcd(b,c) ;
        b/=g ;
        c/=g ;
        n--;
        k-- ;
    }
    return(b) ;
}
long long power(long long k , long long n , long long m=mod)
{
    long long res = 1;
    while(n)
    {
        if(n%2!=0)
        {
            res = (res*k)%m ;
        }
        k = (k*k)%m ;
        n = n/2 ;
    }
    return(res) ;
}
void READ(vll &v, ll n)
{
    ll a ;
    for(ll i=0 ; i<n ; i=i+1)
    {
        cin>>a ;
        v.pb(a) ;
    }
}
void PRINT(vll &v , ll a = 0)
{
    //ll a ;
    for(ll i=a ; i<v.size() ; i=i+1)
    {
        v[i] = max((ll)0,v[i]);
        cout<<v[i]<<" " ;
    }
    cout<<"\n" ;
}
ll n,m,k;
bool ok = true;
int main()
{
    FastIO ;
    PRECISION ;
    ll a,b,c,d,e,i,j,t,x,y,z,x1,y1,z1,f;
    //double p,q,r,u,w ;
    char ch,chr ;
    string str,st,str2,str3;
    //ll a1=0,a2=0,b1=0,b2=0,c1=0 ;
    //cin>>str>>str1;
    cin>>n;
    vll v;
    READ(v,n);
    if(n<=2)
    {
        cout<<n;
        exit(0);
    }
    x = 2;
    map<pll,ll>mp;
    for(i=0 ; i<n ; i++)
    {
        for(j=i-1 ; j>=0 ; j--)
        {
            a = v[j] - v[i];
            b = v[i] - v[j];
            c = mp[mkp(j,a)];
            d = mp[mkp(j,b)];
            if(mp[mkp(i,b)]<c+1)
            {
                mp[mkp(i,b)] = c + 1;
                //x = max(x,c+2);
            }
            if(mp[mkp(i,a)]<d+1)
            {
                mp[mkp(i,a)] = d + 1;
                //x = max(x,d+2);
            }
        }
    }
    for(auto j : mp)
    {
        x = max(x,j.se+1);
    }
    cout<<x;
}


#include<bits/stdc++.h>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x)  (long long)(x).size()
#define sqr(x) (x)*(x)
#define sof sizeof
#define ins insert
#define lowerB lower_bound
#define upperB upper_bound
#define mkp make_pair
#define mod 1000000007
#define mod1 998244353
#define N 100003
#define inf 1e18
#define minf -1e18
#define min3(a,b,c) min(a,min(b,c));
#define max3(a,b,c) max(a,max(b,c));
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION std::cout.unsetf ( std::ios::floatfield );std::cout.precision(15)
#define PI M_PI
#define fi first
#define se second
#define here cout<<"RSSB"<<endl
#define showzero cout<<std::showpoint
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define br "\n"
using namespace  std ;
typedef double db;
typedef long long ll ;
typedef vector<int>vi ;
//typedef vector<l>vl ;
typedef vector<ll>vll ;
typedef vector<vector<ll> >vvl ;
typedef pair<int,int> pii ;
typedef pair<ll,ll> pll ;
typedef vector<pll>vpll;
typedef vector<string>vstr;
typedef vector<bool>vbool ;
/*vbool isprime(N,true);
vll spf(N,0);
vll prime;
void sieve()
{
    isprime[0] = isprime[1] = false;
    for(ll i = 2 ; i<N ; i++)
    {
        if(isprime[i])
        {
            for(ll j=2*i ; j<N ; j+=i)
            {
                isprime[j] = false;
                if(spf[j]==0)
                {
                    spf[j] = i;
                }
            }
            prime.pb(i);
        }
    }
}*/
ll nck(ll n , ll k ,ll m=1e18+7)
{
    ll i , j,b=1,c=1 ;
    if(n-k<k)
    {
        k = n-k ;
    }
    while(k)
    {
        b*=n ;
        c*=k ;
        ll g = __gcd(b,c) ;
        b/=g ;
        c/=g ;
        n--;
        k-- ;
    }
    return(b) ;
}
long long power(long long k , long long n , long long m=mod)
{
    long long res = 1;
    while(n)
    {
        if(n%2!=0)
        {
            res = (res*k)%m ;
        }
        k = (k*k)%m ;
        n = n/2 ;
    }
    return(res) ;
}
void READ(vll &v, ll n)
{
    ll a ;
    for(ll i=0 ; i<n ; i=i+1)
    {
        cin>>a ;
        v.pb(a) ;
    }
}
void PRINT(vll &v , ll a = 0)
{
    //ll a ;
    for(ll i=a ; i<v.size() ; i=i+1)
    {
        v[i] = max((ll)0,v[i]);
        cout<<v[i]<<" " ;
    }
    cout<<"\n" ;
}
ll n,m,k;
bool ok = true;
int main()
{
    FastIO ;
    PRECISION ;
    ll a,b,c,d,e,i,j,t,x,y,z,x1,y1,z1,f;
    //double p,q,r,u,w ;
    char ch,chr ;
    string str,st,str2,str3;
    //ll a1=0,a2=0,b1=0,b2=0,c1=0 ;
    //cin>>str>>str1;
    cin>>n;
    vll v;
    vll dp(n,1);
    vll diff(n,0);
    READ(v,n);
    if(n<=2)
    {
        cout<<n;
        exit(0);
    }
    for(i=0 ; i<n ; i++)
    {
        for(j=i-1; j>=0 ; j--)
        {
            if(dp[j]%2 && (dp[j]==1 || v[j]-diff[j]==v[i]))
            {
                if(dp[i]<=dp[j]+1)
                {
                    dp[i] = dp[j] + 1;
                    if(dp[j]==1)
                    {
                        diff[i] = v[j] - v[i];
                    }
                    else
                    {
                        diff[i] = diff[j];
                    }
                }
            }
            if(dp[j]%2==0 && v[j]+diff[j]==v[i])
            {
                if(dp[i]<=dp[j]+1)
                {
                    dp[i] = dp[j] + 1;
                    diff[i] = diff[j];
                }
            }
        }
    }
    x = 2;
    for(i=0 ; i<n ; i++)
    {
        x = max(x,dp[i]);
        dp[i] = 1;
        diff[i] = 0;
    }
    for(i=0 ; i<n ; i++)
    {
        for(j=i-1; j>=0 ; j--)
        {
            if(dp[j]%2 && (dp[j]==1 || v[j]+diff[j]==v[i]))
            {
                if(dp[i]<=dp[j]+1)
                {
                    dp[i] = dp[j] + 1;
                    if(dp[j]==1)
                    {
                        diff[i] = v[i] - v[j];
                    }
                    else
                    {
                        diff[i] = diff[j];
                    }
                }
            }
            if(dp[j]%2==0 && v[j]-diff[j]==v[i])
            {
                if(dp[i]<=dp[j]+1)
                {
                    dp[i] = dp[j] + 1;
                    diff[i] = diff[j];
                }
            }
        }
    }
    for(i=0 ; i<n ; i++)
    {
        x = max(x,dp[i]);
        dp[i] = 1;
        diff[i] = 0;
    }
    cout<<x;
}


#include<bits/stdc++.h>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x)  (long long)(x).size()
#define sqr(x) (x)*(x)
#define sof sizeof
#define ins insert
#define lowerB lower_bound
#define upperB upper_bound
#define mkp make_pair
#define mod 1000000007
#define mod1 998244353
#define N 100003
#define inf 1e18
#define minf -1e18
#define min3(a,b,c) min(a,min(b,c));
#define max3(a,b,c) max(a,max(b,c));
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION std::cout.unsetf ( std::ios::floatfield );std::cout.precision(15)
#define PI M_PI
#define fi first
#define se second
#define here cout<<"RSSB"<<endl
#define showzero cout<<std::showpoint
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define br "\n"
using namespace  std ;
typedef double db;
typedef long long ll ;
typedef vector<int>vi ;
//typedef vector<l>vl ;
typedef vector<ll>vll ;
typedef vector<vector<ll> >vvl ;
typedef pair<int,int> pii ;
typedef pair<ll,ll> pll ;
typedef vector<pll>vpll;
typedef vector<string>vstr;
typedef vector<bool>vbool ;
/*vbool isprime(N,true);
vll spf(N,0);
vll prime;
void sieve()
{
    isprime[0] = isprime[1] = false;
    for(ll i = 2 ; i<N ; i++)
    {
        if(isprime[i])
        {
            for(ll j=2*i ; j<N ; j+=i)
            {
                isprime[j] = false;
                if(spf[j]==0)
                {
                    spf[j] = i;
                }
            }
            prime.pb(i);
        }
    }
}*/
ll nck(ll n , ll k ,ll m=1e18+7)
{
    ll i , j,b=1,c=1 ;
    if(n-k<k)
    {
        k = n-k ;
    }
    while(k)
    {
        b*=n ;
        c*=k ;
        ll g = __gcd(b,c) ;
        b/=g ;
        c/=g ;
        n--;
        k-- ;
    }
    return(b) ;
}
long long power(long long k , long long n , long long m=mod)
{
    long long res = 1;
    while(n)
    {
        if(n%2!=0)
        {
            res = (res*k)%m ;
        }
        k = (k*k)%m ;
        n = n/2 ;
    }
    return(res) ;
}
void READ(vll &v, ll n)
{
    ll a ;
    for(ll i=0 ; i<n ; i=i+1)
    {
        cin>>a ;
        v.pb(a) ;
    }
}
void PRINT(vll &v , ll a = 0)
{
    //ll a ;
    for(ll i=a ; i<v.size() ; i=i+1)
    {
        v[i] = max((ll)0,v[i]);
        cout<<v[i]<<" " ;
    }
    cout<<"\n" ;
}
ll n,m,k;
bool ok = true;
vll v;
int main()
{
    FastIO ;
    PRECISION ;
    ll a,b,c,d,e,i,j,t,x,y,z,x1,y1,z1,f;
    //double p,q,r,u,w ;
    char ch,chr ;
    string str,st,str2,str3;
    //ll a1=0,a2=0,b1=0,b2=0,c1=0 ;
    //cin>>str>>str1;
    cin>>n;
    READ(v,n);
    if(n<=2)
    {
        cout<<n;
        exit(0);
    }
    vll dp[n+2][n+2];
    x = 2;
    for(i=0 ; i<n ; i++)
    {
        ll p = 0;
        for(j=0 ; j<i ; j++)
        {
            dp[j][i] = 1 + dp[p][j];
            x = max(x,dp[j][i]);
            if(v[i]==v[j])
            {
                p = j;
            }

        }
    }
    cout<<x;
}


/*
 #######################################################
 #                                                     #
 #        masaka :(               #
 #                                                     #
 #######################################################
  * */
  #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
#define vll vector< ll >
#define M 4000
#define pb push_back
#define REP(i,a,b) for(ll i = a; i <= b; i++)
#define pii pair<ll,ll>
#define vec(a) vector<a >
#define se second
#define fi first
#define ins insert
#define inchar
#define outchar(x) putchar_unlocked(x)
 // template<typename T>inline void in(T &x){x=0;register T c=inchar();while(((c<48)||(c>57))&&(c!='-'))c=inchar();bool neg=false;if(c=='-')neg=true;for(;c<48||c>57;c=inchar());for(;c>47&&c<58;c=inchar())x=(x<<3)+(x<<1)+(c&15);if(neg)x=-x;}
 // template<typename T>inline void out(T n){if(n<0){outchar('-');n*=-1;}char snum[65];ll i=0;do {snum[i++]=n%10+'0';n/=10;}while(n);i=i-1;while(i>=0)outchar(snum[i--]);outchar('\n');}
#define MD 1000000007
ll md=MD;

inline ll mulD(ll a,ll b){ll r=0;while(b>0){if(b&1){r=r+a%md;r=r%md;}b/=2;a=2*a%md;a=a%md;}return r%md;;}
inline ll exp(ll a,ll b){ll r=1LL;while(b>0){if(b&1){r=r*(a%md);r=(r+md)%md;}b/=2;a=(a%md)*(a%md);a=(a+md)%md;}return (r+md)%md;}
inline ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
inline ll poww(ll a,ll b){ll r=1LL;while(b>0){if(b&1)r=r*a;b/=2;a=a*a;}return (int)r;}
int f[M+1][M+1],a[M+1];
map<ll,ll> mp;
int main() {
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);//ifstream fin("input.txt");ofstream fout("output.txt");
    ll t,i,s,j,m,x,y,z,k,q,n,p,T;string s1,s2,s3;x=0;p=0;
    cin>>n;
    for(i=1;i<=n;++i)cin>>a[i];
    // for(i=1;i<=)
    for(i=1;i<=n;++i){
    	for(j=i-1;j>=1;--j){
    		if(a[j]==a[i])break;

    	}
    	k=j;ll ans=0;
    	for(j=i-1;j>=0;--j){
    		if(a[j]==a[i])break;
    			f[i][j]=f[j][k]+1;
    	}
    }p=0;
    for(i=1;i<=n;++i){for(j=1;j<=n;++j)p=max(p,(1ll)*f[i][j]);}cout<<p;

}


/*
 #######################################################
 #                                                     #
 #        masaka :(               #
 #                                                     #
 #######################################################
  * */
  #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
#define vll vector< ll >
#define M 4000
#define pb push_back
#define REP(i,a,b) for(ll i = a; i <= b; i++)
#define pii pair<ll,ll>
#define vec(a) vector<a >
#define se second
#define fi first
#define ins insert
#define inchar
#define outchar(x) putchar_unlocked(x)
 // template<typename T>inline void in(T &x){x=0;register T c=inchar();while(((c<48)||(c>57))&&(c!='-'))c=inchar();bool neg=false;if(c=='-')neg=true;for(;c<48||c>57;c=inchar());for(;c>47&&c<58;c=inchar())x=(x<<3)+(x<<1)+(c&15);if(neg)x=-x;}
 // template<typename T>inline void out(T n){if(n<0){outchar('-');n*=-1;}char snum[65];ll i=0;do {snum[i++]=n%10+'0';n/=10;}while(n);i=i-1;while(i>=0)outchar(snum[i--]);outchar('\n');}
#define MD 1000000007
ll md=MD;
inline ll mulD(ll a,ll b){ll r=0;while(b>0){if(b&1){r=r+a%md;r=r%md;}b/=2;a=2*a%md;a=a%md;}return r%md;;}
inline ll exp(ll a,ll b){ll r=1LL;while(b>0){if(b&1){r=r*(a%md);r=(r+md)%md;}b/=2;a=(a%md)*(a%md);a=(a+md)%md;}return (r+md)%md;}
inline ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
inline ll poww(ll a,ll b){ll r=1LL;while(b>0){if(b&1)r=r*a;b/=2;a=a*a;}return (ll)r;}
int a[M+1];
map<ll,ll> mp;
int dp[M+1][M+1];
int main() {
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);//ifstream fin("input.txt");ofstream fout("output.txt");
    int t,i,s,j,m,x,y,z,k,q,n,p,T;string s1,s2,s3;x=0;p=0;
    cin>>n;int cnt=0;
    for(i=1;i<=n;++i){cin>>a[i];if(mp.find(a[i])==mp.end())mp.ins({a[i],++cnt});}
    // for(i=1;i<=)
    	p=0;
    for(i=1;i<=n;++i){
    	for(j=1;j<i;++j){
    		dp[i][mp[a[j]]]=max(dp[i][mp[a[j]]],dp[j][mp[a[i]]]+1);
    		p=max(p,dp[i][mp[a[j]]]);
    	}
    }
    // for(i=1;i<=n;++i)for(j=1;j<=n;++j)p=max(dp[i][j],p);
    	cout<<p+1;

}


/*
 #######################################################
 #                                                     #
 #        masaka :(               #
 #                                                     #
 #######################################################
  * */
  #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
#define vll vector< ll >
#define M 4000
#define pb push_back
#define REP(i,a,b) for(ll i = a; i <= b; i++)
#define pii pair<ll,ll>
#define vec(a) vector<a >
#define se second
#define fi first
#define ins insert
#define inchar
#define outchar(x) putchar_unlocked(x)
 // template<typename T>inline void in(T &x){x=0;register T c=inchar();while(((c<48)||(c>57))&&(c!='-'))c=inchar();bool neg=false;if(c=='-')neg=true;for(;c<48||c>57;c=inchar());for(;c>47&&c<58;c=inchar())x=(x<<3)+(x<<1)+(c&15);if(neg)x=-x;}
 // template<typename T>inline void out(T n){if(n<0){outchar('-');n*=-1;}char snum[65];ll i=0;do {snum[i++]=n%10+'0';n/=10;}while(n);i=i-1;while(i>=0)outchar(snum[i--]);outchar('\n');}
#define MD 1000000007
ll md=MD;
inline ll mulD(ll a,ll b){ll r=0;while(b>0){if(b&1){r=r+a%md;r=r%md;}b/=2;a=2*a%md;a=a%md;}return r%md;;}
inline ll exp(ll a,ll b){ll r=1LL;while(b>0){if(b&1){r=r*(a%md);r=(r+md)%md;}b/=2;a=(a%md)*(a%md);a=(a+md)%md;}return (r+md)%md;}
inline ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
inline ll poww(ll a,ll b){ll r=1LL;while(b>0){if(b&1)r=r*a;b/=2;a=a*a;}return (ll)r;}
int a[M+1];
map<ll,ll> mp;
int dp[M+1][M+1];
int main() {
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);//ifstream fin("input.txt");ofstream fout("output.txt");
    ll t,i,s,j,m,x,y,z,k,q,n,p,T;string s1,s2,s3;x=0;p=0;
    cin>>n;int cnt=0;
    for(i=1;i<=n;++i){cin>>a[i];if(mp.find(a[i])==mp.end())mp.ins({a[i],++cnt});}
    // for(i=1;i<=)
    	p=0;
    for(i=1;i<=n;++i){
    	for(j=1;j<i;++j){
    		dp[i][mp[a[j]]]=max(dp[i][mp[a[j]]],dp[j][mp[a[i]]]+1);
    		p=max(p,dp[i]mp[a[j]]);
    	}
    }p=0;
    // for(i=1;i<=n;++i)for(j=1;j<=n;++j)p=max(dp[i][j],p);
    	cout<<p+1;

}


/*
 #######################################################
 #                                                     #
 #        masaka :(               #
 #                                                     #
 #######################################################
  * */
  #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
#define vll vector< ll >
#define M 4000
#define pb push_back
#define REP(i,a,b) for(ll i = a; i <= b; i++)
#define pii pair<ll,ll>
#define vec(a) vector<a >
#define se second
#define fi first
#define ins insert
#define inchar
#define outchar(x) putchar_unlocked(x)
 // template<typename T>inline void in(T &x){x=0;register T c=inchar();while(((c<48)||(c>57))&&(c!='-'))c=inchar();bool neg=false;if(c=='-')neg=true;for(;c<48||c>57;c=inchar());for(;c>47&&c<58;c=inchar())x=(x<<3)+(x<<1)+(c&15);if(neg)x=-x;}
 // template<typename T>inline void out(T n){if(n<0){outchar('-');n*=-1;}char snum[65];ll i=0;do {snum[i++]=n%10+'0';n/=10;}while(n);i=i-1;while(i>=0)outchar(snum[i--]);outchar('\n');}
#define MD 1000000007
ll md=MD;
inline ll mulD(ll a,ll b){ll r=0;while(b>0){if(b&1){r=r+a%md;r=r%md;}b/=2;a=2*a%md;a=a%md;}return r%md;;}
inline ll exp(ll a,ll b){ll r=1LL;while(b>0){if(b&1){r=r*(a%md);r=(r+md)%md;}b/=2;a=(a%md)*(a%md);a=(a+md)%md;}return (r+md)%md;}
inline ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
inline ll poww(ll a,ll b){ll r=1LL;while(b>0){if(b&1)r=r*a;b/=2;a=a*a;}return (ll)r;}
int a[M+1];
unordered_map<ll,ll> mp;
int dp[M+1][M+1];
int main() {
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);//ifstream fin("input.txt");ofstream fout("output.txt");
    int t,i,s,j,m,x,y,z,k,q,n,p,T;string s1,s2,s3;x=0;p=0;
    cin>>n;int cnt=0;
    for(i=1;i<=n;++i){cin>>a[i];if(mp.find(a[i])==mp.end())mp.ins({a[i],++cnt});}
    // for(i=1;i<=)
    	p=0;
    for(i=1;i<=n;++i){
    	for(j=1;j<i;++j){
    		dp[i][mp[a[j]]]=max(dp[i][mp[a[j]]],dp[j][mp[a[i]]]+1);
    		p=max(p,dp[i][mp[a[j]]]);
    	}
    }
    // for(i=1;i<=n;++i)for(j=1;j<=n;++j)p=max(dp[i][j],p);
    	cout<<p+1;

}


/*
 #######################################################
 #                                                     #
 #        masaka :(               #
 #                                                     #
 #######################################################
  * */
  #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
#define vll vector< ll >
#define M 4000
#define pb push_back
#define REP(i,a,b) for(ll i = a; i <= b; i++)
#define pii pair<ll,ll>
#define vec(a) vector<a >
#define se second
#define fi first
#define ins insert
#define inchar
#define outchar(x) putchar_unlocked(x)
 // template<typename T>inline void in(T &x){x=0;register T c=inchar();while(((c<48)||(c>57))&&(c!='-'))c=inchar();bool neg=false;if(c=='-')neg=true;for(;c<48||c>57;c=inchar());for(;c>47&&c<58;c=inchar())x=(x<<3)+(x<<1)+(c&15);if(neg)x=-x;}
 // template<typename T>inline void out(T n){if(n<0){outchar('-');n*=-1;}char snum[65];ll i=0;do {snum[i++]=n%10+'0';n/=10;}while(n);i=i-1;while(i>=0)outchar(snum[i--]);outchar('\n');}
#define MD 1000000007
ll md=MD;
inline ll mulD(ll a,ll b){ll r=0;while(b>0){if(b&1){r=r+a%md;r=r%md;}b/=2;a=2*a%md;a=a%md;}return r%md;;}
inline ll exp(ll a,ll b){ll r=1LL;while(b>0){if(b&1){r=r*(a%md);r=(r+md)%md;}b/=2;a=(a%md)*(a%md);a=(a+md)%md;}return (r+md)%md;}
inline ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
inline ll poww(ll a,ll b){ll r=1LL;while(b>0){if(b&1)r=r*a;b/=2;a=a*a;}return (ll)r;}
int a[M+1];
map<ll,ll> mp;
ll dp[M+1][M+1];
int main() {
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);//ifstream fin("input.txt");ofstream fout("output.txt");
    ll t,i,s,j,m,x,y,z,k,q,n,p,T;string s1,s2,s3;x=0;p=0;
    cin>>n;int cnt=0;
    for(i=1;i<=n;++i){cin>>a[i];if(mp.find(a[i])==mp.end())mp.ins({a[i],++cnt});}
    // for(i=1;i<=)
    for(i=1;i<=n;++i){
    	for(j=1;j<i;++j){
    		dp[mp[a[i]]][mp[a[j]]]=max(dp[mp[a[i]]][mp[a[j]]],dp[mp[a[j]]][mp[a[i]]]+1);

    	}
    }p=0;
    for(i=1;i<=n;++i)for(j=1;j<=n;++j)p=max(dp[i][j],p);
    	cout<<p+1;

}


/*
 #######################################################
 #                                                     #
 #        masaka :(               #
 #                                                     #
 #######################################################
  * */
  #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
#define vll vector< ll >
#define M 4000
#define pb push_back
#define REP(i,a,b) for(ll i = a; i <= b; i++)
#define pii pair<ll,ll>
#define vec(a) vector<a >
#define se second
#define fi first
#define ins insert
#define inchar
#define outchar(x) putchar_unlocked(x)
 // template<typename T>inline void in(T &x){x=0;register T c=inchar();while(((c<48)||(c>57))&&(c!='-'))c=inchar();bool neg=false;if(c=='-')neg=true;for(;c<48||c>57;c=inchar());for(;c>47&&c<58;c=inchar())x=(x<<3)+(x<<1)+(c&15);if(neg)x=-x;}
 // template<typename T>inline void out(T n){if(n<0){outchar('-');n*=-1;}char snum[65];ll i=0;do {snum[i++]=n%10+'0';n/=10;}while(n);i=i-1;while(i>=0)outchar(snum[i--]);outchar('\n');}
#define MD 1000000007
ll md=MD;
inline ll mulD(ll a,ll b){ll r=0;while(b>0){if(b&1){r=r+a%md;r=r%md;}b/=2;a=2*a%md;a=a%md;}return r%md;;}
inline ll exp(ll a,ll b){ll r=1LL;while(b>0){if(b&1){r=r*(a%md);r=(r+md)%md;}b/=2;a=(a%md)*(a%md);a=(a+md)%md;}return (r+md)%md;}
inline ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
inline ll poww(ll a,ll b){ll r=1LL;while(b>0){if(b&1)r=r*a;b/=2;a=a*a;}return (ll)r;}
int a[M+1];
map<ll,ll> mp;
int dp[M+1][M+1];
int main() {
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);//ifstream fin("input.txt");ofstream fout("output.txt");
    int t,i,s,j,m,x,y,z,k,q,n,p,T;string s1,s2,s3;x=0;p=0;
    cin>>n;int cnt=0;
    for(i=1;i<=n;++i){cin>>a[i];if(mp.find(a[i])==mp.end())mp.ins({a[i],++cnt});}
    // for(i=1;i<=)
    	p=0;
    for(i=1;i<=n;++i){
    	for(j=1;j<i;++j){
    		dp[i][mp[a[j]]]=max(dp[i][mp[a[j]]],dp[j][mp[a[i]]]+1);
    		p=max(p,dp[i][mp[a[j]]]);
    	}
    }p=0;
    // for(i=1;i<=n;++i)for(j=1;j<=n;++j)p=max(dp[i][j],p);
    	cout<<p+1;

}


/*
 #######################################################
 #                                                     #
 #        masaka :(               #
 #                                                     #
 #######################################################
  * */
  #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
#define vll vector< ll >
#define M 4000
#define pb push_back
#define REP(i,a,b) for(ll i = a; i <= b; i++)
#define pii pair<ll,ll>
#define vec(a) vector<a >
#define se second
#define fi first
#define ins insert
#define inchar
#define outchar(x) putchar_unlocked(x)
 // template<typename T>inline void in(T &x){x=0;register T c=inchar();while(((c<48)||(c>57))&&(c!='-'))c=inchar();bool neg=false;if(c=='-')neg=true;for(;c<48||c>57;c=inchar());for(;c>47&&c<58;c=inchar())x=(x<<3)+(x<<1)+(c&15);if(neg)x=-x;}
 // template<typename T>inline void out(T n){if(n<0){outchar('-');n*=-1;}char snum[65];ll i=0;do {snum[i++]=n%10+'0';n/=10;}while(n);i=i-1;while(i>=0)outchar(snum[i--]);outchar('\n');}
#define MD 1000000007
ll md=MD;
inline ll mulD(ll a,ll b){ll r=0;while(b>0){if(b&1){r=r+a%md;r=r%md;}b/=2;a=2*a%md;a=a%md;}return r%md;;}
inline ll exp(ll a,ll b){ll r=1LL;while(b>0){if(b&1){r=r*(a%md);r=(r+md)%md;}b/=2;a=(a%md)*(a%md);a=(a+md)%md;}return (r+md)%md;}
inline ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
inline ll poww(ll a,ll b){ll r=1LL;while(b>0){if(b&1)r=r*a;b/=2;a=a*a;}return (ll)r;}
int a[M+1];
map<ll,ll> mp;
ll dp[M+1][M+1];
int main() {
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);//ifstream fin("input.txt");ofstream fout("output.txt");
    ll t,i,s,j,m,x,y,z,k,q,n,p,T;string s1,s2,s3;x=0;p=0;
    cin>>n;int cnt=0;
    for(i=1;i<=n;++i){cin>>a[i];if(mp.find(a[i])==mp.end())mp.ins({a[i],++cnt});}
    // for(i=1;i<=)
    for(i=1;i<=n;++i){
    	for(j=1;j<i;++j){
    		dp[i][mp[a[j]]]=max(dp[i][mp[a[j]]],dp[j][mp[a[i]]]+1);

    	}
    }p=0;
    for(i=1;i<=n;++i)for(j=1;j<=n;++j)p=max(dp[i][j],p);
    	cout<<p+1;

}


/*
 #######################################################
 #                                                     #
 #        masaka :(               #
 #                                                     #
 #######################################################
  * */
  #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
#define vll vector< ll >
#define M 4000
#define pb push_back
#define REP(i,a,b) for(ll i = a; i <= b; i++)
#define pii pair<ll,ll>
#define vec(a) vector<a >
#define se second
#define fi first
#define ins insert
#define inchar
#define outchar(x) putchar_unlocked(x)
 // template<typename T>inline void in(T &x){x=0;register T c=inchar();while(((c<48)||(c>57))&&(c!='-'))c=inchar();bool neg=false;if(c=='-')neg=true;for(;c<48||c>57;c=inchar());for(;c>47&&c<58;c=inchar())x=(x<<3)+(x<<1)+(c&15);if(neg)x=-x;}
 // template<typename T>inline void out(T n){if(n<0){outchar('-');n*=-1;}char snum[65];ll i=0;do {snum[i++]=n%10+'0';n/=10;}while(n);i=i-1;while(i>=0)outchar(snum[i--]);outchar('\n');}
#define MD 1000000007
ll md=MD;

inline ll mulD(ll a,ll b){ll r=0;while(b>0){if(b&1){r=r+a%md;r=r%md;}b/=2;a=2*a%md;a=a%md;}return r%md;;}
inline ll exp(ll a,ll b){ll r=1LL;while(b>0){if(b&1){r=r*(a%md);r=(r+md)%md;}b/=2;a=(a%md)*(a%md);a=(a+md)%md;}return (r+md)%md;}
inline ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
inline ll poww(ll a,ll b){ll r=1LL;while(b>0){if(b&1)r=r*a;b/=2;a=a*a;}return (int)r;}
int f[M+1][M+1],a[M+1];
map<ll,ll> mp;
int main() {
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);//ifstream fin("input.txt");ofstream fout("output.txt");
    ll t,i,s,j,m,x,y,z,k,q,n,p,T;string s1,s2,s3;x=0;p=0;
    cin>>n;
    for(i=1;i<=n;++i)cin>>a[i];
    // for(i=1;i<=)
    for(i=1;i<=n;++i){
    	for(j=i-1;j>=1;--j){
    		if(a[j]==a[i])break;

    	}
    	k=j;ll ans=0;
    	for(j=i-1;j>=0;--j){
    		if(a[j]==a[i])break;
    			f[i][j]=f[j][k]+1;
    	}
    }p=1;
    for(i=1;i<=n;++i){for(j=1;j<=n;++j)p=max(p,(1ll)*f[i][j]);}cout<<p;

}


#include<bits/stdc++.h>
#include <chrono>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define MOD 1000000007
#define PI 3.14159265
#define INF 1000000000000000000
#define pll pair<ll,ll>
#define fastio  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define vll vector<ll>
#define all(a) a.begin(),a.end()
#define forl(i,n) for(ll i=0;i<n;i++)
#define fore(i,n) for(ll i=1;i<=n;i++)

ll max(ll a,ll b)
{
    if(a>b)
    return a;
    return b;
}

ll min(ll a,ll b)
{
    return a+b-max(a,b);
}
ll mulmod(ll a, ll b, ll mod)
{
    /*ll res = 0; // Initialize result
    a = a % mod;
    while (b > 0)
    {
        // If b is odd, add 'a' to result
        if (b % 2 == 1)
            res = (res + a) % mod;

        // Multiply 'a' with 2
        a = (a * 2) % mod;

        // Divide b by 2
        b /= 2;
    }

    // Return result
    return res % mod; */
    return ((a%MOD)*(b%MOD)%MOD);
}
ll mod(ll a,ll b)
{
    if(a>b)
    return a-b;
    else
    return b-a;
}
ll power(ll x,ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x)%p;
        y = y>>1;
        x = (x*x)%p;
    }
    return res;

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vector<ll> z_function(string s) {
   ll n = (ll) s.length();
   vector<ll> z(n);
   for (ll i = 1, l = 0, r = 0; i < n; ++i) {
      if (i <= r)
         z[i] = min (r - i + 1, z[i - l]);
      while (i + z[i] < n && s[z[i]] == s[i + z[i]])
         ++z[i];
      if (i + z[i] - 1 > r)
         l = i, r = i + z[i] - 1;
   }
   return z;
}
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}
ll lcm(ll a,ll b,ll c)
{
    ll rets = (a*b)/(__gcd(a,b));
    return (rets*c)/(__gcd(rets,c));
}
bool prime[100005];

void prep()
{

    memset(prime, true, sizeof(prime));
    prime[1] = false;
    for (int p=2; p*p<=100000; p++)
    {
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true)
        {
            // Update all multiples of p greater than or
            // equal to the square of it
            // numbers which are multiple of p and are
            // less than p^2 are already been marked.
            for (int i=p*p; i<=100000; i += p)
                prime[i] = false;
        }
    }
    return;
}
ll modInv(ll x)
{
    return power(x,MOD-2,MOD);
}
ll fact[200005];
ll ncr(ll n,ll r)
{
    ll x = fact[n];
    ll y = fact[n-r];
    ll z = fact[r];
    ll ff = (y*z)%MOD;
    ll gg1 = modInv(ff);
    return (gg1*x)%MOD;
}
// ll prob(ll x,ll y)
// {
//     return (x*modInv(y))%MOD;
// }
// ll dist(ll x1,ll y1,ll x2,ll y2)
// {
//     return mod(x1,x2)+mod(y1,y2);
// }

ll logs(ll x)
{
    ll tot = 0;
    ll n = x;
    while(x>0)
    {
        x/=2;
        tot++;
    }
    return tot;
}

ll case_no = 1;
void op()
{
    cout<<"Case #"<<case_no<<": ";
    case_no++;

}
ll f(ll a,ll b,ll c)
{
    return (a-b)*(a-b) + (b-c)*(b-c) + (c-a)*(c-a);
}
ll gg(ll x)
{
    ll ctr = 0;
    while(x>1)
    {
        x/=2;
        ctr++;
    }
    return ctr;
}

vector<int> bit;
int getSum( int index)
{
    int sum = 0; // Iniialize result

    // index in BITree[] is 1 more than the index in arr[]
    index = index + 1;

    // Traverse ancestors of BITree[index]
    while (index>0)
    {
        // Add current element of BITree to sum
        sum += bit[index];

        // Move index to parent node in getSum View
        index -= index & (-index);
    }
    return sum;
}

// Updates a node in Binary Index Tree (BITree) at given index
// in BITree. The given value 'val' is added to BITree[i] and
// all of its ancestors in tree.
void update(int n, int index, int val)
{
    // index in BITree[] is 1 more than the index in allrr[]
    index = index + 1;
    while (index <= n)
    {
    bit[index] += val;
    index += index & (-index);
    }
}

vll vec;
ll n;
void add(ll x,ll y,ll val,ll depth)
{
    if(val>n)
    return;
    if(depth>10)
    return;
    add(x,y,val*10+x,depth+1);
    add(x,y,val*10+y,depth+1);
    if(val<=n)
    vec.pb(val);

}
void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    ll ans = 0;
    map<ll,ll> m2;
    for(auto it:vec)
    {
        m2[it]++;
        ans = max(ans,m2[it]);
    }
    forl(i,n)
    {
        map<ll,ll> m1;
        ll cur = 1;
        ll others = 0;
        ll prev = vec[i];

        for(ll j=i+1;j<n;j++)
        {
            if(vec[j]!=vec[i])
            {
                if(m1[vec[j]]<cur)
                m1[vec[j]]++;
                others = max(others,m1[vec[j]]);
                if(m1[vec[j]]==cur)
                ans = max(ans,cur*2);
                prev = vec[j];
            }
            else
            {
                if(prev!=vec[i])
                {
                    cur++;
                    if(others+1 == cur)
                    ans = max(ans,2*cur-1);
                    prev = vec[i];
                }
                else
                ;
            }
        }

    }
    cout<<ans;

}
int main()
{
    fastio;

    ll t;
    //cin>>t;
    t = 1;

    //prep();
    while(t--)
    {
        solve();

    }

}


#include<bits/stdc++.h>
#include <chrono>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define MOD 1000000007
#define PI 3.14159265
#define INF 1000000000000000000
#define pll pair<ll,ll>
#define fastio  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define vll vector<ll>
#define all(a) a.begin(),a.end()
#define forl(i,n) for(ll i=0;i<n;i++)
#define fore(i,n) for(ll i=1;i<=n;i++)

ll max(ll a,ll b)
{
    if(a>b)
    return a;
    return b;
}

ll min(ll a,ll b)
{
    return a+b-max(a,b);
}
ll mulmod(ll a, ll b, ll mod)
{
    /*ll res = 0; // Initialize result
    a = a % mod;
    while (b > 0)
    {
        // If b is odd, add 'a' to result
        if (b % 2 == 1)
            res = (res + a) % mod;

        // Multiply 'a' with 2
        a = (a * 2) % mod;

        // Divide b by 2
        b /= 2;
    }

    // Return result
    return res % mod; */
    return ((a%MOD)*(b%MOD)%MOD);
}
ll mod(ll a,ll b)
{
    if(a>b)
    return a-b;
    else
    return b-a;
}
ll power(ll x,ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x)%p;
        y = y>>1;
        x = (x*x)%p;
    }
    return res;

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vector<ll> z_function(string s) {
   ll n = (ll) s.length();
   vector<ll> z(n);
   for (ll i = 1, l = 0, r = 0; i < n; ++i) {
      if (i <= r)
         z[i] = min (r - i + 1, z[i - l]);
      while (i + z[i] < n && s[z[i]] == s[i + z[i]])
         ++z[i];
      if (i + z[i] - 1 > r)
         l = i, r = i + z[i] - 1;
   }
   return z;
}
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}
ll lcm(ll a,ll b,ll c)
{
    ll rets = (a*b)/(__gcd(a,b));
    return (rets*c)/(__gcd(rets,c));
}
bool prime[100005];

void prep()
{

    memset(prime, true, sizeof(prime));
    prime[1] = false;
    for (int p=2; p*p<=100000; p++)
    {
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true)
        {
            // Update all multiples of p greater than or
            // equal to the square of it
            // numbers which are multiple of p and are
            // less than p^2 are already been marked.
            for (int i=p*p; i<=100000; i += p)
                prime[i] = false;
        }
    }
    return;
}
ll modInv(ll x)
{
    return power(x,MOD-2,MOD);
}
ll fact[200005];
ll ncr(ll n,ll r)
{
    ll x = fact[n];
    ll y = fact[n-r];
    ll z = fact[r];
    ll ff = (y*z)%MOD;
    ll gg1 = modInv(ff);
    return (gg1*x)%MOD;
}
// ll prob(ll x,ll y)
// {
//     return (x*modInv(y))%MOD;
// }
// ll dist(ll x1,ll y1,ll x2,ll y2)
// {
//     return mod(x1,x2)+mod(y1,y2);
// }

ll logs(ll x)
{
    ll tot = 0;
    ll n = x;
    while(x>0)
    {
        x/=2;
        tot++;
    }
    return tot;
}

ll case_no = 1;
void op()
{
    cout<<"Case #"<<case_no<<": ";
    case_no++;

}
ll f(ll a,ll b,ll c)
{
    return (a-b)*(a-b) + (b-c)*(b-c) + (c-a)*(c-a);
}
ll gg(ll x)
{
    ll ctr = 0;
    while(x>1)
    {
        x/=2;
        ctr++;
    }
    return ctr;
}

vector<int> bit;
int getSum( int index)
{
    int sum = 0; // Iniialize result

    // index in BITree[] is 1 more than the index in arr[]
    index = index + 1;

    // Traverse ancestors of BITree[index]
    while (index>0)
    {
        // Add current element of BITree to sum
        sum += bit[index];

        // Move index to parent node in getSum View
        index -= index & (-index);
    }
    return sum;
}

// Updates a node in Binary Index Tree (BITree) at given index
// in BITree. The given value 'val' is added to BITree[i] and
// all of its ancestors in tree.
void update(int n, int index, int val)
{
    // index in BITree[] is 1 more than the index in allrr[]
    index = index + 1;
    while (index <= n)
    {
    bit[index] += val;
    index += index & (-index);
    }
}

vll vec;
ll n;
void add(ll x,ll y,ll val,ll depth)
{
    if(val>n)
    return;
    if(depth>10)
    return;
    add(x,y,val*10+x,depth+1);
    add(x,y,val*10+y,depth+1);
    if(val<=n)
    vec.pb(val);

}
void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    ll ans = 0;
    map<ll,ll> m2;
    for(auto it:vec)
    {
        m2[it]++;
        ans = max(ans,m2[it]);
    }
    forl(i,n)
    {
        map<ll,ll> m1;
        ll cur = 1;
        ll others = 0;
        ll prev = vec[i];

        for(ll j=i+1;j<n;j++)
        {
            if(vec[j]!=vec[i])
            {
                if(m1[vec[j]]==cur-1)
                m1[vec[j]]++;
                others = max(others,m1[vec[j]]);
                if(m1[vec[j]]==cur)
                ans = max(ans,cur*2);
                prev = vec[j];
            }
            else
            {
                if(prev!=vec[i])
                {
                    cur++;
                    if(others+1 == cur)
                    ans = max(ans,2*cur-1);
                    prev = vec[i];
                }
                else
                ;
            }
        }

    }
    cout<<ans;

}
int main()
{
    fastio;

    ll t;
    //cin>>t;
    t = 1;

    //prep();
    while(t--)
    {
        solve();

    }

}