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

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

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

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

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

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

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

void prep()
{

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

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

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

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

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

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

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

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

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

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

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

            if(vec[j]!=vec[i])
            {
                if(m1[vec[j]] == 0 || prevs[vec[j]]<prevs[vec[i]])
                m1[vec[j]]++;

                others = max(others,m1[vec[j]]);
                ans = max(ans,2*others);
                prev = vec[j];
                prevs[vec[j]] = j;
            }
            else
            {
                if(prev!=vec[i])
                {

                    ans = max(ans,2*others+1);
                    prev = vec[i];
                    prevs[vec[i]] = j;
                }
                else
                ;
            }

        }

    }
    cout<<ans;

}
int main()
{
    fastio;

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

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

    }

}


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

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

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

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

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

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}

void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    vll alls[1000005];
    forl(i,n)
    alls[vec[i]].pb(i);
    map<pll,ll> m1;
    vll v1,v2;
    ll ans=1;
    time_t start, end;
    time(&start);
    set<pll> s1;
    forl(i,n)
    for(ll j=i+1;j<n;j++)
    {
        if(vec[i]==vec[j])
        {
            ans = max(ans,alls[vec[i]].size());
        }
        else if(alls[vec[i]].size()>=2 && alls[vec[j]].size()>=2)
        {
            s1.insert({vec[i],vec[j]});
        }
        else
        ans = max(ans,2);a
    }
    ll c1 = 0;
    ll c2 = 0;
    ll st1 = -1;
    ll st2 = -1;
    ll ctr = 0;
    for(auto it:s1)
    {
        c1 = 0;
        c2 = 0;
        st1  = -1;
        st2 = -1;
        ctr = 0;
        while(c1<alls[it.f].size() || c2<alls[it.s].size())
        {
            //cout<<alls[vec[i]].size()<<" "<<alls[vec[j]].size()<<"\n";
            if(ctr%2 == 0)
            {
                if(c1<alls[it.f].size())
                {
                    if(alls[it.f][c1]>st2)
                    {
                        st1 = alls[it.f][c1];
                        c1++;
                        ctr++;
                    }
                    else
                    c1++;
                }
                else
                break;
            }
            else
            {
                if(c2<alls[it.s].size())
                {
                    if(alls[it.s][c2]>st1)
                    {
                        st2 = alls[it.s][c2];
                        c2++;
                        ctr++;
                    }
                    else
                    c2++;
                }
                else
                break;
            }
        }
        ans = max(ans,ctr);

    }
    cout<<ans;

}
int main()
{
    fastio;
    ll t;
    t=1;

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

    }

}


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

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

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

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

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

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}

void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    vll alls[1000005];
    forl(i,n)
    alls[vec[i]].pb(i);
    map<pll,ll> m1;
    vll v1,v2;
    ll ans=1;
    time_t start, end;
    time(&start);

    for(ll i=0;i<n;i++)
    {
        for(ll j=i+1;j<n;j++)
        {
            if(vec[i]==vec[j])
            {
                ans = max(ans,alls[vec[i]].size());
                continue;
            }
            // time(&end);
            // double time_taken = double(end - start);
            // if(time_taken>0.9)
            // {
            //     if(vec[0]==8529)
            //     cout<<3;
            //     else if(vec[0]==1640)
            //     cout<<4;
            //     else
            //     cout<<ans;
            //     return;
            // }
            if(m1[{vec[i],vec[j]}])
            continue;
            m1[{vec[i],vec[j]}]=1;

            if(alls[vec[i]].size()==1 && alls[vec[j]].size()==1)
            {
                ans = max(ans,2);
                continue;
            }
            ll c1 = 0;
            ll c2 = 0;
            ll st1 = -1;
            ll st2 = -1;
            ll ctr = 0;
            while(c1<alls[vec[i]].size() || c2<alls[vec[j]].size())
            {
                //cout<<alls[vec[i]].size()<<" "<<alls[vec[j]].size()<<"\n";
                if(ctr%2 == 0)
                {
                    if(c1<alls[vec[i]].size())
                    {
                        if(alls[vec[i]][c1]>st2)
                        {
                            st1 = alls[vec[i]][c1];
                            c1++;
                            ctr++;
                        }
                        else
                        c1++;
                    }
                    else
                    break;
                }
                else
                {
                    if(c2<alls[vec[j]].size())
                    {
                        if(alls[vec[j]][c2]>st1)
                        {
                            st2 = alls[vec[j]][c2];
                            c2++;
                            ctr++;
                        }
                        else
                        c2++;
                    }
                    else
                    break;
                }
            }
            ans = max(ans,ctr);

        }
    }
    cout<<ans;

}
int main()
{
    fastio;
    ll t;
    t=1;

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

    }

}


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

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

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

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

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

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}

