#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const int N = 4010;
vector<int>g[N];

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;
    set<int>s;
    for(int i = 1;i<=n;i++)
    {
        int a;
        cin>>a;
        g[a].emplace_back(i);
        s.insert(a);
    }
    int ans = 0;

    for(auto i:s)
    {
        for(auto j:s)
        {
            int id = g[i][0], c=1;
            while(1)
            {
                auto it = upper_bound(g[j].begin(), g[j].end(), id);
                if(it == g[j].end())
                    break;
                id = *it;
                c++;
                it = upper_bound(g[i].begin(), g[i].end(), id);
                if(it == g[i].end())
                    break;
                id = *it;
                c++;
            }
            ans = max(ans, c);
        }
    }
    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

const long long N = 4010;
vector<long long>g[N];

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;
    set<long long>s;
    for(long long i = 1;i<=n;i++)
    {
        long long a;
        cin>>a;
        g[a].emplace_back(i);
        s.insert(a);
    }
    long long ans = 0;

    for(auto i:s)
    {
        for(auto j:s)
        {
            long long id = g[i][0], c=1;
            while(1)
            {
                auto it = upper_bound(g[j].begin(), g[j].end(), id);
                if(it == g[j].end())
                    break;
                id = *it;
                c++;
                auto it2 = upper_bound(g[i].begin(), g[i].end(), id);
                if(it2 == g[i].end())
                    break;
                id = *it2;
                c++;
            }
            ans = max(ans, c);
        }
    }
    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, a[4001];
map <int, vector <int> > occ;
set <int> dist_num, curr;
map <int, int> curr_count;
map <int, bool> another_one;

int main()
{
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i = 0; i < n; i++)
	{
		cin>>a[i];
		occ[a[i]].push_back(i);
		dist_num.insert(a[i]);
	}

	int ans = 0;
	for (auto num: dist_num)
	{
		for (int i = 0; i < occ[num].size() - 1; i++)
		{
			int start = occ[num][i], end = occ[num][i + 1];

			for (int j = start + 1; j <= end - 1; j++)
				curr.insert(a[j]);
			for (auto curr_num: curr)
				curr_count[curr_num]++;

			curr.clear();
		}

		int last_of_num = occ[num][occ[num].size() - 1];
		for (auto x: dist_num)
			another_one[x] = (upper_bound(occ[x].begin(), occ[x].end(), last_of_num) != occ[x].end());

		int curr_ans = 0;
		for (auto x: dist_num)
			if (another_one[x])
				curr_ans = max(curr_ans, 2*curr_count[x] + 2);
			else
				curr_ans = max(curr_ans, 2*curr_count[x] + 1);

		ans = max(curr_ans, ans);
		curr_count.clear();
		another_one.clear();
	}

	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, a[4001];
map <int, vector <int> > occ;
set <int> dist_num, curr;
unordered_map <int, int> curr_count;
unordered_map <int, bool> another_one;

int main()
{
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i = 0; i < n; i++)
	{
		cin>>a[i];
		occ[a[i]].push_back(i);
		dist_num.insert(a[i]);
	}

	int ans = 0;
	for (int num: dist_num)
	{
		for (int i = 0; i < occ[num].size() - 1; i++)
		{
			int start = occ[num][i], end = occ[num][i + 1];

			for (int j = start + 1; j <= end - 1; j++)
				curr.insert(a[j]);
			for (int curr_num: curr)
				curr_count[curr_num]++;

			curr.clear();
		}

		int last_of_num = occ[num][occ[num].size() - 1];
		for (int x: dist_num)
			another_one[x] = (upper_bound(occ[x].begin(), occ[x].end(), last_of_num) != occ[x].end());

		int curr_ans = 0;
		for (int x: dist_num)
			if (another_one[x])
				curr_ans = max(curr_ans, 2*curr_count[x] + 2);
			else
				curr_ans = max(curr_ans, 2*curr_count[x] + 1);

		ans = max(curr_ans, ans);
		curr_count.clear();
		another_one.clear();
	}

	for (int x: dist_num)
		ans = max(ans, (int)occ[x].size());

	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, a[4001];
vector <int> occ[1000001];
set <int> dist_num, curr;
int curr_count[1000001];
bool another_one[1000001];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for (int i = 0; i < n; i++)
	{
		cin>>a[i];
		occ[a[i]].push_back(i);
		dist_num.insert(a[i]);
	}

	int ans = 0;
	for (int num: dist_num)
	{
		for (int i = 0; i < occ[num].size() - 1; i++)
		{
			int start = occ[num][i], end = occ[num][i + 1];

			for (int j = start + 1; j <= end - 1; j++)
				curr.insert(a[j]);
			for (int curr_num: curr)
				curr_count[curr_num]++;

			curr.clear();
		}

		int last_of_num = occ[num][occ[num].size() - 1];
		for (int x: dist_num)
			another_one[x] = (upper_bound(occ[x].begin(), occ[x].end(), last_of_num) != occ[x].end());

		int curr_ans = 0;
		for (int x: dist_num)
			if (another_one[x])
				curr_ans = max(curr_ans, 2*curr_count[x] + 2);
			else
				curr_ans = max(curr_ans, 2*curr_count[x] + 1);

		ans = max(curr_ans, ans);
		memset(curr_count, 0, sizeof curr_count);
		memset(another_one, 0, sizeof another_one);
	}

	for (int x: dist_num)
		ans = max(ans, (int)occ[x].size());

	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, a[4001];
vector <int> occ[1000001];
set <int> dist_num, curr;
int curr_count[1000001];
bool another_one[1000001];

int main()
{
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i = 0; i < n; i++)
	{
		cin>>a[i];
		occ[a[i]].push_back(i);
		dist_num.insert(a[i]);
	}

	int ans = 0;
	for (int num: dist_num)
	{
		for (int i = 0; i < occ[num].size() - 1; i++)
		{
			int start = occ[num][i], end = occ[num][i + 1];

			for (int j = start + 1; j <= end - 1; j++)
				curr.insert(a[j]);
			for (int curr_num: curr)
				curr_count[curr_num]++;

			curr.clear();
		}

		int last_of_num = occ[num][occ[num].size() - 1];
		for (int x: dist_num)
			another_one[x] = (upper_bound(occ[x].begin(), occ[x].end(), last_of_num) != occ[x].end());

		int curr_ans = 0;
		for (int x: dist_num)
			if (another_one[x])
				curr_ans = max(curr_ans, 2*curr_count[x] + 2);
			else
				curr_ans = max(curr_ans, 2*curr_count[x] + 1);

		ans = max(curr_ans, ans);
		memset(curr_count, 0, sizeof curr_count);
		memset(another_one, 0, sizeof another_one);
	}

	for (int x: dist_num)
		ans = max(ans, (int)occ[x].size());

	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int a[4000], n, curr_count[1000001], another[1000001];
set <int> dist_nums, curr;

int next_occ(int x, int i)
{
	for (int j = i + 1; j < n; j++)
		if (a[j] == x)
			return j;
	return -1;
}

