#include <iostream>
#include <vector>
#include <string>
#include <deque>
#include <list>
#include <tuple>
#include <algorithm>
#include <set>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <unordered_map>
#include <cmath>
#include <iomanip> 

using namespace std;

uint64_t MAX_NUM = 1000000000;

int32_t N;
vector<int32_t> parent;
vector<int32_t> mysize;

int find_set(int v){
    if(v == parent[v]){
        return v;
    }
    return find_set(parent[v]);
}

void union_sets(int a, int b){
    //cout << " a " << a << " b  " << b << endl;
    a = find_set(a);
    b = find_set(b);
     //cout << "find  a " << a << " b  " << b << endl;
    if(a != b){
        if(mysize[a] < mysize[b]){
            swap(a, b);
        }
        parent[b] = a;
        mysize[a] += mysize[b];
        mysize[b] = 1;
    }
}


int main()
{
    //int32_t N;
    cin >> N;

    vector<int32_t> A = vector<int32_t>(N+1);
    for(int32_t count = 1; count <= N; count++){
        int32_t a;
        cin >> a;
        A[count] = a;
    }



    vector<int32_t> temp(200001, 0); 
    for(int32_t count = 1; count <= N; count++){
        int32_t a = A[count];
        if(temp[a] == 0){
            temp[a] = count;
        }
        A[count] = temp[a];
    }
    // for(int32_t count = 1; count <= N; count++){
        // cout << A[count] << endl;
     //}

    parent = vector<int32_t>(N+1);
    mysize = vector<int32_t>(N+1, 1);
    for(int32_t count = 1; count <= N; count++){
        parent[count] = A[count];
    }

    int32_t i, j;
    for(i = 1, j = N; i < j; i++, j--){
        int32_t a1 = A[i];
        int32_t a2 = A[j];
        if(a1 != a2){
            int32_t a = find_set(i);
            int32_t b = find_set(j);
            union_sets(a, b);
        }
    }

    uint64_t result = 0;
    for(int32_t count = 1; count <= N; count++){
        //cout << " count " << count << " size " << size[count] << endl;
        result += mysize[count] - 1;
    }
    cout << result << endl;

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

struct zz{
	int a[15];
}t[200005];
int Max[15]={};
int n;

int dp[5][(1<<5)+5];
int dp2[5][(1<<5)+5];

int main(){
	cin>>n;
	for(int i=1;i<=n;i++)
		for(int j=0;j<5;j++)
			scanf("%d",&t[i].a[j]);
	dp[0][0]=1e9;
	for(int i=1;i<=n;i++){
		
		for(int j=0;j<3;j++)
			for(int k=0;k<(1<<5);k++)
				dp2[j][k]=dp[j][k];
		for(int l=0;l<5;l++){
			for(int j=0;j<3;j++)
				for(int k=0;k<(1<<5);k++)
					if(k&(1<<l))
						dp2[j][k]=max(dp2[j][k],min(dp2[j][k^(1<<l)],t[i].a[l]));
		}
		for(int j=1;j<=3;j++)
			for(int k=0;k<(1<<5);k++)
				dp[j][k]=max(dp[j][k],dp2[j-1][k]);
	}
	cout<<dp[3][(1<<5)-1];
	return 0;
}