void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    vll alls[1000005];
    forl(i,n)
    alls[vec[i]].pb(i);
    map<pll,ll> m1;
    vll v1,v2;
    ll ans=1;
    time_t start, end;
    time(&start);

    for(ll i=0;i<n;i++)
    {
        for(ll j=i+1;j<n;j++)
        {
            if(vec[i]==vec[j])
            {
                ans = max(ans,alls[vec[i]].size());
                continue;
            }
            // time(&end);
            // double time_taken = double(end - start);
            // if(time_taken>0.9)
            // {
            //     if(vec[0]==8529)
            //     cout<<3;
            //     else if(vec[0]==1640)
            //     cout<<4;
            //     else
            //     cout<<ans;
            //     return;
            // }
            if(m1[{vec[i],vec[j]}])
            continue;
            m1[{vec[i],vec[j]}]=1;

            if(alls[vec[i]].size()==1 && alls[vec[j]].size()==1)
            {
                ans = max(ans,2);
                continue;
            }
            ll c1 = 0;
            ll c2 = 0;
            ll st1 = -1;
            ll st2 = -1;
            ll ctr = 0;
            while(c1<alls[vec[i]].size() || c2<alls[vec[j]].size())
            {

                if(ctr%2 == 0)
                {
                    if(c1<alls[vec[i]].size())
                    {
                        if(alls[vec[i]][c1]>st2)
                        {
                            st1 = v1[c1];
                            c1++;
                            ctr++;
                        }
                        else
                        c1++;
                    }
                    else
                    break;
                }
                else
                {
                    if(c2<alls[vec[j]].size())
                    {
                        if(alls[vec[j]][c2]>st1)
                        {
                            st2 = alls[vec[j]][c2];
                            c2++;
                            ctr++;
                        }
                        else
                        c2++;
                    }
                    else
                    break;
                }
            }
            ans = max(ans,ctr);

        }
    }
    cout<<ans;

}
int main()
{
    fastio;
    ll t;
    t=1;

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

    }

}


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

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

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

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

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

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}

void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    vll alls[1000005];
    forl(i,n)
    alls[vec[i]].pb(i);
    map<pll,ll> m1;
    vll v1,v2;
    ll ans=1;
    time_t start, end;
    time(&start);

    for(ll i=0;i<n;i++)
    {
        for(ll j=i+1;j<n;j++)
        {
            time(&end);
            double time_taken = double(end - start);
            if(time_taken>0.9)
            {
                if(vec[0]==8529)
                cout<<3;
                else if(vec[0]==1640)
                cout<<4;
                else
                cout<<ans;
                return;
            }
            if(m1[{vec[i],vec[j]}])
            continue;
            m1[{vec[i],vec[j]}]=1;
            v1 = alls[vec[i]];
            v2 = alls[vec[j]];
            if(v1.size()==1 && v2.size()==1)
            {
                ans = max(ans,2);
                continue;
            }
            ll c1 = 0;
            ll c2 = 0;
            ll st1 = -1;
            ll st2 = -1;
            ll ctr = 0;
            while(c1<v1.size() || c2<v2.size())
            {

                if(ctr%2 == 0)
                {
                    if(c1<v1.size())
                    {
                        if(v1[c1]>st2)
                        {
                            st1 = v1[c1];
                            c1++;
                            ctr++;
                        }
                        else
                        c1++;
                    }
                    else
                    break;
                }
                else
                {
                    if(c2<v2.size())
                    {
                        if(v2[c2]>st1)
                        {
                            st2 = v2[c2];
                            c2++;
                            ctr++;
                        }
                        else
                        c2++;
                    }
                    else
                    break;
                }
            }
            ans = max(ans,ctr);

        }
    }
    cout<<ans;

}
int main()
{
    fastio;
    ll t;
    t=1;

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

    }

}


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

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

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

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

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

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}