void clear_curr()
{
	for (auto x: dist_nums)
	{
		curr_count[x] = 0;
		another[x] = 0;
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i = 0; i < n; i++)
	{
		cin>>a[i];
		dist_nums.insert(a[i]);
	}

	int ans = 0;
	for (auto x: dist_nums)
	{
		int start = next_occ(x, -1);
		int end = next_occ(x, start);

		while (end != -1)
		{
			for (int i = start + 1; i <= end - 1; i++)
				curr.insert(a[i]);

			for (auto c: curr)
				curr_count[c]++;
			curr.clear();

			start = end;
			end = next_occ(x, end);
		}

		for (int i = start + 1; i < n; i++)
			another[a[i]] = 1;

		for (auto x: dist_nums)
			if (another[x])
				ans = max(ans, 2*curr_count[x] + 2);
			else
				ans = max(ans, 2*curr_count[x] + 1);

		clear_curr();
	}

	for (int i = 0; i < n; i++)
		curr_count[a[i]]++;
	for (auto x: dist_nums)
		ans = max(curr_count[x], ans);

	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, a[4001];
map <int, vector <int> > occ;
set <int> dist_num, curr;
map <int, int> curr_count;
map <int, bool> another_one;

int main()
{
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i = 0; i < n; i++)
	{
		cin>>a[i];
		occ[a[i]].push_back(i);
		dist_num.insert(a[i]);
	}

	int ans = 0;
	for (auto num: dist_num)
	{
		for (int i = 0; i < occ[num].size() - 1; i++)
		{
			int start = occ[num][i], end = occ[num][i + 1];

			for (int j = start + 1; j <= end - 1; j++)
				curr.insert(a[j]);
			for (auto curr_num: curr)
				curr_count[curr_num]++;

			curr.clear();
		}

		int last_of_num = occ[num][occ[num].size() - 1];
		for (auto x: dist_num)
			another_one[x] = (upper_bound(occ[x].begin(), occ[x].end(), last_of_num) != occ[x].end());

		int curr_ans = 0;
		for (auto x: dist_num)
			if (another_one[x])
				curr_ans = max(curr_ans, 2*curr_count[x] + 2);
			else
				curr_ans = max(curr_ans, 2*curr_count[x] + 1);

		ans = max(curr_ans, ans);
		curr_count.clear();
		another_one.clear();
	}

	for (auto x: dist_num)
		ans = max(ans, (int)occ[x].size());

	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, a[4001];
vector <int> occ[1000001];
set <int> dist_num, curr;
int curr_count[1000001];
bool another_one[1000001];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for (int i = 0; i < n; i++)
	{
		cin>>a[i];
		occ[a[i]].push_back(i);
		dist_num.insert(a[i]);
	}

	int ans = 0;
	for (int num: dist_num)
	{
		for (int i = 0; i < occ[num].size() - 1; i++)
		{
			int start = occ[num][i], end = occ[num][i + 1];

			for (int j = start + 1; j <= end - 1; j++)
				curr.insert(a[j]);
			for (int curr_num: curr)
				curr_count[curr_num]++;

			curr.clear();
		}

		int last_of_num = occ[num][occ[num].size() - 1];
		for (int i = last_of_num; i < n; i++)
			another_one[a[i]] = 1;

		int curr_ans = 0;
		for (int x: dist_num)
			if (another_one[x])
				curr_ans = max(curr_ans, 2*curr_count[x] + 2);
			else
				curr_ans = max(curr_ans, 2*curr_count[x] + 1);

		ans = max(curr_ans, ans);
		memset(curr_count, 0, sizeof curr_count);
		memset(another_one, 0, sizeof another_one);
	}

	for (int x: dist_num)
		ans = max(ans, (int)occ[x].size());

	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int a[4000], n, curr_count[1000001], another[1000001];
set <int> dist_nums, curr;

int next_occ(int x, int i)
{
	for (int j = i + 1; j < n; j++)
		if (a[j] == x)
			return j;
	return -1;
}

void clear_curr()
{
	for (auto x: dist_nums)
	{
		curr_count[x] = 0;
		another[x] = 0;
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin>>n;
	for (int i = 0; i < n; i++)
	{
		cin>>a[i];
		dist_nums.insert(a[i]);
	}

	int ans = 0;
	for (auto x: dist_nums)
	{
		int start = next_occ(x, -1);
		int end = next_occ(x, start);

		while (end != -1)
		{
			for (int i = start + 1; i <= end - 1; i++)
				curr.insert(a[i]);

			for (auto c: curr)
				curr_count[c]++;
			curr.clear();

			start = end;
			end = next_occ(x, end);
		}

		for (int i = start + 1; i < n; i++)
			another[a[i]] = 1;

		for (auto x: dist_nums)
			if (another[x])
				ans = max(ans, 2*curr_count[x] + 2);
			else
				ans = max(ans, 2*curr_count[x] + 1);

		clear_curr();
	}

	for (int i = 0; i < n; i++)
		curr_count[a[i]]++;
	for (auto x: dist_nums)
		ans = max(curr_count[x], ans);

	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, a[4001];
map <int, vector <int> > occ;
set <int> dist_num, curr;
map <int, int> curr_count;
map <int, bool> another_one;

int main()
{
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i = 0; i < n; i++)
	{
		cin>>a[i];
		occ[a[i]].push_back(i);
		dist_num.insert(a[i]);
	}

	int ans = 0;
	for (int num: dist_num)
	{
		for (int i = 0; i < occ[num].size() - 1; i++)
		{
			int start = occ[num][i], end = occ[num][i + 1];

			for (int j = start + 1; j <= end - 1; j++)
				curr.insert(a[j]);
			for (int curr_num: curr)
				curr_count[curr_num]++;

			curr.clear();
		}

		int last_of_num = occ[num][occ[num].size() - 1];
		for (int x: dist_num)
			another_one[x] = (upper_bound(occ[x].begin(), occ[x].end(), last_of_num) != occ[x].end());

		int curr_ans = 0;
		for (int x: dist_num)
			if (another_one[x])
				curr_ans = max(curr_ans, 2*curr_count[x] + 2);
			else
				curr_ans = max(curr_ans, 2*curr_count[x] + 1);

		ans = max(curr_ans, ans);
		curr_count.clear();
		another_one.clear();
	}

	for (int x: dist_num)
		ans = max(ans, (int)occ[x].size());

	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, a[4001];
vector <int> occ[1000001];
set <int> dist_num, curr;
int curr_count[1000001];
bool another_one[1000001];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for (int i = 0; i < n; i++)
	{
		cin>>a[i];
		occ[a[i]].push_back(i);
		dist_num.insert(a[i]);
	}

	int ans = 0;
	for (int num: dist_num)
	{
		for (int i = 0; i < occ[num].size() - 1; i++)
		{
			int start = occ[num][i], end = occ[num][i + 1];

			for (int j = start + 1; j <= end - 1; j++)
				curr.insert(a[j]);
			for (int curr_num: curr)
				curr_count[curr_num]++;

			curr.clear();
		}

		int last_of_num = occ[num][occ[num].size() - 1];
		for (int i = last_of_num + 1; i < n; i++)
			another_one[a[i]] = 1;

		int curr_ans = 0;
		for (int x: dist_num)
			if (another_one[x])
				curr_ans = max(curr_ans, 2*curr_count[x] + 2);
			else
				curr_ans = max(curr_ans, 2*curr_count[x] + 1);

		ans = max(curr_ans, ans);
		memset(curr_count, 0, sizeof curr_count);
		memset(another_one, 0, sizeof another_one);
	}

	for (int x: dist_num)
		ans = max(ans, (int)occ[x].size());

	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, a[4001];
map <int, vector <int> > occ;
set <int> dist_num, curr;
map <int, int> curr_count;
map <int, bool> another_one;

int main()
{
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i = 0; i < n; i++)
	{
		cin>>a[i];
		occ[a[i]].push_back(i);
		dist_num.insert(a[i]);
	}

	int ans = 0;
	for (auto num: dist_num)
	{
		for (int i = 0; i < occ[num].size() - 1; i++)
		{
			int start = occ[num][i], end = occ[num][i + 1];

			for (int j = start + 1; j <= end - 1; j++)
				curr.insert(a[j]);
			for (auto curr_num: curr)
				curr_count[curr_num]++;

			curr.clear();
		}

		int last_of_num = occ[num][occ[num].size() - 1];
		// cout<<"last occ of "<<num<<" = "<<last_of_num<<endl;
		for (auto x: dist_num)
			if (x != num)
				another_one[x] = (upper_bound(occ[x].begin(), occ[x].end(), last_of_num) != occ[x].end());

		int curr_ans = 0;
		for (auto x: dist_num)
		{
			if (x != num)
			{
				// cout<<"num = "<<num<<", x = "<<x<<", curr_count = "<<curr_count[x]<<", another_one = "<<another_one[x]<<endl;
				if (another_one[x])
					curr_ans = max(curr_ans, 2*curr_count[x] + 2);
				else
					curr_ans = max(curr_ans, 2*curr_count[x] + 1);
			}
		}
		ans = max(curr_ans, ans);
		curr_count.clear();
		another_one.clear();
	}

	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define max1 1000000007
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
int main()
{
    iostream::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    cin>>n;
    li b[n];
    forn(i,n)
        cin>>b[i];
    li a[n][n];
    forn(i,n)
        forn(j,n)
            a[i][j]=1;
   // a[0][0]=1;
    unordered_map<int,vector<int> > m;
    forn(i,n)
    {
        m[b[i]].pb(i);
    }
    /*forn(i,m.size())
    {
        forn(j,m[i].size())
            cout<<i<<" "<<m[i][j]<<" ";
        cout<<endl;
    }*/

    forn(i,n)
    {
        for(li j=0;j<=i;j++)
        {
            //if(j>i)
                //a[i][j]=0;

            if(i==j)
                a[i][j]=1;
            else if(j==0)
            {
                a[i][j]=2;
            }
            else
            {
                if(m.find(b[i])!=m.end())
                {
                //	cout<<i<<" "<<j<<" "<<endl;
                    for(li k=0;k<m[b[i]].size();k++)
                    {
                    	//cout<<i<<" "<<j<<" "<<k<<endl;
                        if(1+a[j][k]>a[i][j]&&k<j&&k!=i )
                            a[i][j]=1+a[j][k];
                    }
                }
                if(a[i][j]==1)
                    a[i][j]=2;
            }
        }
    }
    li maxm=1;
    // 12 14 17 19
    //cout<<a[17][14]<<endl<<a[14][12]<<endl;//<<" "<<b[14]<<" "<<b[17]<<" "<<b[19]<<endl;
    forn(i,n)
    {
        forn(j,n)
        {
            if(a[i][j]>maxm)
                maxm=a[i][j];
        }
    }
    cout<<maxm;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define max1 1000000007
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
int main()
{
    iostream::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    cin>>n;
    li b[n];
    forn(i,n)
        cin>>b[i];
    li a[n][n];
    forn(i,n)
        forn(j,n)
            a[i][j]=1;
   // a[0][0]=1;
    map<int,int> m;
    li k=1;
    forn(i,n)
    {
        if(m[b[i]]==0){
            m[b[i]]=k;
            k++;}
    }
    li c[n];
    forn(i,n)
        c[i]=m[b[i]]-1;
    //forn(i,n)
      //  cout<<c[i]<<endl;
    forn(i,n)
    {
        for(li j=0;j<i;j++)
        {
            a[i][c[j]]=max(a[i][c[j]],1+a[j][c[i]]);
            //cout<<i<<" "<<j<<" "<<a[i][j]<<endl;
        }
    }
    li maxm=1;
    forn(i,n)
    {
        forn(j,n)
        {
            if(a[i][j]>maxm)
                maxm=a[i][j];
        }
    }
    cout<<maxm;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define max1 1000000007
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
int main()
{
    iostream::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    cin>>n;
    li b[n];
    forn(i,n)
        cin>>b[i];
    li a[n][n];
    forn(i,n)
        forn(j,n)
            a[i][j]=1;
   // a[0][0]=1;
    map<int,int> m;
    li k=0;
    forn(i,n)
    {
        if(m[i]==0){
            m[i]=k;
            k++;}
    }
    li c[n];
    forn(i,n)
        c[i]=m[b[i]];
    forn(i,n)
    {
        for(li j=0;j<i;j++)
        {
            a[i][j]=max(a[i][c[j]],1+a[j][c[i]]);

        }
    }
    li maxm=1;
    forn(i,n)
    {
        forn(j,n)
        {
            if(a[i][j]>maxm)
                maxm=a[i][j];
        }
    }
    cout<<maxm;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define max1 1000000007
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
int main()
{
    iostream::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    cin>>n;
    li b[n];
    forn(i,n)
        cin>>b[i];
    vector<vector<int> >v(n,vector<int>(3,-1));
    forn(i,n)
        v[i][0]=1;
    v[0][0]=1;v[0][1]=b[0];v[0][2]=-1;
    v[1][0]=2;v[1][1]=b[0];v[1][2]=b[1];
    //v[]
    for(int i=2;i<n;i++)
    {
        bool flag=0;
        for(int j=0;j<i;j++)
        {
            if(b[i]==v[j][1]&&1+v[j][0]>v[i][0])
            {
                v[i][0]=v[j][0]+1;v[i][1]=v[j][2];v[i][2]=v[j][1];
                flag=1;
                //cout<<"j"<<j<<endl;
            }

        }
    }
    //cout<<v[2][0];
    li maxm=v[0][0];
    forn(i,n)
    {
        if(maxm<v[i][0])
            maxm=v[i][0];
    }
    cout<<maxm;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define max1 1000000007
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
int main()
{
    iostream::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    cin>>n;
    li b[n];
    forn(i,n)
        cin>>b[i];
    li a[n][n];
    forn(i,n)
        forn(j,n)
            a[i][j]=1;
   // a[0][0]=1;
    unordered_map<int,vector<int> > m;
    forn(i,n)
    {
        m[b[i]].pb(i);
    }
    /*forn(i,m.size())
    {
        forn(j,m[i].size())
            cout<<i<<" "<<m[i][j]<<" ";
        cout<<endl;
    }*/

    forn(i,n)
    {
        for(li j=0;j<=i;j++)
        {
            //if(j>i)
                //a[i][j]=0;

            if(i==j)
                a[i][j]=1;
            else if(j==0)
            {
                a[i][j]=2;
            }
            else
            {
                if(m.find(b[i])!=m.end())
                {
                //	cout<<i<<" "<<j<<" "<<endl;
                    for(li z=0;z<m[b[i]].size();z++)
                    {
                    	//cout<<i<<" "<<j<<" "<<k<<endl;
                    	li k=m[b[i]][z];
                        if(1+a[j][k]>a[i][j]&&k<j&&k!=i )
                        {
                        	//cout<<i<<" "<<j<<" "<<k<<endl;
                            a[i][j]=1+a[j][k];
                            //cout<<a[i][j]<<endl;
                        }
                    }
                }
                if(a[i][j]==1)
                    a[i][j]=2;
            }
        }
    }
    li maxm=1;
    // 12 14 17 19
    //cout<<a[19][14]<<endl<<a[17][14]<<endl<<a[14][12]<<endl;//<<" "<<b[14]<<" "<<b[17]<<" "<<b[19]<<endl;
    forn(i,n)
    {
        forn(j,n)
        {
            if(a[i][j]>maxm)
                maxm=a[i][j];
        }
    }
    cout<<maxm;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define max1 1000000007
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
int main()
{
    iostream::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    cin>>n;
    li b[n];
    forn(i,n)
        cin>>b[i];
    li a[n][n];
    forn(i,n)
        forn(j,n)
            a[i][j]=1;
   // a[0][0]=1;
    forn(i,n)
    {
        forn(j,n)
        {
            if(j>i)
                a[i][j]=0;
            else if(i==j)
                a[i][j]=1;
            else if(j==0)
            {
                a[i][j]=2;
            }
            else
            {
                for(li k=0;k<j;k++)
                {
                    if(b[k]==b[i]&&1+a[j][k]>a[i][j])
                        a[i][j]=1+a[j][k];
                }
                if(a[i][j]==1)
                    a[i][j]=2;
            }
        }
    }
    li maxm=1;
    // 12 14 17 19
    //cout<<a[14][12];//<<" "<<b[14]<<" "<<b[17]<<" "<<b[19]<<endl;
    forn(i,n)
    {
        forn(j,n)
        {
            if(a[i][j]>maxm)
                maxm=a[i][j];
        }
    }
    cout<<maxm;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define max1 1000000007
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
int main()
{
    iostream::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    cin>>n;
    li b[n];
    forn(i,n)
        cin>>b[i];
    vector<vector<int> >v(n,vector<int>(3,-1));
    forn(i,n)
        v[i][0]=1;
    if(n==1)
    {
        cout<<"1";
        exit(0);
    }
    v[0][0]=1;v[0][1]=b[0];v[0][2]=-1;
    v[1][0]=2;v[1][1]=b[0];v[1][2]=b[1];
    //v[]
    for(int i=2;i<n;i++)
    {
        bool flag=0;
        for(int j=0;j<i;j++)
        {
            if(b[i]==v[j][1]&&1+v[j][0]>v[i][0])
            {
                v[i][0]=v[j][0]+1;v[i][1]=v[j][2];v[i][2]=v[j][1];
                flag=1;
                //cout<<"j"<<j<<endl;
            }

        }
    }
    //cout<<v[2][0];
    li maxm=v[0][0];
    forn(i,n)
    {
        if(maxm<v[i][0])
            maxm=v[i][0];
    }
    cout<<maxm;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define max1 1000000007
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
int main()
{
    iostream::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    li n;
    cin>>n;
    vector<li> a(n);
    forn(i,n)
        cin>>a[i];
    li cnt=0,cnt1=0;
    fore(i,1,n)
    {
        if(a[i]==a[i-1])
        {
            while(a[i]==a[i-1]&&i<n)
            {
                cnt1++;
                i++;
            }
        }
        cnt=max(cnt,cnt1);
        cnt1=0;
    }
    set<li> s;
    s.insert(a[0]);
    li z=0;
    fore(i,1,n)
    {
        //s.insert()
        z=i;
        while(a[i]!=a[i+1]&&s.size()<=2&&i<n)
        {
            s.insert(a[i]);
            i++;
        }
        if(z==1&&s.size()<=2)
            cnt=max(cnt,i-z+1);
        else
        cnt=max(cnt,i-z);
        s.clear();
    }
    cout<<cnt;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define max1 1000000007
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
int main()
{
    iostream::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    li n;
    cin>>n;
    vector<li> a(n);
    forn(i,n)
        cin>>a[i];
    li cnt=0,cnt1=0;
    fore(i,1,n)
    {
        if(a[i]==a[i-1])
        {
            cnt1++;
            while(a[i]==a[i-1]&&i<n)
            {
                cnt1++;
                i++;
            }
        }
        cnt=max(cnt,cnt1);
        //cout<<"cn"<<cnt;
        cnt1=0;
    }
    set<li> s;
    s.insert(a[0]);
    li z=0;
    fore(i,1,n)
    {
        //s.insert()
        z=i;
        while(a[i]!=a[i-1]&&s.size()<=2&&i<n)
        {
            s.insert(a[i]);
            i++;
        }
        //cout<<"er"<<i-z+1;
        if(z==1&&s.size()<=2)
            cnt=max(cnt,i-z+1);
        else
        cnt=max(cnt,i-z);
        s.clear();
        s.insert(a[i]);
    }
    cout<<cnt;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define max1 1000000007
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
int main()
{
    iostream::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    cin>>n;
    li b[n];
    forn(i,n)
        cin>>b[i];
    li a[n][n];
    forn(i,n)
        forn(j,n)
            a[i][j]=1;
   // a[0][0]=1;
    map<int,int> m;
    li k=1;
    forn(i,n)
    {
        if(m[b[i]]==0){
            m[b[i]]=k;
            k++;}
    }
    li c[n];
    forn(i,n)
        c[i]=m[b[i]]-1;
    //forn(i,n)
      //  cout<<c[i]<<endl;
    forn(i,n)
    {
        for(li j=0;j<i;j++)
        {
            a[i][j]=max(a[i][c[j]],1+a[j][c[i]]);
            //cout<<i<<" "<<j<<" "<<a[i][j]<<endl;
        }
    }
    li maxm=1;
    forn(i,n)
    {
        forn(j,n)
        {
            if(a[i][j]>maxm)
                maxm=a[i][j];
        }
    }
    cout<<maxm;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define max1 1000000007
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
int main()
{
    iostream::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    cin>>n;
    li b[n];
    forn(i,n)
        cin>>b[i];
    li a[n][n];
    forn(i,n)
        forn(j,n)
            a[i][j]=1;
   // a[0][0]=1;
    forn(i,n)
    {
        forn(j,n)
        {
            if(j>i)
                a[i][j]=0;
            else if(i==j)
                a[i][j]=1;
            else if(j==0)
            {
                a[i][j]=2;
            }
            else
            {
                for(li k=0;k<j;k++)
                {
                    if(b[k]==b[i]&&1+a[j][k]>a[i][j])
                        a[i][j]=1+a[j][k];
                }
            }
        }
    }
    li maxm=1;
    forn(i,n)
    {
        forn(j,n)
        {
            if(a[i][j]>maxm)
                maxm=a[i][j];
        }
    }
    cout<<maxm;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define max1 1000000007
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
int main()
{
    iostream::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n;
    cin>>n;
    li b[n];
    forn(i,n)
        cin>>b[i];
    li a[n][n];
    forn(i,n)
        forn(j,n)
            a[i][j]=1;
   // a[0][0]=1;
    map<int,int> m;
    li k=0;
    forn(i,n)
    {
        if(m[b[i]]==0){
            m[b[i]]=k;
            k++;}
    }
    li c[n];
    forn(i,n)
        c[i]=m[b[i]];
    forn(i,n)
    {
        for(li j=0;j<i;j++)
        {
            a[i][j]=max(a[i][c[j]],1+a[j][c[i]]);

        }
    }
    li maxm=1;
    forn(i,n)
    {
        forn(j,n)
        {
            if(a[i][j]>maxm)
                maxm=a[i][j];
        }
    }
    cout<<maxm;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[4005];
map<int ,vector<int> > u; // pos
map<int , pair<bool,int> > e; // times certain number appeared in intervals
int ans ;
int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	if(n==1){
        cout <<1;
        return 0;
	}
    for(int i = 0 ; i < n ; i ++){
        cin>>a[i];
        u[ a[i] ].push_back(i);
    }
    // for each unique element iterate over it's appearances
    for(auto i : u){
        int start = i.second[0]+1;
        // for each unique element position
        for(int j = 1 ; j < i.second.size() ; j ++){
            // check mini interval
            for(int k = start ; k < i.second[j] ; k++){
                if(e[ a[k] ].first == false){
                    e[ a[k] ].first = true;
                    e[ a[k] ].second++;
                }
            }
            for(auto k : e)k.second.first = false;
            start = i.second[j]+1;
        }
        for(auto k : e)e[ k.first ].first = false;
        for(int k = start ; k < n ; k++){
            if(e[ a[k] ].first == false){
                e[ a[k] ].first = true;
                e[ a[k] ].second++;
            }
        }
        int temp = 0;
        for(auto j : e ){
            temp = max ( temp   ,  j.second.second*2 +(  !j.second.first ) );
        }
        ans = max (temp , ans);
        e.clear();
    }
    cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[4005];
map<int ,vector<int> > u; // pos
map<int , pair<bool,int> > e; // times certain number appeared in intervals
int ans ;
int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	if(n==1){
        cout <<1;
        return 0;
	}
    for(int i = 0 ; i < n ; i ++){
        cin>>a[i];
        u[ a[i] ].push_back(i);
    }
    // for each unique element iterate over it's appearances
    for(auto i : u){
        int start = i.second[0]+1;
        // for each unique element position
        for(int j = 1 ; j < i.second.size() ; j ++){
            // check mini interval
            for(int k = start ; k < i.second[j] ; k++){
                if(e[ a[k] ].first == false){
                    e[ a[k] ].first = true;
                    e[ a[k] ].second++;
                }
            }
            for(auto k : e)e[ k.first ].first = false;
            start = i.second[j]+1;
        }
        for(int k = start ; k < n ; k++){
            if(e[ a[k] ].first == false){
                e[ a[k] ].first = true;
                e[ a[k] ].second++;
            }
        }
        int temp = i.second.size();
        ans = max(temp , ans);
        for(auto j : e ){
            temp = max ( temp   ,  j.second.second*2 +(  !j.second.first ) );
        }
        ans = max (temp , ans);
        e.clear();
    }
    cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[4005];
map<int ,vector<int> > u; // pos
map<int , pair<bool,int> > e; // times certain number appeared in intervals
int ans ;
int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	if(n==1){
        cout <<1;
        return 0;
	}
    for(int i = 0 ; i < n ; i ++){
        cin>>a[i];
        u[ a[i] ].push_back(i);
    }
    // for each unique element iterate over it's appearances
    for(auto i : u){
        int start = i.second[0]+1;
        // for each unique element position
        for(int j = 1 ; j < i.second.size() ; j ++){
            // check mini interval
            for(int k = start ; k < i.second[j] ; k++){
                if(e[ a[k] ].first == false){
                    e[ a[k] ].first = true;
                    e[ a[k] ].second++;
                }
            }
            for(auto k : e)e[ k.first ].first = false;
            start = i.second[j]+1;
        }
        for(auto k : e)e[ k.first ].first = false;
        for(int k = start ; k < n ; k++){
            if(e[ a[k] ].first == false){
                e[ a[k] ].first = true;
                e[ a[k] ].second++;
            }
        }
        int temp = 0;
        for(auto j : e ){
            temp = max ( temp   ,  j.second.second*2 +(  !j.second.first ) );
        }
        ans = max (temp , ans);
        e.clear();
    }
    cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[4005];
map<int ,vector<int> > u; // pos
map<int , pair<bool,int> > e; // times certain number appeared in intervals
int ans ;
int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	for(int i = 0 ; i < n ; i ++){
        cin >> a[i];
        u [ a[i] ].push_back(i);
	}
    // for each unique element iterate over it's appearances
    for(auto i : u){
        if( i.second.size() < ans/2 )continue;
        int start = i.second[0]+1;
        // for each unique element position
        for(int j = 1 ; j < i.second.size() ; j ++){
            // check mini interval
            for(int k = start ; k < i.second[j] ; k++){
                if(e[ a[k] ].first == false){
                    e[ a[k] ].first = true;
                    e[ a[k] ].second++;
                }
            }
            for(auto k : e)e[ k.first ].first = false;
            start = i.second[j]+1;
        }
        for(int k = start ; k < n ; k++){
            if(e[ a[k] ].first == false){
                e[ a[k] ].first = true;
                e[ a[k] ].second++;
            }
        }
        int temp = i.second.size();
        for(auto j : e ){
            temp = max ( temp   ,  j.second.second*2 +(  !j.second.first ) );
        }
        ans = max (temp , ans);
        e.clear();
    }
    cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[4005];
map<int ,vector<int> > u; // pos
map<int , pair<bool,int> > e; // times certain number appeared in intervals
int ans ;
int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	if(n==1){
        cout <<1;
        return 0;
	}
    for(int i = 0 ; i < n ; i ++){
        cin>>a[i];
        u[ a[i] ].push_back(i);
    }
    // for each unique element iterate over it's appearances
    for(auto i : u){
        int start = i.second[0]+1;
        // for each unique element position
        for(int j = 1 ; j < i.second.size() ; j ++){
            // check mini interval
            for(int k = start ; k < i.second[j] ; k++){
                if(e[ a[k] ].first == false){
                    e[ a[k] ].first = true;
                    e[ a[k] ].second++;
                }
            }
            for(auto k : e)e[ k.first ].first = false;
            start = i.second[j]+1;
        }
        for(int k = start ; k < n ; k++){
            if(e[ a[k] ].first == false){
                e[ a[k] ].first = true;
                e[ a[k] ].second++;
            }
        }
        int temp = 0;
        for(auto j : e ){
            temp = max ( temp   ,  j.second.second*2 +(  !j.second.first ) );
        }
        ans = max (temp , ans);
        ans = max (ans , u.second.size());
        e.clear();
    }
    cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[4005];
map<int ,vector<int> > u; // pos
map<int , pair<bool,int> > e; // times certain number appeared in intervals
int ans ;
int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
    for(int i = 0 ; i < n ; i ++){
        cin>>a[i];
        u[ a[i] ].push_back(i);
    }
    // for each unique element iterate over it's appearances
    for(auto i : u){
        int start = i.second[0]+1;
        // for each unique element position
        for(int j = 1 ; j < i.second.size() ; j ++){
            // check mini interval
            for(int k = start ; k < i.second[j] ; k++){
                if(e[ a[k] ].first == false){
                    e[ a[k] ].first = true;
                    e[ a[k] ].second++;
                }
            }
            for(auto k : e)k.second.first = false;
            start = i.second[j]+1;
        }
        for(auto k : e)e[ k.first ].first = false;
        for(int k = start ; k < n ; k++){
            if(e[ a[k] ].first == false){
                e[ a[k] ].first = true;
                e[ a[k] ].second++;
            }
        }
        int temp = 0;
        for(auto j : e ){
            temp = max ( temp   ,  j.second.second*2 +(  !j.second.first ) );
        }
        ans = max (temp , ans);
        e.clear();
    }
    cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[4005];
map<int ,vector<int> > u; // pos
map<int , pair<bool,int> > e; // times certain number appeared in intervals
int ans ;
int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	if(n==1){
        cout <<1;
        return 0;
	}
    for(int i = 0 ; i < n ; i ++){
        cin>>a[i];
        u[ a[i] ].push_back(i);
    }
    // for each unique element iterate over it's appearances
    for(auto i : u){
        int start = i.second[0]+1;
        // for each unique element position
        for(int j = 1 ; j < i.second.size() ; j ++){
            // check mini interval
            for(int k = start ; k < i.second[j] ; k++){
                if(e[ a[k] ].first == false){
                    e[ a[k] ].first = true;
                    e[ a[k] ].second++;
                }
            }
            for(auto k : e)e[ k.first ].first = false;
            start = i.second[j]+1;
        }
        for(int k = start ; k < n ; k++){
            if(e[ a[k] ].first == false){
                e[ a[k] ].first = true;
                e[ a[k] ].second++;
            }
        }
        int temp = 0;
        for(auto j : e ){
            temp = max ( temp   ,  j.second.second*2 +(  !j.second.first ) );
        }
        ans = max (temp , ans);
        e.clear();
    }
    cout << ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[4005];
map<int ,vector<int> > u; // pos
map<int , pair<bool,int> > e; // times certain number appeared in intervals
int ans ;
int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	for(int i = 0 ; i < n ; i ++){
        cin >> a[i];
        u [ a[i] ].push_back(i);
	}
    // for each unique element iterate over it's appearances
    for(auto i : u){
        if( i.second.size() <= ans/2 )continue;
        int start = i.second[0]+1;
        // for each unique element position
        for(int j = 1 ; j < i.second.size() ; j ++){
            // check mini interval
            for(int k = start ; k < i.second[j] ; k++){
                if(e[ a[k] ].first == false){
                    e[ a[k] ].first = true;
                    e[ a[k] ].second++;
                }
            }
            for(auto k : e)e[ k.first ].first = false;
            start = i.second[j]+1;
        }
        for(int k = start ; k < n ; k++){
            if(e[ a[k] ].first == false){
                e[ a[k] ].first = true;
                e[ a[k] ].second++;
            }
        }
        int temp = i.second.size();
        for(auto j : e ){
            temp = max ( temp   ,  j.second.second*2 +(  !j.second.first ) );
        }
        ans = max (temp , ans);
        e.clear();
    }
    cout << ans;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, b[4010];
int dp[4010][4010];
int cnt[1000010];
vector<int> a;
int res;

int main() {
    ios_base::sync_with_stdio(0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> b[i];
        a.push_back(b[i]);
        cnt[b[i]]++;
        res = max(res, cnt[b[i]]);
    }
    res--;
    sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end());
    for (int i = 0; i < n; i++) {
        b[i] = lower_bound(a.begin(), a.end(), b[i]) - a.begin();
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < i; j++) {
            if (b[i] == b[j]) continue;
            dp[b[i]][b[j]] = dp[b[j]][b[i]] + 1;
            res = max(res, dp[b[i]][b[j]]);
        }
    }
    cout << res + 1 << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, b[4010];
map<int, int> dp[4010];

int main() {
    ios_base::sync_with_stdio(0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }
    int res = (n >= 2) ? 2 : 1;
    for (int i = 0; i < n; i++) {
        dp[i][0] = 1;
    }
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            dp[i][b[i] - b[j]] = 2;
        }
    }
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            int x = b[i] - b[j];
            if (dp[j].find(-x) != dp[j].end()) {
                dp[i][x] = max(dp[i][x], dp[j][-x] + 1);
                res = max(res, dp[i][x]);
            }
        }
    }
    cout << res << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int dp[4010][4010];
int n, b[4010];

int main() {
    ios_base::sync_with_stdio(0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }
    int res = 1;
    memset(dp, 1, sizeof dp);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == j) dp[i][j] = 1;
            else dp[i][j] = 2;
            res = max(res, dp[i][j]);
        }
    }
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            if (b[i - 1] - b[j] == b[i - 1] - b[i]) {
                dp[i][j] = dp[i - 1][j] + 1;
                res = max(res, dp[i][j]);
            }
        }
    }
    cout << res << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, b[4010];
map<int, int> dp[4010];

int main() {
    ios_base::sync_with_stdio(0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }
    int res = (n >= 2) ? 2 : 1;
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            dp[i][b[i] - b[j]] = 2;
        }
        dp[i][0] = 1;
    }
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            int x = b[i] - b[j];
            if (dp[j].find(-x) != dp[j].end()) {
                dp[i][x] = max(dp[i][x], dp[j][-x] + 1);
                res = max(res, dp[i][x]);
            }
        }
    }
    cout << res << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, b[4010];
map<int, int> dp[4010];

int main() {
    ios_base::sync_with_stdio(0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }
    int res = (n >= 2) ? 2 : 1;
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            dp[i][b[i] - b[j]] = 2;
        }
        dp[i][0] = 1;
    }
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            int x = b[j] - b[i];
            if (dp[j].find(x) != dp[j].end()) {
                dp[i][x] = max(dp[i][x], dp[j][x] + 1);
                res = max(res, dp[i][x]);
            }
        }
    }
    cout << res << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, b[4010];
unordered_map<int, int> dp[4010];

int main() {
    ios_base::sync_with_stdio(0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }
    int res = (n >= 2) ? 2 : 1;
    for (int i = 0; i < n; i++) {
        dp[i][0] = 1;
    }
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            dp[i][b[i] - b[j]] = 2;
        }
    }
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            int x = b[i] - b[j];
            if (dp[j].find(-x) != dp[j].end()) {
                dp[i][x] = max(dp[i][x], dp[j][-x] + 1);
                res = max(res, dp[i][x]);
            }
        }
    }
    cout << res << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, b[4010];