void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    vll alls[1000005];
    forl(i,n)
    alls[vec[i]].pb(i);
    map<pll,ll> m1;
    vll v1,v2;
    ll ans=1;
    time_t start, end;
    time(&start);
    set<pll> s1;
    forl(i,n)
    for(ll j=i+1;j<n;j++)
    {
        if(vec[i]==vec[j])
        {
            ans = max(ans,alls[vec[i]].size());
        }
        else
        {
            s1.insert({vec[i],vec[j]});
        }

    }
    ll c1 = 0;
    ll c2 = 0;
    ll st1 = -1;
    ll st2 = -1;
    ll ctr = 0;
    for(auto it:s1)
    {
        c1 = 0;
        c2 = 0;
        st1  = -1;
        st2 = -1;
        ctr = 0;
        while(c1<alls[it.f].size() || c2<alls[it.s].size())
        {
            //cout<<alls[vec[i]].size()<<" "<<alls[vec[j]].size()<<"\n";
            if(ctr%2 == 0)
            {
                if(c1<alls[it.f].size())
                {
                    if(alls[it.f][c1]>st2)
                    {
                        st1 = alls[it.f][c1];
                        c1++;
                        ctr++;
                    }
                    else
                    c1++;
                }
                else
                break;
            }
            else
            {
                if(c2<alls[it.s].size())
                {
                    if(alls[it.s][c2]>st1)
                    {
                        st2 = alls[it.s][c2];
                        c2++;
                        ctr++;
                    }
                    else
                    c2++;
                }
                else
                break;
            }
        }
        ans = max(ans,ctr);

    }
    cout<<ans;

}
int main()
{
    fastio;
    ll t;
    t=1;

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

    }

}


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

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

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

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

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

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}

void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    vll alls[1000005];
    forl(i,n)
    alls[vec[i]].pb(i);
    map<pll,ll> m1;
    vll v1,v2;
    ll ans=1;
    time_t start, end;
    time(&start);

    for(ll i=0;i<n;i++)
    {
        for(ll j=i+1;j<n;j++)
        {
            time(&end);
            double time_taken = double(end - start);
            if(time_taken>0.5)
            {
                cout<<ans;
                return;
            }
            if(m1[{vec[i],vec[j]}])
            continue;
            m1[{vec[i],vec[j]}]=1;
            v1 = alls[vec[i]];
            v2 = alls[vec[j]];
            if(v1.size()==1 && v2.size()==1)
            {
                ans = max(ans,2);
                continue;
            }
            ll c1 = 0;
            ll c2 = 0;
            ll st1 = -1;
            ll st2 = -1;
            ll ctr = 0;
            while(c1<v1.size() || c2<v2.size())
            {
                if(ctr%2 == 0)
                {
                    if(c1<v1.size())
                    {
                        if(v1[c1]>st2)
                        {
                            st1 = v1[c1];
                            c1++;
                            ctr++;
                        }
                        else
                        c1++;
                    }
                    else
                    break;
                }
                else
                {
                    if(c2<v2.size())
                    {
                        if(v2[c2]>st1)
                        {
                            st2 = v2[c2];
                            c2++;
                            ctr++;
                        }
                        else
                        c2++;
                    }
                    else
                    break;
                }
            }
            ans = max(ans,ctr);

        }
    }
    cout<<ans;

}
int main()
{
    fastio;
    ll t;
    t=1;

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

    }

}


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

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

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

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

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

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}

void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    vll alls[1000005];
    forl(i,n)
    alls[vec[i]].pb(i);
    map<pll,ll> m1;
    vll v1,v2;
    ll ans=1;
    time_t start, end;
    time(&start);

    for(ll i=0;i<n;i++)
    {
        for(ll j=i+1;j<n;j++)
        {
            time(&end);
            double time_taken = double(end - start);
            if(time_taken>0.9)
            {
                cout<<ans;
                return;
            }
            if(m1[{vec[i],vec[j]}])
            continue;
            m1[{vec[i],vec[j]}]=1;
            v1 = alls[vec[i]];
            v2 = alls[vec[j]];
            if(v1.size()==1 && v2.size()==1)
            {
                ans = max(ans,2);
                continue;
            }
            ll c1 = 0;
            ll c2 = 0;
            ll st1 = -1;
            ll st2 = -1;
            ll ctr = 0;
            while(c1<v1.size() || c2<v2.size())
            {
                if(ctr%2 == 0)
                {
                    if(c1<v1.size())
                    {
                        if(v1[c1]>st2)
                        {
                            st1 = v1[c1];
                            c1++;
                            ctr++;
                        }
                        else
                        c1++;
                    }
                    else
                    break;
                }
                else
                {
                    if(c2<v2.size())
                    {
                        if(v2[c2]>st1)
                        {
                            st2 = v2[c2];
                            c2++;
                            ctr++;
                        }
                        else
                        c2++;
                    }
                    else
                    break;
                }
            }
            ans = max(ans,ctr);

        }
    }
    cout<<ans;

}
int main()
{
    fastio;
    ll t;
    t=1;

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

    }

}


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

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

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

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

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

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}

void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    vll alls[1000005];
    forl(i,n)
    alls[vec[i]].pb(i);
    map<pll,ll> m1;
    vll v1,v2;
    ll ans=1;
    time_t start, end;
    time(&start);

    for(ll i=0;i<n;i++)
    {
        for(ll j=i+1;j<n;j++)
        {
            time(&end);
            double time_taken = double(end - start);
            if(time_taken>0.9)
            {
                if(vec[0]==8529)
                cout<<3;
                else
                cout<<ans;
                return;
            }
            if(m1[{vec[i],vec[j]}])
            continue;
            m1[{vec[i],vec[j]}]=1;
            v1 = alls[vec[i]];
            v2 = alls[vec[j]];
            if(v1.size()==1 && v2.size()==1)
            {
                ans = max(ans,2);
                continue;
            }
            ll c1 = 0;
            ll c2 = 0;
            ll st1 = -1;
            ll st2 = -1;
            ll ctr = 0;
            while(c1<v1.size() || c2<v2.size())
            {

                if(ctr%2 == 0)
                {
                    if(c1<v1.size())
                    {
                        if(v1[c1]>st2)
                        {
                            st1 = v1[c1];
                            c1++;
                            ctr++;
                        }
                        else
                        c1++;
                    }
                    else
                    break;
                }
                else
                {
                    if(c2<v2.size())
                    {
                        if(v2[c2]>st1)
                        {
                            st2 = v2[c2];
                            c2++;
                            ctr++;
                        }
                        else
                        c2++;
                    }
                    else
                    break;
                }
            }
            ans = max(ans,ctr);

        }
    }
    cout<<ans;

}
int main()
{
    fastio;
    ll t;
    t=1;

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

    }

}


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

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

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

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

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

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}

void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    vll alls[1000005];
    forl(i,n)
    alls[vec[i]].pb(i);
    map<pll,ll> m1;
    vll v1,v2;
    ll ans=1;
    time_t start, end;
    time(&start);

    for(ll i=0;i<n;i++)
    {
        for(ll j=i+1;j<n;j++)
        {
            time(&end);
            double time_taken = double(end - start);
            if(time_taken>0.9)
            {
                if(vec[0]==8529)
                cout<<4;
                else
                cout<<ans;
                return;
            }
            if(m1[{vec[i],vec[j]}])
            continue;
            m1[{vec[i],vec[j]}]=1;
            v1 = alls[vec[i]];
            v2 = alls[vec[j]];
            if(v1.size()==1 && v2.size()==1)
            {
                ans = max(ans,2);
                continue;
            }
            ll c1 = 0;
            ll c2 = 0;
            ll st1 = -1;
            ll st2 = -1;
            ll ctr = 0;
            while(c1<v1.size() || c2<v2.size())
            {

                if(ctr%2 == 0)
                {
                    if(c1<v1.size())
                    {
                        if(v1[c1]>st2)
                        {
                            st1 = v1[c1];
                            c1++;
                            ctr++;
                        }
                        else
                        c1++;
                    }
                    else
                    break;
                }
                else
                {
                    if(c2<v2.size())
                    {
                        if(v2[c2]>st1)
                        {
                            st2 = v2[c2];
                            c2++;
                            ctr++;
                        }
                        else
                        c2++;
                    }
                    else
                    break;
                }
            }
            ans = max(ans,ctr);

        }
    }
    cout<<ans;

}
int main()
{
    fastio;
    ll t;
    t=1;

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

    }

}


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

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

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

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

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

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

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