int dp[4010][4010];
int cnt[1000010];
vector<int> a;
int res;

int main() {
    ios_base::sync_with_stdio(0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> b[i];
        a.push_back(b[i]);
        cnt[b[i]]++;
        res = max(res, cnt[b[i]]);
    }
    sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end());
    for (int i = 0; i < n; i++) {
        b[i] = lower_bound(a.begin(), a.end(), b[i]) - a.begin();
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < i; j++) {
            if (b[i] == b[i]) continue;
            dp[b[i]][b[j]] = dp[b[j]][b[i]] + 1;
            res = max(res, dp[b[i]][b[j]]);
        }
    }
    cout << res << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int dp[4010][4010];
int n, b[4010];

int main() {
    ios_base::sync_with_stdio(0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }
    memset(dp, 1, sizeof dp);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            dp[i][j] = 2;
        }
        dp[i][i] = 1;
    }
    int res = 1;
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            if (b[i - 1] - b[j] == b[i - 1] - b[i]) {
                dp[i][j] = dp[i - 1][j] + 1;
                res = max(res, dp[i][j]);
            }
        }
    }
    cout << res << "\n";
    return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int check[1000010];
int gap[1000010];
int check1[1000010];
vector<int> vt;
int a[4001];
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
		vt.push_back(a[i]);
	}
	sort(vt.begin(), vt.end());
	vt.erase(unique(vt.begin(), vt.end()), vt.end());
	int dap = 0;
	for (int i = 0; i < vt.size(); i++)
	{
		gap[vt[i]] = -1;
		int xx = 0;
		for (int y = 0; y < n; y++)
		{
			if (vt[i] == a[y]) {
				check[vt[i]] = vt[i];
				gap[vt[i]] = y;
			}
			else
			{
				if (check[a[y]] != vt[i])
				{
					check[a[y]] = vt[i];
					if (gap[vt[i]] == -1)
						gap[a[y]] = 1;
					else
						gap[a[y]] = 2;
					xx = max(gap[a[y]],xx);
					check1[a[y]] = gap[vt[i]];
				}
				else
				{
					if (gap[vt[i]] != check1[a[y]])
					{
						gap[a[y]] += 2;
						xx = max(gap[a[y]], xx);
						check1[a[y]] = gap[vt[i]];
					}
				}
			}
		}
		for (int y = 0; y < vt.size(); y++)
		{
			if (gap[vt[i]] > check1[vt[y]])
			{
				gap[vt[y]]++;
				xx = max(gap[vt[y]],xx);
			}
		}
		dap = max(xx, dap);
	}
	printf("%d", dap);
}


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int check[1000010];
int gap[1000010];
int check1[1000010];
vector<int> vt;
int a[4001];
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
		vt.push_back(a[i]);
	}
	sort(vt.begin(), vt.end());
	vt.erase(unique(vt.begin(), vt.end()), vt.end());
	int dap = 1;
	for (int i = 0; i < vt.size(); i++)
	{
		gap[vt[i]] = -1;
		int xx = 0;
		for (int y = 0; y < n; y++)
		{
			if (vt[i] == a[y]) {
				gap[vt[i]] = y;
				xx++;
			}
			else
			{
				if (check[a[y]] != vt[i])
				{
					check[a[y]] = vt[i];
					if (gap[vt[i]] == -1)
						gap[a[y]] = 1;
					else
						gap[a[y]] = 2;
					xx = max(gap[a[y]],xx);
					check1[a[y]] = gap[vt[i]];
					dap = max(dap, gap[a[y]]);
				}
				else
				{
					if (gap[vt[i]] != check1[a[y]])
					{
						gap[a[y]] += 2;
						xx = max(gap[a[y]], xx);
						check1[a[y]] = gap[vt[i]];
					}
				}
			}
		}
		dap = max(xx, dap);
	}
	printf("%d", dap);
}


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int check[1000010];
int gap[1000010];
int check1[1000010];
vector<int> vt;
int a[4001];
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
		vt.push_back(a[i]);
	}
	sort(vt.begin(), vt.end());
	vt.erase(unique(vt.begin(), vt.end()), vt.end());
	int dap = 1;
	for (int i = 0; i < vt.size(); i++)
	{
		check1[vt[i]] = -1;
		int xx = 0;
		for (int y = 0; y < n; y++)
		{
			if (vt[i] == a[y]) {
				check[vt[i]] = vt[i];
				check1[vt[i]] = y;
			}
			else
			{
				if (check[a[y]] != vt[i])
				{
					check[a[y]] = vt[i];
					if (check1[vt[i]] == -1)
						check1[a[y]] = 1;
					else
						gap[a[y]] = 2;
					xx = max(gap[a[y]],xx);
					check1[a[y]] = check1[vt[i]];
				}
				else
				{
					if (check1[vt[i]] != check1[a[y]])
					{
						gap[a[y]] += 2;
						xx = max(gap[a[y]], xx);
						check1[a[y]] = check1[vt[i]];
					}
				}
			}
		}
		for (int y = 0; y < vt.size(); y++)
		{
			if (check1[vt[i]] !=check1[vt[y]])
			{
				gap[vt[y]]++;
				xx = max(gap[vt[y]],xx);
			}
		}
		dap = max(xx, dap);
	}
	printf("%d", dap);
}


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int check[1000010];
int gap[1000010];
int check1[1000010];
vector<int> vt;
int a[4001];
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
		vt.push_back(a[i]);
	}
	sort(vt.begin(), vt.end());
	vt.erase(unique(vt.begin(), vt.end()), vt.end());
	int dap = 1;
	for (int i = 0; i < vt.size(); i++)
	{
		gap[vt[i]] = -1;
		int xx = 0;
		for (int y = 0; y < n; y++)
		{
			if (vt[i] == a[y]) {
				check[vt[i]] = vt[i];
				check1[vt[i]] = y;
			}
			else
			{
				if (check[a[y]] != vt[i])
				{
					check[a[y]] = vt[i];
					if (gap[vt[i]] == -1)
						gap[a[y]] = 1;
					else
						gap[a[y]] = 2;
					xx = max(gap[a[y]],xx);
					check1[a[y]] = check1[vt[i]];
				}
				else
				{
					if (check1[vt[i]] != check1[a[y]])
					{
						gap[a[y]] += 2;
						xx = max(gap[a[y]], xx);
						check1[a[y]] = check1[vt[i]];
					}
				}
			}
		}
		for (int y = 0; y < vt.size(); y++)
		{
			if (check1[vt[i]] > check1[vt[y]])
			{
				gap[vt[y]]++;
				xx = max(gap[vt[y]],xx);
			}
		}
		dap = max(xx, dap);
	}
	printf("%d", dap);
}


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int check[1000010];
int gap[1000010];
int check1[1000010];
vector<int> vt;
int a[4001];
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
		vt.push_back(a[i]);
	}
	sort(vt.begin(), vt.end());
	vt.erase(unique(vt.begin(), vt.end()), vt.end());
	int dap = 0;
	for (int i = 0; i < vt.size(); i++)
	{
		gap[vt[i]] = -1;
		int xx = 0;
		for (int y = 0; y < n; y++)
		{
			if (vt[i] == a[y]) {
				check[vt[i]] = vt[i];
				check1[vt[i]] = y;
			}
			else
			{
				if (check[a[y]] != vt[i])
				{
					check[a[y]] = vt[i];
					if (gap[vt[i]] == -1)
						gap[a[y]] = 1;
					else
						gap[a[y]] = 2;
					xx = max(gap[a[y]],xx);
					check1[a[y]] = check1[vt[i]];
				}
				else
				{
					if (check1[vt[i]] != check1[a[y]])
					{
						gap[a[y]] += 2;
						xx = max(gap[a[y]], xx);
						check1[a[y]] = check1[vt[i]];
					}
				}
			}
		}
		for (int y = 0; y < vt.size(); y++)
		{
			if (check1[vt[i]] > check1[vt[y]])
			{
				gap[vt[y]]++;
				xx = max(gap[vt[y]],xx);
			}
		}
		dap = max(xx, dap);
	}
	printf("%d", dap);
}


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int check[1000010];
int gap[1000010];
int check1[1000010];
vector<int> vt;
int a[4001];
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
		vt.push_back(a[i]);
	}
	sort(vt.begin(), vt.end());
	vt.erase(unique(vt.begin(), vt.end()), vt.end());
	int dap = 1;
	for (int i = 0; i < vt.size(); i++)
	{
		check1[vt[i]] = -1;
		for (int y = 0; y < n; y++)
		{
			if (vt[i] == a[y]) {
				check[vt[i]] = vt[i];
				check1[vt[i]] = y;
			}
			else
			{
				if (check[a[y]] != vt[i])
				{
					check[a[y]] = vt[i];
					if (check1[vt[i]] == -1)
						gap[a[y]] = 1;
					else
						gap[a[y]] = 2;
					dap = max(dap, gap[a[y]]);
					check1[a[y]] = check1[vt[i]];
				}
				else
				{
					if (check1[vt[i]] != check1[a[y]])
					{
						gap[a[y]] += 2;
						dap = max(dap, gap[a[y]]);
						check1[a[y]] = check1[vt[i]];
					}
				}
			}
		}
		for (int y = 0; y < vt.size(); y++)
		{
			if (check1[vt[i]] !=check1[vt[y]])
			{
				gap[vt[y]]++;
				dap=max(dap,gap[vt[y]]);
			}
		}
	}
	printf("%d", dap);
}


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int check[1000010];
int gap[1000010];
int check1[1000010];
vector<int> vt;
int a[4001];
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
		vt.push_back(a[i]);
	}
	sort(vt.begin(), vt.end());
	vt.erase(unique(vt.begin(), vt.end()), vt.end());
	int dap = 0;
	for (int i = 0; i < vt.size(); i++)
	{
		gap[vt[i]] = -1;
		int xx = 0;
		for (int y = 0; y < n; y++)
		{
			if (vt[i] == a[y]) {
				check[vt[i]] = vt[i];
				gap[vt[i]] = y;
				xx++;
			}
			else
			{
				if (check[a[y]] != vt[i])
				{
					check[a[y]] = vt[i];
					if (gap[vt[i]] == -1)
						gap[a[y]] = 1;
					else
						gap[a[y]] = 2;
					xx = max(gap[a[y]],xx);
					check1[a[y]] = gap[vt[i]];
					dap = max(dap, gap[a[y]]);
				}
				else
				{
					if (gap[vt[i]] != check1[a[y]])
					{
						gap[a[y]] += 2;
						xx = max(gap[a[y]], xx);
						check1[a[y]] = gap[vt[i]];
					}
				}
			}
		}
		dap = max(xx, dap);
	}
	printf("%d", dap);
}


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int check[1000010];
int gap[1000010];
int check1[1000010];
vector<int> vt;
int a[4001];
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
		vt.push_back(a[i]);
	}
	sort(vt.begin(), vt.end());
	vt.erase(unique(vt.begin(), vt.end()), vt.end());
	int dap = 0;
	for (int i = 0; i < vt.size(); i++)
	{
		gap[vt[i]] = -1;
		for (int y = 0; y < n; y++)
		{
			if (vt[i] == a[y]) {
				gap[vt[i]] = y;
			}
			else
			{
				if (check[a[y]] != vt[i])
				{
					check[a[y]] = vt[i];
					if (gap[vt[i]] == -1)
						gap[a[y]] = 1;
					else
						gap[a[y]] = 2;
					check1[a[y]] = gap[vt[i]];
					dap = max(dap, gap[a[y]]);
				}
				else
				{
					if (gap[vt[i]] != check1[a[y]])
					{
						gap[a[y]] += 2;
						dap = max(dap, gap[a[y]]);
						check1[a[y]] = gap[vt[i]];
					}
				}
			}
		}
	}
	printf("%d", dap);
}


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int check[1000010];
int gap[1000010];
int check1[1000010];
int xx[1000100];
vector<int> vt;
int a[4001];
int main()
{
	int n;
	scanf("%d", &n);
	int dap = 0;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
		xx[a[i]]++;
		dap = max(dap, xx[a[i]]);
		vt.push_back(a[i]);
	}
	sort(vt.begin(), vt.end());
	vt.erase(unique(vt.begin(), vt.end()), vt.end());
	for (int i = 0; i < vt.size(); i++)
	{
		check1[vt[i]] = -1;
		for (int y = 0; y < n; y++)
		{
			if (vt[i] == a[y]) {
				check[vt[i]] = vt[i];
				check1[vt[i]] = y;
			}
			else
			{
				if (check[a[y]] != vt[i])
				{
					check[a[y]] = vt[i];
					if (check1[vt[i]] == -1)
						gap[a[y]] = 1;
					else
						gap[a[y]] = 2;
					dap = max(dap, gap[a[y]]);
					check1[a[y]] = check1[vt[i]];
				}
				else
				{
					if (check1[vt[i]] != check1[a[y]])
					{
						gap[a[y]] += 2;
						dap = max(dap, gap[a[y]]);
						check1[a[y]] = check1[vt[i]];
					}
				}
			}
		}
		for (int y = 0; y < vt.size(); y++)
		{
			if (check1[vt[i]] !=check1[vt[y]])
			{
				gap[vt[y]]++;
				dap=max(dap,gap[vt[y]]);
			}
		}
	}
	printf("%d", dap);
}


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int check[1000010];
int gap[1000010];
int check1[1000010];
vector<int> vt;
int a[4001];
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
		vt.push_back(a[i]);
	}
	sort(vt.begin(), vt.end());
	vt.erase(unique(vt.begin(), vt.end()), vt.end());
	int dap = 0;
	for (int i = 0; i < vt.size(); i++)
	{
		gap[vt[i]] = -1;
		int xx = 0;
		for (int y = 0; y < n; y++)
		{
			if (vt[i] == a[y]) {
				check[vt[i]] = vt[i];
				gap[vt[i]] = y;
			}
			else
			{
				if (check[a[y]] != vt[i])
				{
					check[a[y]] = vt[i];
					if (gap[vt[i]] == -1)
						gap[a[y]] = 1;
					else
						gap[a[y]] = 2;
					xx = max(gap[a[y]],xx);
					check1[a[y]] = gap[vt[i]];
				}
				else
				{
					if (gap[vt[i]] != check1[a[y]])
					{
						gap[a[y]] += 2;
						xx = max(gap[a[y]], xx);
						check1[a[y]] = gap[vt[i]];
					}
				}
			}
		}
		for (int y = 0; y < vt.size(); y++)
		{
			if (gap[vt[i]] > check1[vt[y]])
			{
				gap[vt[y]]++;
				xx = max(gap[vt[y]]);
			}
		}
		dap = max(xx, dap);
	}
	printf("%d", dap);
}


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int check[1000010];
int gap[1000010];
int check1[1000010];
vector<int> vt;
int a[4001];
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
		vt.push_back(a[i]);
	}
	sort(vt.begin(), vt.end());
	vt.erase(unique(vt.begin(), vt.end()), vt.end());
	int dap = 1;
	for (int i = 0; i < vt.size(); i++)
	{
		gap[vt[i]] = -1;
		for (int y = 0; y < n; y++)
		{
			if (vt[i] == a[y]) {
				gap[vt[i]] = y;
			}
			else
			{
				if (check[a[y]] != vt[i])
				{
					check[a[y]] = vt[i];
					if (gap[vt[i]] == -1)
						gap[a[y]] = 1;
					else
						gap[a[y]] = 2;
					check1[a[y]] = gap[vt[i]];
					dap = max(dap, gap[a[y]]);
				}
				else
				{
					if (gap[vt[i]] != check1[a[y]])
					{
						gap[a[y]] += 2;
						dap = max(dap, gap[a[y]]);
						check1[a[y]] = gap[vt[i]];
					}
				}
			}
		}
	}
	printf("%d", dap);
}