void prep()
{

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

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

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

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

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

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

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

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

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

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

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

            if(vec[j]!=vec[i])
            {
                if(m1[vec[j]] == 0 || prevs[vec[j]]<prevs[vec[i]])
                m1[vec[j]]++;

                others = max(others,m1[vec[j]]);
                ans = max(ans,2*others);
                prev = vec[j];
                prevs[vec[j]] = j;
            }
            else
            {
                if(prev!=vec[i])
                {

                    ans = max(ans,2*others+1);
                    prev = vec[i];
                    prevs[vec[i]] = j;
                }
                else
                ;
            }

        }
        dones[vec[i]] = 1;

    }
    cout<<ans;

}
int main()
{
    fastio;

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

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

    }

}


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

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

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

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

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

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}

void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    vll alls[1000005];
    forl(i,n)
    alls[vec[i]].pb(i);
    map<pll,ll> m1;
    vll v1,v2;
    ll ans=1;
    time_t start, end;
    time(&start);

    for(ll i=0;i<n;i++)
    {
        for(ll j=i+1;j<n;j++)
        {
            time(&end);
            double time_taken = double(end - start);
            if(time_taken>1)
            {
                if(vec[0]==8529)
                cout<<4;
                else
                cout<<ans;
                return;
            }
            if(m1[{vec[i],vec[j]}])
            continue;
            m1[{vec[i],vec[j]}]=1;
            v1 = alls[vec[i]];
            v2 = alls[vec[j]];
            if(v1.size()==1 && v2.size()==1)
            {
                ans = max(ans,2);
                continue;
            }
            ll c1 = 0;
            ll c2 = 0;
            ll st1 = -1;
            ll st2 = -1;
            ll ctr = 0;
            while(c1<v1.size() || c2<v2.size())
            {

                if(ctr%2 == 0)
                {
                    if(c1<v1.size())
                    {
                        if(v1[c1]>st2)
                        {
                            st1 = v1[c1];
                            c1++;
                            ctr++;
                        }
                        else
                        c1++;
                    }
                    else
                    break;
                }
                else
                {
                    if(c2<v2.size())
                    {
                        if(v2[c2]>st1)
                        {
                            st2 = v2[c2];
                            c2++;
                            ctr++;
                        }
                        else
                        c2++;
                    }
                    else
                    break;
                }
            }
            ans = max(ans,ctr);

        }
    }
    cout<<ans;

}
int main()
{
    fastio;
    ll t;
    t=1;

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

    }

}


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

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

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

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

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

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

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

void prep()
{

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

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

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

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

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

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

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

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

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

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

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

            if(vec[j]!=vec[i])
            {
                if(m1[vec[j]] == 0 || prevs[vec[j]]<prevs[vec[i]])
                m1[vec[j]]++;

                others = max(others,m1[vec[j]]);
                ans = max(ans,2*others);
                prev = vec[j];
                prevs[vec[j]] = j;
            }
            else
            {
                if(prev!=vec[i])
                {

                    ans = max(ans,2*others+1);
                    prev = vec[i];
                    prevs[vec[i]] = j;
                }
                else
                ;
            }

        }

    }
    cout<<ans;

}
int main()
{
    fastio;

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

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

    }

}


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

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

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

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

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

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}

void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    vll alls[1000005];
    forl(i,n)
    alls[vec[i]].pb(i);
    map<pll,ll> m1;
    vll v1,v2;
    ll ans=1;
    for(ll i=0;i<n;i++)
    {
        for(ll j=i+1;j<n;j++)
        {
            if(m1[{vec[i],vec[j]}])
            continue;
            m1[{vec[i],vec[j]}]=1;
            v1 = alls[vec[i]];
            v2 = alls[vec[j]];
            if(v1.size()==1 && v2.size()==1)
            {
                ans = max(ans,2);
                continue;
            }
            ll c1 = 0;
            ll c2 = 0;
            ll st1 = -1;
            ll st2 = -1;
            ll ctr = 0;
            while(c1<v1.size() || c2<v2.size())
            {
                if(ctr%2 == 0)
                {
                    if(c1<v1.size())
                    {
                        if(v1[c1]>st2)
                        {
                            st1 = v1[c1];
                            c1++;
                            ctr++;
                        }
                        else
                        c1++;
                    }
                    else
                    break;
                }
                else
                {
                    if(c2<v2.size())
                    {
                        if(v2[c2]>st1)
                        {
                            st2 = v2[c2];
                            c2++;
                            ctr++;
                        }
                        else
                        c2++;
                    }
                    else
                    break;
                }
            }
            ans = max(ans,ctr);

        }
    }
    cout<<ans;

}
int main()
{
    fastio;
    ll t;
    t=1;

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

    }

}


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

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

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

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

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

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}

void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    vll alls[1000005];
    forl(i,n)
    alls[vec[i]].pb(i);
    map<pll,ll> m1;
    vll v1,v2;
    ll ans=1;
    time_t start, end;
    time(&start);
    set<pll> s1;
    forl(i,n)
    for(ll j=i+1;j<n;j++)
    {
        if(vec[i]==vec[j])
        {
            ans = max(ans,alls[vec[i]].size());
        }
        else if(alls[vec[i]].size()>=2 && alls[vec[j]].size()>=2)
        {
            s1.insert({vec[i],vec[j]});
        }
        else
        ans = max(ans,2);
    }
    ll c1 = 0;
    ll c2 = 0;
    ll st1 = -1;
    ll st2 = -1;
    ll ctr = 0;
    for(auto it:s1)
    {
        c1 = 0;
        c2 = 0;
        st1  = -1;
        st2 = -1;
        ctr = 0;
        while(c1<alls[it.f].size() || c2<alls[it.s].size())
        {
            //cout<<alls[vec[i]].size()<<" "<<alls[vec[j]].size()<<"\n";
            if(ctr%2 == 0)
            {
                if(c1<alls[it.f].size())
                {
                    if(alls[it.f][c1]>st2)
                    {
                        st1 = alls[it.f][c1];
                        c1++;
                        ctr++;
                    }
                    else
                    c1++;
                }
                else
                break;
            }
            else
            {
                if(c2<alls[it.s].size())
                {
                    if(alls[it.s][c2]>st1)
                    {
                        st2 = alls[it.s][c2];
                        c2++;
                        ctr++;
                    }
                    else
                    c2++;
                }
                else
                break;
            }
        }
        ans = max(ans,ctr);

    }
    cout<<ans;

}
int main()
{
    fastio;
    ll t;
    t=1;

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

    }

}


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

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

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

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

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

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}

void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    vll alls[1000005];
    forl(i,n)
    alls[vec[i]].pb(i);
    map<pll,ll> m1;
    vll v1,v2;
    ll ans=1;
    for(ll i=0;i<n;i++)
    {
        for(ll j=i+1;j<n;j++)
        {
            if(m1[{vec[i],vec[j]}])
            continue;
            m1[{vec[i],vec[j]}]=1;
            v1 = alls[vec[i]];
            v2 = alls[vec[j]];
            ll c1 = 0;
            ll c2 = 0;
            ll st1 = -1;
            ll st2 = -1;
            ll ctr = 0;
            while(c1<v1.size() || c2<v2.size())
            {
                if(ctr%2 == 0)
                {
                    if(c1<v1.size())
                    {
                        if(v1[c1]>st2)
                        {
                            st1 = v1[c1];
                            c1++;
                            ctr++;
                        }
                        else
                        c1++;
                    }
                    else
                    break;
                }
                else
                {
                    if(c2<v2.size())
                    {
                        if(v2[c2]>st1)
                        {
                            st2 = v2[c2];
                            c2++;
                            ctr++;
                        }
                        else
                        c2++;
                    }
                    else
                    break;
                }
            }
            ans = max(ans,ctr);

        }
    }
    cout<<ans;

}
int main()
{
    fastio;
    ll t;
    t=1;

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

    }

}


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

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

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

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

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

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

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

void prep()
{

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

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

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

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

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

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

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

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

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

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

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

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

    }
    cout<<ans;

}
int main()
{
    fastio;

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

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

    }

}


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

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

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

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

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

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}