#include <bits/stdc++.h>

// limit
#define mod1 22439423LL
#define mod2 42342432LL
#define mod3 56454765LL
#define mod4 66867574LL
#define oo 1000000007LL
#define OO 1000000000000000007LL
#define maxN 5007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
// structure
#define ii pair<int, int>
#define iii pair<ii, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
#define matrix vector<vi>

// get value
#define FF first
#define SS second

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T min(T a, T b, T c, T d) {
    return min(a, min(b, min(c, d)));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
T max(T a, T b, T c, T d) {
    return max(a, max(b, max(c, d)));
}

bool cmp(const ii& a, const ii& b) {return (a.FF > b.FF || (a.FF == b.FF && a.SS >= b.SS));}
ll GCD(ll a, ll b) {return (a%b) ? GCD(b, a%b) : b;}

const string namePro = "tmp";

map <int, int> f[maxN];
int a[maxN];

int main() {
    #ifndef ONLINE_JUDGE
        freopen((namePro+".inp").c_str(), "r", stdin);
        freopen((namePro+".out").c_str(), "w", stdout);
    #endif // ONLINE_JUDGE

    int n;
    scanf("%d", &n);

    fto (i, 1, n) scanf("%d", &a[i]);

    int ans = 1;
    fto (i, 1, n) {
        fto (j, 1, i-1) {
            f[j][a[j]-a[i]] = max(f[j][a[j]-a[i]], 1);
            f[i][a[i]-a[j]] = f[j][a[j]-a[i]]+1;
            ans = max(ans, f[i][a[i]-a[j]]);
        }
    }

    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

// limit
#define mod1 22439423LL
#define mod2 42342432LL
#define mod3 56454765LL
#define mod4 66867574LL
#define oo 1000000007LL
#define OO 1000000000000000007LL
#define maxN 5007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
// structure
#define ii pair<int, int>
#define iii pair<ii, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
#define matrix vector<vi>

// get value
#define FF first
#define SS second

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T min(T a, T b, T c, T d) {
    return min(a, min(b, min(c, d)));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
T max(T a, T b, T c, T d) {
    return max(a, max(b, max(c, d)));
}

bool cmp(const ii& a, const ii& b) {return (a.FF > b.FF || (a.FF == b.FF && a.SS >= b.SS));}
ll GCD(ll a, ll b) {return (a%b) ? GCD(b, a%b) : b;}

const string namePro = "tmp";

map <int, int> f[maxN];
int a[maxN];
ii b[maxN];
int trace[1000007];

int main() {
    #ifndef ONLINE_JUDGE
        freopen((namePro+".inp").c_str(), "r", stdin);
        freopen((namePro+".out").c_str(), "w", stdout);
    #endif // ONLINE_JUDGE

    int n;
    scanf("%d", &n);

    fto (i, 1, n) scanf("%d", &a[i]);
    fto (i, 1, n) b[i] = mp(a[i], i);

    sort(b+1, b+n+1);
    b[0].FF = -oo;

    fto (i, 1, n) {
        if (b[i].FF != b[i-1].FF) trace[b[i].FF] = b[i].SS;
    }

    int ans = 0;
    fto (i, 1, n) {
        bool check = 0;
        fto (j, 1, i-1) {
            if (a[i] == a[j] && check) continue;
            if (a[i] == a[j]) check = 1;
            f[trace[a[i]]][trace[a[j]]] = f[trace[a[j]]][trace[a[i]]]+1;
            ans = max(ans, f[trace[a[i]]][trace[a[j]]]);
        }
    }

    cout << ans+1 << endl;
    return 0;
}


#include <bits/stdc++.h>

// limit
#define mod1 22439423LL
#define mod2 42342432LL
#define mod3 56454765LL
#define mod4 66867574LL
#define oo 1000000007LL
#define OO 1000000000000000007LL
#define maxN 5007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
// structure
#define ii pair<int, int>
#define iii pair<ii, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
#define matrix vector<vi>

// get value
#define FF first
#define SS second

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T min(T a, T b, T c, T d) {
    return min(a, min(b, min(c, d)));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
T max(T a, T b, T c, T d) {
    return max(a, max(b, max(c, d)));
}

bool cmp(const ii& a, const ii& b) {return (a.FF > b.FF || (a.FF == b.FF && a.SS >= b.SS));}
ll GCD(ll a, ll b) {return (a%b) ? GCD(b, a%b) : b;}

const string namePro = "tmp";

map <int, int> f[maxN];
int a[maxN];
ii b[maxN];
int trace[1000007];

int main() {
    #ifndef ONLINE_JUDGE
        freopen((namePro+".inp").c_str(), "r", stdin);
        freopen((namePro+".out").c_str(), "w", stdout);
    #endif // ONLINE_JUDGE

    int n;
    scanf("%d", &n);

    fto (i, 1, n) scanf("%d", &a[i]);
    fto (i, 1, n) b[i] = mp(a[i], i);

    sort(b+1, b+n+1);
    b[0].FF = -oo;

    fto (i, 1, n) {
        if (b[i].FF != b[i-1].FF) trace[b[i].FF] = b[i].SS;
    }

    int ans = 0;
    set <int> st;

    fto (i, 1, n) {
        ftoit (it, st) {
            if (*it == a[i]) continue;
            f[trace[a[i]]][trace[*it]] = f[trace[*it]][trace[a[i]]]+1;
            ans = max(ans, f[trace[a[i]]][trace[*it]]);
        }
        st.insert(a[i]);
    }

    cout << ans+1 << endl;
    return 0;
}


#include <bits/stdc++.h>

// limit
#define mod1 22439423LL
#define mod2 42342432LL
#define mod3 56454765LL
#define mod4 66867574LL
#define oo 1000000007LL
#define OO 1000000000000000007LL
#define maxN 5007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
// structure
#define ii pair<int, int>
#define iii pair<ii, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
#define matrix vector<vi>

// get value
#define FF first
#define SS second

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T min(T a, T b, T c, T d) {
    return min(a, min(b, min(c, d)));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
T max(T a, T b, T c, T d) {
    return max(a, max(b, max(c, d)));
}

bool cmp(const ii& a, const ii& b) {return (a.FF > b.FF || (a.FF == b.FF && a.SS >= b.SS));}
ll GCD(ll a, ll b) {return (a%b) ? GCD(b, a%b) : b;}

const string namePro = "tmp";

map <int, int> f[maxN];
int a[maxN];
ii b[maxN];
int trace[1000007];

int main() {
    #ifndef ONLINE_JUDGE
        freopen((namePro+".inp").c_str(), "r", stdin);
        freopen((namePro+".out").c_str(), "w", stdout);
    #endif // ONLINE_JUDGE

    int n;
    scanf("%d", &n);

    fto (i, 1, n) scanf("%d", &a[i]);
    fto (i, 1, n) b[i] = mp(a[i], i);

    sort(b+1, b+n+1);
    b[0].FF = -oo;

    fto (i, 1, n) {
        if (b[i].FF != b[i-1].FF) trace[b[i].FF] = b[i].SS;
    }

    int ans = 0;
    fto (i, 1, n) {
        fto (j, 1, i-1) {
            f[trace[a[i]]][trace[a[j]]] = f[trace[a[j]]][trace[a[i]]]+1;
            ans = max(ans, f[trace[a[i]]][trace[a[j]]]);
        }
    }

    cout << ans+1 << endl;
    return 0;
}


#include <bits/stdc++.h>

// limit
#define mod1 22439423LL
#define mod2 42342432LL
#define mod3 56454765LL
#define mod4 66867574LL
#define oo 1000000007LL
#define OO 1000000000000000007LL
#define maxN 5007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
// structure
#define ii pair<int, int>
#define iii pair<ii, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
#define matrix vector<vi>

// get value
#define FF first
#define SS second

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T min(T a, T b, T c, T d) {
    return min(a, min(b, min(c, d)));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
T max(T a, T b, T c, T d) {
    return max(a, max(b, max(c, d)));
}

bool cmp(const ii& a, const ii& b) {return (a.FF > b.FF || (a.FF == b.FF && a.SS >= b.SS));}
ll GCD(ll a, ll b) {return (a%b) ? GCD(b, a%b) : b;}

const string namePro = "tmp";

map <int, int> f[maxN];
int a[maxN];
ii b[maxN];
int trace[1000007];

int main() {
    #ifndef ONLINE_JUDGE
        freopen((namePro+".inp").c_str(), "r", stdin);
        freopen((namePro+".out").c_str(), "w", stdout);
    #endif // ONLINE_JUDGE

    int n;
    scanf("%d", &n);

    fto (i, 1, n) scanf("%d", &a[i]);
    fto (i, 1, n) b[i] = mp(a[i], i);

    sort(b+1, b+n+1);
    b[0].FF = -oo;

    fto (i, 1, n) {
        if (b[i].FF != b[i-1].FF) trace[b[i].FF] = b[i].SS;
    }

    int ans = 0;
    set <int> st;

    fto (i, 1, n) {
        ftoit (it, st) {
            //if (*it == a[i]) continue;
            f[trace[a[i]]][trace[*it]] = f[trace[*it]][trace[a[i]]]+1;
            ans = max(ans, f[trace[a[i]]][trace[*it]]);
        }
        st.insert(a[i]);
    }

    cout << ans+1 << endl;
    return 0;
}


#include <bits/stdc++.h>

// limit
#define mod1 22439423LL
#define mod2 42342432LL
#define mod3 56454765LL
#define mod4 66867574LL
#define oo 1000000007LL
#define OO 1000000000000000007LL
#define maxN 5007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
// structure
#define ii pair<int, int>
#define iii pair<ii, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
#define matrix vector<vi>

// get value
#define FF first
#define SS second

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T min(T a, T b, T c, T d) {
    return min(a, min(b, min(c, d)));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
T max(T a, T b, T c, T d) {
    return max(a, max(b, max(c, d)));
}

bool cmp(const ii& a, const ii& b) {return (a.FF > b.FF || (a.FF == b.FF && a.SS >= b.SS));}
ll GCD(ll a, ll b) {return (a%b) ? GCD(b, a%b) : b;}

const string namePro = "tmp";

int f[maxN][maxN];
int a[maxN];
ii b[maxN];
int trace[1000007];

int main() {
    #ifndef ONLINE_JUDGE
        freopen((namePro+".inp").c_str(), "r", stdin);
        freopen((namePro+".out").c_str(), "w", stdout);
    #endif // ONLINE_JUDGE

    int n;
    scanf("%d", &n);

    fto (i, 1, n) scanf("%d", &a[i]);
    fto (i, 1, n) b[i] = mp(a[i], i);

    sort(b+1, b+n+1);
    b[0].FF = -oo;

    fto (i, 1, n) {
        if (b[i].FF != b[i-1].FF) trace[b[i].FF] = b[i].SS;
    }

    int ans = 0;
    fto (i, 1, n) {
        bool check = 0;
        fto (j, 1, i-1) {
            if (a[i] == a[j] && check) continue;
            if (a[i] == a[j]) check = 1;
            f[trace[a[i]]][trace[a[j]]] = f[trace[a[j]]][trace[a[i]]]+1;
            ans = max(ans, f[trace[a[i]]][trace[a[j]]]);
        }
    }

    cout << ans+1 << endl;
    return 0;
}


#include <bits/stdc++.h>

// limit
#define mod1 22439423LL
#define mod2 42342432LL
#define mod3 56454765LL
#define mod4 66867574LL
#define oo 1000000007LL
#define OO 1000000000000000007LL
#define maxN 5007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
// structure
#define ii pair<int, int>
#define iii pair<ii, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
#define matrix vector<vi>

// get value
#define FF first
#define SS second

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T min(T a, T b, T c, T d) {
    return min(a, min(b, min(c, d)));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
T max(T a, T b, T c, T d) {
    return max(a, max(b, max(c, d)));
}

bool cmp(const ii& a, const ii& b) {return (a.FF > b.FF || (a.FF == b.FF && a.SS >= b.SS));}
ll GCD(ll a, ll b) {return (a%b) ? GCD(b, a%b) : b;}

const string namePro = "tmp";

map <int, int> f[maxN];
int a[maxN];

int main() {
    #ifndef ONLINE_JUDGE
        freopen((namePro+".inp").c_str(), "r", stdin);
        freopen((namePro+".out").c_str(), "w", stdout);
    #endif // ONLINE_JUDGE

    int n;
    scanf("%d", &n);

    fto (i, 1, n) scanf("%d", &a[i]);

    int ans = 1;
    fto (i, 1, n) {
        fto (j, 1, i-1) {
            f[j][a[i]-a[j]] = max(f[j][a[i]-a[j]], 1);
            f[i][a[i]-a[j]] = f[j][a[i]-a[j]]+1;
            ans = max(ans, f[i][a[i]-a[j]]);
        }
    }

    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

// limit
#define mod1 22439423LL
#define mod2 42342432LL
#define mod3 56454765LL
#define mod4 66867574LL
#define oo 1000000007LL
#define OO 1000000000000000007LL
#define maxN 5007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
// structure
#define ii pair<int, int>
#define iii pair<ii, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
#define matrix vector<vi>

// get value
#define FF first
#define SS second

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T min(T a, T b, T c, T d) {
    return min(a, min(b, min(c, d)));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
T max(T a, T b, T c, T d) {
    return max(a, max(b, max(c, d)));
}

bool cmp(const ii& a, const ii& b) {return (a.FF > b.FF || (a.FF == b.FF && a.SS >= b.SS));}
ll GCD(ll a, ll b) {return (a%b) ? GCD(b, a%b) : b;}

const string namePro = "tmp";

map <int, int> f[maxN];
int a[maxN];
ii b[maxN];
int trace[1000007];

int main() {
    #ifndef ONLINE_JUDGE
        freopen((namePro+".inp").c_str(), "r", stdin);
        freopen((namePro+".out").c_str(), "w", stdout);
    #endif // ONLINE_JUDGE

    int n;
    scanf("%d", &n);

    fto (i, 1, n) scanf("%d", &a[i]);
    fto (i, 1, n) b[i] = mp(a[i], i);

    sort(b+1, b+n+1);
    b[0].FF = -oo;

    fto (i, 1, n) {
        if (b[i].FF != b[i-1].FF) trace[b[i].FF] = b[i].SS;
    }

    int ans = 0;
    fto (i, 1, n) {
        fto (j, 1, i-1) {
            if (a[i] == a[j]) continue;
            f[trace[a[i]]][trace[a[j]]] = f[trace[a[j]]][trace[a[i]]]+1;
            ans = max(ans, f[trace[a[i]]][trace[a[j]]]);
        }
    }

    cout << ans+1 << endl;
    return 0;
}


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

const int MAX = 4e3 + 5;

int n;
int dp[MAX][MAX];
int a[MAX];
map<int, int> mp;
int comp[MAX * 100];

void pre() {
    memset(dp, -1, sizeof dp);
}

int main() {
    //pre();
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        mp[a[i]] = 1;
    }
    map<int, int>::iterator it;
    int co = 0;
    for(it = mp.begin(); it != mp.end(); it++) {
        comp[(*it).first] = ++co;
    }
    for(int i = 0; i < n; i++) {
        a[i] = comp[a[i]];
    }
    int ans = 0;
    for(int i = 0; i < n; i++) {
        for(int j = i - 1; j >= 0; j--) {
            dp[i][a[j]] = max(dp[j][a[i]], dp[j][0] + 1) + 1;
            ans = max(ans, dp[i][a[j]]);
        }
    }
    printf("%d", ans);
}


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

const int MAX = 4e3 + 5;

int n;
int dp[MAX][MAX];
int a[MAX];
map<int, int> mp;
int comp[MAX * 100];

void pre() {
    memset(dp, -1, sizeof dp);
}

int main() {
    //pre();
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        mp[a[i]] = 1;
    }
    map<int, int>::iterator it;
    int co = 0;
    for(it = mp.begin(); it != mp.end(); it++) {
        comp[(*it).first] = ++co;
    }
    for(int i = 0; i < n; i++) {
        a[i] = comp[a[i]];
    }
    int ans = 1;
    for(int i = 0; i < n; i++) {
        for(int j = i - 1; j >= 0; j--) {
            dp[i][a[j]] = max(dp[j][a[i]], dp[j][0] + 1) + 1;
            ans = max(ans, dp[i][a[j]]);
        }
    }
    printf("%d", ans);
}


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

const int MAX = 4e3 + 5;

int n;
int dp[MAX][MAX];
int a[MAX];
map<int, int> mp;
int comp[MAX * 100];

void pre() {
    memset(dp, -1, sizeof dp);
}

int main() {
    //pre();
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        mp[a[i]] = 1;
    }
    map<int, int>::iterator it;
    int co = 0;
    for(it = mp.begin(); it != mp.end(); it++) {
        comp[(*it).first] = ++co;
    }
    for(int i = 0; i < n; i++) {
        a[i] = comp[a[i]];
    }
    int ans = 0;
    for(int i = 0; i < n; i++) {
        for(int j = i - 1; j >= 0; j--) {
            dp[i][a[j]] = max(dp[j][a[i]], dp[j][0] + 1) + 1;
            ans = max(ans, dp[i][a[j]]);
        }
    }
    printf("%d", ans + 1);
}


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

const int MAX = 4e3 + 5;

int n;
int dp[MAX][MAX];
int a[MAX];
map<int, int> mp;
int comp[MAX * 1000];

int main() {
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        dp[i][0] = 1;
        mp[a[i]] = 1;
    }
    map<int, int>::iterator it;
    int co = 0;
    for(it = mp.begin(); it != mp.end(); it++) {
        comp[(*it).first] = ++co;
    }
    for(int i = 0; i < n; i++) {
        a[i] = comp[a[i]];
    }
    int ans = 1;
    for(int i = 0; i < n; i++) {
        for(int j = i - 1; j >= 0; j--) {
            dp[i][a[j]] = max(dp[j][a[i]], dp[j][0]) + 1;
            ans = max(ans, dp[i][a[j]]);
        }
    }
    printf("%d", ans);
}


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

const int MAX = 4e3 + 5;

int n;
int dp[MAX][MAX];
int a[MAX];
map<int, int> mp;
int comp[MAX * 1000];

int main() {
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        dp[i][0] = 1;
        mp[a[i]] = 1;
    }
    map<int, int>::iterator it;
    int co = 0;
    for(it = mp.begin(); it != mp.end(); it++) {
        comp[(*it).first] = ++co;
    }
    for(int i = 0; i < n; i++) {
        a[i] = comp[a[i]];
    }
    int ans = 1;
    for(int i = 0; i < n; i++) {
        for(int j = i - 1; j >= 0; j--) {
            dp[i][a[j]] = max(dp[i][a[j]], max(dp[j][a[i]], dp[j][0]) + 1);
            ans = max(ans, dp[i][a[j]]);
        }
    }
    printf("%d", ans);
}


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

const int MAX = 4e3 + 5;

int n;
int dp[MAX][MAX];
int a[MAX];
map<int, int> mp;
int comp[MAX * 100];

void pre() {
    memset(dp, -1, sizeof dp);
}

int main() {
    //pre();
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        dp[i][0] = 1;
        mp[a[i]] = 1;
    }
    map<int, int>::iterator it;
    int co = 0;
    for(it = mp.begin(); it != mp.end(); it++) {
        comp[(*it).first] = ++co;
    }
    for(int i = 0; i < n; i++) {
        a[i] = comp[a[i]];
    }
    int ans = 0;
    for(int i = 0; i < n; i++) {
        for(int j = i - 1; j >= 0; j--) {
            dp[i][a[j]] = max(dp[j][a[i]], dp[j][0]) + 1;
            ans = max(ans, dp[i][a[j]]);
        }
    }
    printf("%d", ans);
}


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

const int MAX = 4e3 + 5;

int n;
int dp[MAX][MAX];
int a[MAX];
map<int, int> mp;
int comp[MAX * 1000];

void pre() {
    memset(dp, -1, sizeof dp);
}

int main() {
    //pre();
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        mp[a[i]] = 1;
    }
    map<int, int>::iterator it;
    int co = 0;
    for(it = mp.begin(); it != mp.end(); it++) {
        comp[(*it).first] = ++co;
    }
    for(int i = 0; i < n; i++) {
        a[i] = comp[a[i]];
    }
    int ans = 1;
    for(int i = 0; i < n; i++) {
        for(int j = i - 1; j >= 0; j--) {
            dp[i][a[j]] = max(dp[j][a[i]], dp[j][0] + 1) + 1;
            ans = max(ans, dp[i][a[j]]);
        }
    }
    printf("%d", ans);
}


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

const int MAX = 4e3 + 5;

int n;
int dp[MAX][MAX];
int a[MAX];
map<int, int> mp;
int comp[MAX * 100];

void pre() {
    memset(dp, -1, sizeof dp);
}

int main() {
    //pre();
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        mp[a[i]] = 1;
    }
    map<int, int>::iterator it;
    int co = 0;
    for(it = mp.begin(); it != mp.end(); it++) {
        comp[(*it).first] = ++co;
    }
    for(int i = 0; i < n; i++) {
        a[i] = comp[a[i]];
    }
    int ans = 0;
    for(int i = 0; i < n; i++) {
        for(int j = i - 1; j >= 0; j--) {
            dp[i][a[j]] = max(dp[j][a[i]], dp[j][0]) + 1;
            ans = max(ans, dp[i][a[j]]);
        }
    }
    printf("%d", ans + 1);
}


//TO CHANGE THE WORLD
#include<bits/stdc++.h>

using namespace std;

#define mp make_pair
#define pb push_back
#define L first
#define R second
const long long maxn=1e5, maxl=20;

bool mark[maxn];
long long sum=0, cmp, d[maxn];
pair<long long, long long> par[maxn][maxl];
vector <pair<long long, long long> > adj[maxn];

struct Type_Dsu
{
    long long par[maxn], h[maxn];
    void make_set(long long x)
    {
        par[x]=x;
        h[x]=0;
    }
    long long findpar(long long x)
    {
        return par[x]=(x==par[x]?x:findpar(par[x]));
    }
    bool merge(long long x, long long y)
    {
        x=findpar(x);
        y=findpar(y);
        if(x==y)
            return false;
        if(d[x]>d[y])
            swap(x,y);
        par[x]=y;
        h[y]+=(h[x]==h[y]);
        return true;
    }
    bool same(long long x, long long y)
    {
        return (findpar(x)==findpar(y));
    }
}dsu;
struct Edge
{
    long long fi, se, w;
};
vector<Edge> edges;
void input()
{
    long long n,m;
    cin>>n>>m;
    cmp=n;
    for(long long i=0; i<n; i++)
        dsu.make_set(i);
    for(long long i=0; i<m; i++)
    {
        long long fi,se,w;
        cin>>fi>>se>>w;
        fi--; se--;
        Edge edg;
        edg.fi=fi; edg.se=se; edg.w=w;
        edges.pb(edg);
    }
}
bool comp(Edge a, Edge b)
{
    return (a.w<b.w);
}
void mst()
{
    sort(edges.begin(), edges.end(), comp);
    for(long long i=0; i<edges.size(); i++)
    {
        long long fi=edges[i].fi, se=edges[i].se, w=edges[i].w;
        if(!dsu.same(fi,se))
        {
            cmp--;
            sum+=w;
            dsu.merge(fi,se);
            adj[fi].pb(mp(w,se));
            adj[se].pb(mp(w,fi));
        }
    }
}
void dfs(long long x)
{
    mark[x]=true;
    for(long long i=0; i<adj[x].size(); i++)
    {
        long long child=adj[x][i].R, w=adj[x][i].L;
        if(mark[child])
            continue;
        par[child][0]=mp(x, w);
        d[child]=d[x]+1;
        for(long long j=1; j<maxl; j++)
        {
            long long p=par[par[child][j-1].L][j-1].L;
            long long maxe=max(par[child][j-1].R, par[par[child][j-1].L][j-1].R);
            par[child][j]=mp(p, maxe);
        }
        dfs(child);
    }
}
pair<long long, long long> findpar(long long x, long long d)
{
    long long p=x, maxe=0;
    for(long long i=0; i<maxl; i++)
        if((d>>i)&1)
        {
            maxe=max(maxe,par[p][i].R);
            p=par[p][i].L;
        }
    return mp(p, maxe);
}
long long lca(long long x, long long y)
{
    long long maxe=0;
    if(d[x]>d[y])
        swap(x,y);
    maxe=findpar(y,d[y]-d[x]).R;
    y=findpar(y,d[y]-d[x]).L;
    if(x==y)
        return maxe;
    for(long long i=maxl-1; i>=0; i--)
        if(par[x][i].L!=par[y][i].L)
        {
            maxe=max(maxe, par[x][i].R);
            maxe=max(maxe, par[y][i].R);
            x=par[x][i].L;
            y=par[y][i].L;
        }
    maxe=max(maxe, par[x][0].R);
    maxe=max(maxe, par[y][0].R);
    return maxe;
}
void output()
{
    long long q;
    cin>>q;
    for(long long i=0; i<q; i++)
    {
        long long A,B;
        cin>>A>>B;
        A--; B--;
        cout<<endl;
        if(cmp>2)
            cout<<-1<<endl;
        if(cmp==2)
            if(dsu.same(A, B))
                cout<<-1<<endl;
            else
                cout<<sum<<endl;
        if(cmp==1)
            cout<<sum-lca(A, B)<<endl;
    }
}
int main()
{
    std::sync_with_ios_base("false");
    input();
    mst();
    dfs(0);
    output();
    return 0;
}


//TO CHANGE THE WORLD
#include<bits/stdc++.h>

using namespace std;

#define mp make_pair
#define pb push_back
#define L first
#define R second
const long long maxn=1e5, maxl=20;

bool mark[maxn];
long long sum=0, cmp, d[maxn];
pair<long long, long long> par[maxn][maxl];
vector <pair<long long, long long> > adj[maxn];

struct Type_Dsu
{
    long long par[maxn], h[maxn];
    void make_set(long long x)
    {
        par[x]=x;
        h[x]=0;
    }
    long long findpar(long long x)
    {
        return par[x]=(x==par[x]?x:findpar(par[x]));
    }
    bool merge(long long x, long long y)
    {
        x=findpar(x);
        y=findpar(y);
        if(x==y)
            return false;
        if(d[x]>d[y])
            swap(x,y);
        par[x]=y;
        h[y]+=(h[x]==h[y]);
        return true;
    }
    bool same(long long x, long long y)
    {
        return (findpar(x)==findpar(y));
    }
}dsu;
struct Edge
{
    long long fi, se, w;
};
vector<Edge> edges;
void input()
{
    long long n,m;
    cin>>n>>m;
    cmp=n;
    for(long long i=0; i<n; i++)
        dsu.make_set(i);
    for(long long i=0; i<m; i++)
    {
        long long fi,se,w;
        cin>>fi>>se>>w;
        fi--; se--;
        Edge edg;
        edg.fi=fi; edg.se=se; edg.w=w;
        edges.pb(edg);
    }
}
bool comp(Edge a, Edge b)
{
    return (a.w<b.w);
}
void mst()
{
    sort(edges.begin(), edges.end(), comp);
    for(long long i=0; i<edges.size(); i++)
    {
        long long fi=edges[i].fi, se=edges[i].se, w=edges[i].w;
        if(!dsu.same(fi,se))
        {
            cmp--;
            sum+=w;
            dsu.merge(fi,se);
            adj[fi].pb(mp(w,se));
            adj[se].pb(mp(w,fi));
        }
    }
}
void dfs(long long x)
{
    mark[x]=true;
    for(long long i=0; i<adj[x].size(); i++)
    {
        long long child=adj[x][i].R, w=adj[x][i].L;
        if(mark[child])
            continue;
        par[child][0]=mp(x, w);
        d[child]=d[x]+1;
        for(long long j=1; j<maxl; j++)
        {
            long long p=par[par[child][j-1].L][j-1].L;
            long long maxe=max(par[child][j-1].R, par[par[child][j-1].L][j-1].R);
            par[child][j]=mp(p, maxe);
        }
        dfs(child);
    }
}
pair<long long, long long> findpar(long long x, long long d)
{
    long long p=x, maxe=0;
    for(long long i=0; i<maxl; i++)
        if((d>>i)&1)
        {
            maxe=max(maxe,par[p][i].R);
            p=par[p][i].L;
        }
    return mp(p, maxe);
}
long long lca(long long x, long long y)
{
    long long maxe=0;
    if(d[x]>d[y])
        swap(x,y);
    maxe=findpar(y,d[y]-d[x]).R;
    y=findpar(y,d[y]-d[x]).L;
    if(x==y)
        return maxe;
    for(long long i=maxl-1; i>=0; i--)
        if(par[x][i].L!=par[y][i].L)
        {
            maxe=max(maxe, par[x][i].R);
            maxe=max(maxe, par[y][i].R);
            x=par[x][i].L;
            y=par[y][i].L;
        }
    maxe=max(maxe, par[x][0].R);
    maxe=max(maxe, par[y][0].R);
    return maxe;
}
void output()
{
    long long q;
    cin>>q;
    for(long long i=0; i<q; i++)
    {
        long long A,B;
        cin>>A>>B;
        A--; B--;
        cout<<endl;
        if(cmp>2)
            cout<<-1<<endl;
        if(cmp==2)
            if(dsu.same(A, B))
                cout<<-1<<endl;
            else
                cout<<sum<<endl;
        if(cmp==1)
            cout<<sum-lca(A, B)<<endl;
    }
}
int main()
{
    input();
    mst();
    dfs(0);
    output();
    return 0;
}


//YA HOO
#include <bits/stdc++.h>
#define B begin()
#define E end()
#define RE resize
#define mP make_pair
#define pB push_back
#define X first
#define Y second
#define S size()
#define ST setprecision
const int MAXN = 100 * 1000 + 11;
typedef long long ll;
using namespace std;
int mark[MAXN],D[MAXN],par[MAXN][18],par1[MAXN][18];
vector <pair<int,int> > adj[MAXN],a[MAXN];
vector <pair<int,pair<int,int> > > b,ar_a;
struct Type_DSU
{
	int size[MAXN],par[MAXN];
	void make_set(int x)
	{
		size[x] = 0;
		par[x] = x;
	}
	int findPar(int x)
	{
		return par[x] = (par[x] == x ? x : findPar(par[x]));
	}
	void merge(int x, int y)
	{
		x = findPar(x);
		y = findPar(y);
		if(x == y)
			return;
		if(size[y] > size[x])
			swap(x, y);
		par[y] = x;
		size[x] += (size[x] == size[y]);
		return;
	}
}DSU;
int dfs(int x)
{
	mark[x]=1;
	for(int i=0;i<adj[x].S;i++)
	{
		int pesar=adj[x][i].X;
		if(mark[pesar])
			continue;
		D[pesar]=D[x]+1;
		par[pesar][0]=x;
		par1[pesar][0]=adj[x][i].Y;
		for(int j=1;j<18;j++)
		{
			par[pesar][j]=par[par[pesar][j-1]][j-1];
			par1[pesar][j]=max(par1[pesar][j-1],par1[par[pesar][j-1]][j-1]);
		}
		dfs(pesar);
	}
	return 0;
}
pair<int,int> parent(int x,int d)
{
	int y=0;
	for(int i=0;i<18;i++)
		if((d>>i)&1)
		{
			y=max(y,par1[x][i]);
			x=par[x][i];
		}
	return mP(x,y);
}
int lca(int x,int y)
{
	int z=0;
	if(D[x]>D[y])
		swap(x,y);
	z=parent(y,D[y]-D[x]).Y;
	y=parent(y,D[y]-D[x]).X;
	if(x==y)
	{
		return z;
	}
	for(int i=17;i>=0;i--)
		if(par[x][i]!=par[y][i])
		{
			z=max(z,par1[x][i]);
			z=max(z,par1[y][i]);
			x=par[x][i];
			y=par[y][i];
		}
	z=max(z,par1[x][0]);
	z=max(z,par1[x][0]);
	return z;
}
int main()
{
	ios_base::sync_with_stdio(false);
	ll n,m,q,s=0,s1=0;
	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		int t1,t2,t3;
		cin>>t1>>t2>>t3;
		ar_a.pB(mP(t3,mP(t1,t2)));
		adj[t1].pB(mP(t2,t3));
		adj[t2].pB(mP(t1,t3));
	}
	sort(ar_a.B,ar_a.E);
	for(int i=1;i<=n;i++)
		DSU.make_set(i);
	for(int i=0;i<ar_a.S;i++)
	{
		if(DSU.findPar(ar_a[i].Y.X)!=DSU.findPar(ar_a[i].Y.Y))
		{
			DSU.merge(ar_a[i].Y.X,ar_a[i].Y.Y);
			s++;
			s1+=ar_a[i].X;
		}
	}
	dfs(1);
	cin>>q;
	for(int i=0;i<q;i++)
	{
		int t1,t2;
		cin>>t1>>t2;
		if(s<n-2)
			cout<<-1;
		else if(s==n-2)
		{
			if(DSU.findPar(t1)!=DSU.findPar(t2))
				cout<<s1;
			else
				cout<<-1;
		}
		else
		{
			cout<<s1-lca(t1,t2);
		}
		cout<<endl;
	}
	return 0;

}