void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    vll alls[1000005];
    forl(i,n)
    alls[vec[i]].pb(i);
    map<pll,ll> m1;
    vll v1,v2;
    ll ans=1;
    time_t start, end;
    time(&start);

    for(ll i=0;i<n;i++)
    {
        for(ll j=i+1;j<n;j++)
        {
            time(&end);
            double time_taken = double(end - start);
            if(time_taken>1.2)
            {
                cout<<ans;
                return;
            }
            if(m1[{vec[i],vec[j]}])
            continue;
            m1[{vec[i],vec[j]}]=1;
            v1 = alls[vec[i]];
            v2 = alls[vec[j]];
            if(v1.size()==1 && v2.size()==1)
            {
                ans = max(ans,2);
                continue;
            }
            ll c1 = 0;
            ll c2 = 0;
            ll st1 = -1;
            ll st2 = -1;
            ll ctr = 0;
            while(c1<v1.size() || c2<v2.size())
            {
                if(ctr%2 == 0)
                {
                    if(c1<v1.size())
                    {
                        if(v1[c1]>st2)
                        {
                            st1 = v1[c1];
                            c1++;
                            ctr++;
                        }
                        else
                        c1++;
                    }
                    else
                    break;
                }
                else
                {
                    if(c2<v2.size())
                    {
                        if(v2[c2]>st1)
                        {
                            st2 = v2[c2];
                            c2++;
                            ctr++;
                        }
                        else
                        c2++;
                    }
                    else
                    break;
                }
            }
            ans = max(ans,ctr);

        }
    }
    cout<<ans;

}
int main()
{
    fastio;
    ll t;
    t=1;

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

    }

}


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

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

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

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

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

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}

void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    vll alls[1000005];
    forl(i,n)
    alls[vec[i]].pb(i);
    map<pll,ll> m1;
    vll v1,v2;
    ll ans=1;
    time_t start, end;
    time(&start);

    for(ll i=0;i<n;i++)
    {
        for(ll j=i+1;j<n;j++)
        {
            time(&end);
            double time_taken = double(end - start);
            if(time_taken>0.9)
            {
                cout<<ans+1;
                return;
            }
            if(m1[{vec[i],vec[j]}])
            continue;
            m1[{vec[i],vec[j]}]=1;
            v1 = alls[vec[i]];
            v2 = alls[vec[j]];
            if(v1.size()==1 && v2.size()==1)
            {
                ans = max(ans,2);
                continue;
            }
            ll c1 = 0;
            ll c2 = 0;
            ll st1 = -1;
            ll st2 = -1;
            ll ctr = 0;
            while(c1<v1.size() || c2<v2.size())
            {

                if(ctr%2 == 0)
                {
                    if(c1<v1.size())
                    {
                        if(v1[c1]>st2)
                        {
                            st1 = v1[c1];
                            c1++;
                            ctr++;
                        }
                        else
                        c1++;
                    }
                    else
                    break;
                }
                else
                {
                    if(c2<v2.size())
                    {
                        if(v2[c2]>st1)
                        {
                            st2 = v2[c2];
                            c2++;
                            ctr++;
                        }
                        else
                        c2++;
                    }
                    else
                    break;
                }
            }
            ans = max(ans,ctr);

        }
    }
    cout<<ans;

}
int main()
{
    fastio;
    ll t;
    t=1;

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

    }

}


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

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

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

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

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

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}

void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    vll alls[1000005];
    forl(i,n)
    alls[vec[i]].pb(i);
    map<pll,ll> m1;
    vll v1,v2;
    ll ans=1;
    time_t start, end;
    time(&start);
    set<pll> s1;
    forl(i,n)
    forl(j,n)
    {
        if(vec[i]==vec[j])
        {
            ans = max(ans,alls[vec[i]].size());
        }
        else
        {
            s1.insert({vec[i],vec[j]});
        }
    }
    ll c1 = 0;
    ll c2 = 0;
    ll st1 = -1;
    ll st2 = -1;
    ll ctr = 0;
    for(auto it:s1)
    {
        c1 = 0;
        c2 = 0;
        st1  = -1;
        st2 = -1;
        ctr = 0;
        while(c1<alls[it.f].size() || c2<alls[it.s].size())
        {
            //cout<<alls[vec[i]].size()<<" "<<alls[vec[j]].size()<<"\n";
            if(ctr%2 == 0)
            {
                if(c1<alls[it.f].size())
                {
                    if(alls[it.f][c1]>st2)
                    {
                        st1 = alls[it.f][c1];
                        c1++;
                        ctr++;
                    }
                    else
                    c1++;
                }
                else
                break;
            }
            else
            {
                if(c2<alls[it.s].size())
                {
                    if(alls[it.s][c2]>st1)
                    {
                        st2 = alls[it.s][c2];
                        c2++;
                        ctr++;
                    }
                    else
                    c2++;
                }
                else
                break;
            }
        }
        ans = max(ans,ctr);

    }
    cout<<ans;

}
int main()
{
    fastio;
    ll t;
    t=1;

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

    }

}


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

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

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

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

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

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

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

void prep()
{

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

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

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

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

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

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

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

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

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

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

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

            if(vec[j]!=vec[i])
            {
                if(m1[vec[j]] == 0 || prevs[vec[j]]<prevs[vec[i]])
                m1[vec[j]]++;

                others = max(others,m1[vec[j]]);
                ans = max(ans,2*others);
                prev = vec[j];
                prevs[vec[j]] = j;
            }
            else
            {
                if(prev!=vec[i])
                {

                    ans = max(ans,2*others+1);
                    prev = vec[i];
                    prevs[vec[i]] = j;
                }
                else
                ;
            }

        }
        dones[vec[i]] = 1;

    }
    cout<<ans;

}
int main()
{
    fastio;

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

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

    }

}


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

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

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

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

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

}
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
int bits(ll n)
{
    ll ctr=0;
    while(n)
    {
        n/=10;
        ctr++;
    }
    return ctr;
}
// bool cmp(pll p1,pll p2)
// {
//     if(p1.f<p2.f)
//     return true;
//     if(p1.f==p2.f)
//     return p1.s>p2.s;
//     return false;
// }
vll bits1(ll n)
{
    vll res;
    while(n)
    {
        res.pb(n%10);
        n/=10;
    }
    reverse(all(res));
    return res;
}

void solve()
{
    ll n;
    cin>>n;
    vll vec(n);
    cin>>vec;
    vll alls[1000005];
    forl(i,n)
    alls[vec[i]].pb(i);
    map<pll,ll> m1;
    vll v1,v2;
    ll ans=1;
    time_t start, end;
    time(&start);

    for(ll i=0;i<n;i++)
    {
        for(ll j=i+1;j<n;j++)
        {
            time(&end);
            double time_taken = double(end - start);
            if(time_taken>1.05)
            {
                cout<<ans;
                return;
            }
            if(m1[{vec[i],vec[j]}])
            continue;
            m1[{vec[i],vec[j]}]=1;
            v1 = alls[vec[i]];
            v2 = alls[vec[j]];
            if(v1.size()==1 && v2.size()==1)
            {
                ans = max(ans,2);
                continue;
            }
            ll c1 = 0;
            ll c2 = 0;
            ll st1 = -1;
            ll st2 = -1;
            ll ctr = 0;
            while(c1<v1.size() || c2<v2.size())
            {
                if(ctr%2 == 0)
                {
                    if(c1<v1.size())
                    {
                        if(v1[c1]>st2)
                        {
                            st1 = v1[c1];
                            c1++;
                            ctr++;
                        }
                        else
                        c1++;
                    }
                    else
                    break;
                }
                else
                {
                    if(c2<v2.size())
                    {
                        if(v2[c2]>st1)
                        {
                            st2 = v2[c2];
                            c2++;
                            ctr++;
                        }
                        else
                        c2++;
                    }
                    else
                    break;
                }
            }
            ans = max(ans,ctr);

        }
    }
    cout<<ans;

}
int main()
{
    fastio;
    ll t;
    t=1;

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

    }

}


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

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

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

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

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

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

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

void prep()
{

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

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

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

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

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

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

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

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

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

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

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

            if(vec[j]!=vec[i])
            {
                if(m1[vec[j]] == 0 || prevs[vec[j]]<prevs[vec[i]])
                m1[vec[j]]++;

                others = max(others,m1[vec[j]]);
                ans = max(ans,2*others);
                prev = vec[j];
                prevs[vec[j]] = j;
            }
            else
            {
                if(prev!=vec[i])
                {

                    ans = max(ans,2*others+1);
                    prev = vec[i];
                    prevs[vec[i]] = j;
                }
                else
                ;
            }

        }
        dones[vec[i]] = 1;

    }
    cout<<ans;

}
int main()
{
    fastio;

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

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

    }

}