#include<iostream>
#include<map>
#include<vector>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		vector<int>v(n);
		map<int,int>m;
		map<int,int> ::iterator it;
		for(int i=0; i<n; i++)
		{
			cin>>v[i];
		}
		for(int i=0; i<n; i++)
		{
			m[v[i]]++;
		}
		it = m.begin();
		if(it->second==1)
		{
			cout<<it->first<<endl;
		}
		else
		{
			cout<<-1<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,f=0,p;
		cin>>n;
		vector<long long int>v(n);
		map<long long int,long long int>m;
		map<long long int,long long int> ::iterator it;
		for(int i=0; i<n; i++)
		{
			cin>>v[i];
		}

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

		for(it=m.begin(); it!=m.end(); it++)
		{
			if((*it).second==1)
			{
				 p = (*it).first;
				f=1;
				break;
			}
		}

		if(f==0)
		{
			cout<<-1<<endl;
		}
		else
		{	for(int i=0; i<v.size(); i++)
			{
			    if(v[i]==p)
			    {
			        cout<<i+1<<endl;
			        break;
			    }
			}
		}
	}
}


#include<iostream>
#include<map>
#include<vector>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,f=0;
		cin>>n;
		vector<int>v(n);
		map<int,int>m;
		map<int,int> ::iterator it;
		for(int i=0; i<n; i++)
		{
			cin>>v[i];
		}
		for(int i=0; i<n; i++)
		{
			m[v[i]]++;
		}
		for(it = m.begin(); it!=m.end(); it++)
		{

			if(it->second==1)
			{
				cout<<it->first<<endl;
				f=1;
				break;
			}
		}
		if(f==0)
		{
			cout<<-1<<endl;
		}
	}
}


#include<bit/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,f=0,p;
		cin>>n;
		vector<long long int>v(n);
		map<long long int,long long int>m;
		map<long long int,long long int> ::iterator it;
		for(int i=0; i<n; i++)
		{
			cin>>v[i];
		}

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

		for(it=m.begin(); it!=m.end(); it++)
		{
			if((*it).second==1)
			{
				 p = (*it).first;
				f=1;
				break;
			}
		}

		if(f==0)
		{
			cout<<-1<<endl;
		}
		else
		{
			auto it = find(v.begin(), v.end(), p);
			cout<<(it-v.begin())+1;
		}
	}
}


#include<iostream>
#include<map>
#include<vector>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,f=0;
		cin>>n;
		vector<int>v(n);
		map<int,int>m;
		map<int,int> ::iterator it;
		for(int i=0; i<n; i++)
		{
			cin>>v[i];
		}
		for(int i=0; i<n; i++)
		{
			m[v[i]]++;
		}
	for(it = m.begin(); it!=m.end(); it++)
	{

		if(it->second==1)
		{
			cout<<it->first<<endl;
			f=1;
			break;
		}
	}
	if(f==0)
	{
		cout<<-1<<endl;
	}
}


#include<iostream>
#include<map>
#include<vector>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,f=0;
		cin>>n;
		vector<int>v(n);
		map<int,int>m;
		map<int,int> ::iterator it;
		for(int i=0; i<n; i++)
		{
			cin>>v[i];
		}

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

		for(it=m.begin(); it!=m.end(); it++)
		{
			if(it->second==1)
			{
				cout<<it->first<<endl;
				f=1;
				break;
			}
		}

		if(f==0)
		{
			cout<<-1<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,f=0,p;
		cin>>n;
		vector<long long int>v(n);
		map<long long int,long long int>m;
		map<long long int,long long int> ::iterator it;
		for(int i=0; i<n; i++)
		{
			cin>>v[i];
		}

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

		for(it=m.begin(); it!=m.end(); it++)
		{
			if((*it).second==1)
			{
				 p = (*it).first;
				f=1;
				break;
			}
		}

		if(f==0)
		{
			cout<<-1<<endl;
		}
		else
		{
			auto it = find(v.begin(), v.end(), p);
			cout<<(it-v.begin())+1;
		}
	}
}


#include<iostream>
#include<map>
#include<vector>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,f=0;
		cin>>n;
		vector<long long int>v(n);
		map<long long int,long long int>m;
		map<long long int,long long int> ::iterator it;
		for(int i=0; i<n; i++)
		{
			cin>>v[i];
		}

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

		for(it=m.begin(); it!=m.end(); it++)
		{
			if((*it).second==1)
			{
				cout<<(*it).first<<endl;
				f=1;
				break;
			}
		}

		if(f==0)
		{
			cout<<-1<<endl;
		}
	}
}


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

int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int m,j,c=-1,mi=10000000;
        cin>>m;
        vector<int>v(m);
        map<int,int> M;
        for(int i = 0;i<m;i++){
            cin>>v[i];
            M[v[i]]++;
        }
        map<int,int>:: iterator it;
        int ans =-1;
        for(it = M.begin();it!=M.end();it++){
            if(it->second==1){
                ans = it->first;
                break;
            }
        }

        for(int i = 0;i<m;i++){
            if(v[i]==ans) {
                ans = i+1;
                break;
            }
        }
        cout<<ans<<endl;
    }
}


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

int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int m,j,c=-1,mi;
        cin>>m;
        vector<int>v;
        long long b[1000000]={};
        int a[m];
        for(int i=0; i<m; i++)
        {
            cin>>a[i];v.push_back(a[i]);
            b[a[i]]++;
        }
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
       for(int i=0;i<m;i++)
       {
           if(b[v[i]]==1){mi=v[i];c=1;break;}
       }

if(c==1)
{
    for(int i=0;i<m;i++)
    {
        if(a[i]==mi){cout<<i+1<<endl;break;}
    }
}
else cout<<-1<<endl;

    }
}


#include<bits/stdc++.h>
#define lli long long int
using namespace std;
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int m,j,c=0;
        cin>>m;
        int a[m],b[100000]={};
        for(int i=0;i<m;i++){cin>>a[i];b[a[i]]++;}
        for( j=0;j<100000;j++)
        {
            if(b[j]==1){c=1;break;}
        }
        if(c==1)
        {
            for(int i=0;i<m;i++)
            {
                if(a[i]==j){cout<<i+1<<endl;break;}
            }
        }
        else cout<<-1<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    for(cin>>t;t--;)
    {
        int m;
        cin>>m;
        map<int,int>mp;
        map<int,int>::iterator it;
        int a[m];for(int i=0;i<m;i++){cin>>a[i];mp[a[i]]++;}
        int ans=-1,c;
        for(it=mp.begin();it!=mp.end();it++)
        {
            if(it->second==1){ans=it->first;break;}
        }
        for(int i=0;i<m;i++)if(a[i]==c){ans=i+1;break;}
        cout<<ans<<endl;
    }

}


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

int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int m,j,c=0;
        cin>>m;
        long long b[1000000]={};
        int a[m];
        for(int i=0;i<m;i++){cin>>a[i];b[a[i]]++;}
        for( j=0;j<1000000;j++)
        {
            if(b[j]==1){c=1;break;}
        }
        if(c==1)
        {
            for(int i=0;i<m;i++)
            {
                if(a[i]==j){cout<<i+1<<endl;break;}
            }
        }
        else cout<<-1<<endl;

    }
}


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

int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int m,j,c=-1,mi;
        cin>>m;
        vector<int>v;
        long long b[1000000]={};
        int a[m];
        for(int i=0; i<m; i++)
        {
            cin>>a[i];v.push_back(a[i]);
            b[a[i]]++;
        }
sort(a,a+m);
       for(int i=0;i<m;i++)
       {
           if(b[a[i]]==1){mi=a[i];c=1;break;}
       }

if(c==1)
{
    for(int i=0;i<m;i++)
    {
        if(v[i]==mi){cout<<i+1<<endl;break;}
    }
}
else cout<<-1<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    for(cin>>t;t--;)
    {
        int m;
        cin>>m;
        map<int,int>mp;
        map<int,int>::iterator it;
        int a[m];for(int i=0;i<m;i++){cin>>a[i];mp[a[i]]++;}
        int ans=-1,c;
        for(it=mp.begin();it!=mp.end();it++)
        {
            if(it->second==1){c=it->first;break;}
        }
        for(int i=0;i<m;i++)if(a[i]==c){ans=i+1;break;}
        cout<<ans<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MN LLONG_MIN
#define MX LLONG_MAX
#define UL ULLONG_MAX
#define pb push_back
#define e "\n"
#define iff(u,v)  if(abs(u-v)<1e-9)
#define vi vector<long long>v,v1,v2;
#define pi 3.1415926536
#define dc ll i,j,k,n,m,len,ans=0,mx=-1,mn,sum=0; string s; bool flag=false;

void solve()
{
	dc; cin>>n; ll ar[n];
	map<int,int>mp;
	for(i=0;i<n;i++)
	{
		cin>>ar[i]; if(mp[ar[i]]==0)mp[ar[i]]=i;
	}
	sort(ar,ar+n);
	for(i=0;i<n-1;i++)
	{
		if(i==0 && ar[0]!=ar[1])
		{
			cout<<mp[ar[0]]+1<<e;
			goto end;
		}
		else
		{
			if(ar[i]!=ar[i-1] && ar[i]!=ar[i+1])
			{
				cout<<mp[ar[i]]+1<<e;
				goto end;
			}
		}
	}
	if(ar[n-1]!=ar[n-2]){cout<<mp[ar[n-1]]+1<<e; goto end;}
	cout<<-1<<e;
	end:;
}

int main()
{
    //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int t; cin>>t; while(t--)solve();
    //solve();
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MN LLONG_MIN
#define MX LLONG_MAX
#define UL ULLONG_MAX
#define pb push_back
#define e "\n"
#define iff(u,v)  if(abs(u-v)<1e-9)
#define vi vector<long long>v,v1,v2;
#define pi 3.1415926536
#define dc ll i,j,k,n,m,len,ans=0,mx=-1,mn,sum=0; string s; bool flag=false;

void solve()
{
	dc; cin>>n; ll ar[n];
	map<int,int>mp;
	for(i=0;i<n;i++)
	{
		cin>>ar[i]; if(mp[ar[i]]==0)mp[ar[i]]=i;
	}
	sort(ar,ar+n);
	for(i=0;i<n;i++)
	{
		if(i==0 && ar[0]!=ar[1])
		{
			cout<<mp[ar[0]]+1<<e;
			goto end;
		}
		else
		{
			if(ar[i]!=ar[i-1] && ar[i]!=ar[i+1])
			{
				cout<<mp[ar[i]]+1<<e;
				goto end;
			}
		}
	}
	cout<<-1<<e;
	end:;
}

int main()
{
    //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int t; cin>>t; while(t--)solve();
    //solve();
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MN LLONG_MIN
#define MX LLONG_MAX
#define UL ULLONG_MAX
#define pb push_back
#define e "\n"
#define iff(u,v)  if(abs(u-v)<1e-9)
#define vi vector<long long>v,v1,v2;
#define pi 3.1415926536
#define dc ll i,j,k,n,m,len,ans=0,mx=-1,mn,sum=0; string s; bool flag=false;

void solve()
{
	dc; cin>>n; ll ar[n];
	map<ll,ll>mp;
	for(i=0;i<n;i++)
	{
		cin>>ar[i]; mp[ar[i]]=i;
	}
	sort(ar,ar+n);
	for(i=0;i<n;i++)
	{
		if(i==0 && ar[0]!=ar[1])
		{
			cout<<mp[ar[0]]+1<<e;
			goto end;
		}
		else
		{
			if(ar[i]!=ar[i-1] && ar[i]!=ar[i+1])
			{
				cout<<mp[ar[i]]+1<<e;
				goto end;
			}
		}
	}
	cout<<-1<<e;
	end:;
}

int main()
{
    //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int t; cin>>t; while(t--)solve();
    //solve();
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MN LLONG_MIN
#define MX LLONG_MAX
#define UL ULLONG_MAX
#define pb push_back
#define e "\n"
#define iff(u,v)  if(abs(u-v)<1e-9)
#define vi vector<long long>v,v1,v2;
#define pi 3.1415926536
#define dc ll i,j,k,n,m,len,ans=0,mx=-1,mn,sum=0; string s; bool flag=false;

void solve()
{
	dc; cin>>n;
	ll ar[n+1],br[n+2];
	br[0]=-1;
	for(i=1;i<=n;i++)
	{
		cin>>ar[i];
		br[i]=ar[i];
	}
	br[n+1]=MX;
	sort(br,br+n+2);
	for(i=1;i<=n;i++)
	{
		if(br[i]!=br[i-1] && br[i]!=br[i+1]){k=br[i]; break;}
	}
	//cout<<k;
	for(i=1;i<=n;i++)
	{
		if(ar[i]==k){cout<<i<<e; goto end;}
	}
	cout<<-1<<e;
	end:;
}

int main()
{
    //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int t; cin>>t; while(t--)solve();
    //solve();
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MN LLONG_MIN
#define MX LLONG_MAX
#define UL ULLONG_MAX
#define pb push_back
#define e "\n"
#define iff(u,v)  if(abs(u-v)<1e-9)
#define vi vector<long long>v,v1,v2;
#define pi 3.1415926536
#define dc ll i,j,k,n,m,len,ans=0,mx=-1,mn,sum=0; string s; bool flag=false;

void solve()
{
	dc; cin>>n; ll ar[n];
	map<int,int>mp;
	for(i=0;i<n;i++)
	{
		cin>>ar[i]; mp[ar[i]]=i;
	}
	sort(ar,ar+n);
	for(i=0;i<n;i++)
	{
		if(i==0 && ar[0]!=ar[1])
		{
			cout<<mp[ar[0]]+1<<e;
			goto end;
		}
		else
		{
			if(ar[i]!=ar[i-1] && ar[i]!=ar[i+1])
			{
				cout<<mp[ar[i]]+1<<e;
				goto end;
			}
		}
	}
	cout<<-1<<e;
	end:;
}

int main()
{
    //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int t; cin>>t; while(t--)solve();
    //solve();
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MN LLONG_MIN
#define MX LLONG_MAX
#define UL ULLONG_MAX
#define pb push_back
#define e "\n"
#define iff(u,v)  if(abs(u-v)<1e-9)
#define vi vector<long long>v,v1,v2;
#define pi 3.1415926536
#define dc ll i,j,k,n,m,len,ans=0,mx=-1,mn,sum=0; string s; bool flag=false;

void solve()
{
	dc; cin>>n; ll ar[n];
	map<ll,ll>mp;
	for(i=0;i<n;i++)
	{
		cin>>ar[i]; if(mp[ar[i]]==0)mp[ar[i]]=i;
	}
	sort(ar,ar+n);
	for(i=0;i<n;i++)
	{
		if(i==0 && ar[0]!=ar[1])
		{
			cout<<mp[ar[0]]+1<<e;
			goto end;
		}
		else
		{
			if(ar[i]!=ar[i-1] && ar[i]!=ar[i+1])
			{
				cout<<mp[ar[i]]+1<<e;
				goto end;
			}
		}
	}
	cout<<-1<<e;
	end:;
}

int main()
{
    //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int t; cin>>t; while(t--)solve();
    //solve();
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MN LLONG_MIN
#define MX LLONG_MAX
#define UL ULLONG_MAX
#define pb push_back
#define e "\n"
#define iff(u,v)  if(abs(u-v)<1e-9)
#define vi vector<long long>v,v1,v2;
#define pi 3.1415926536
#define dc ll i,j,k,n,m,len,ans=0,mx=-1,mn,sum=0; string s; bool flag=false;

void solve()
{
	dc; cin>>n; k=-2;
	ll ar[n+1],br[n+2];
	br[0]=-1;
	for(i=1;i<=n;i++)
	{
		cin>>ar[i];
		br[i]=ar[i];
	}
	br[n+1]=MX;
	sort(br,br+n+2);
	for(i=1;i<=n;i++)
	{
		if(br[i]!=br[i-1] && br[i]!=br[i+1]){k=br[i]; break;}
	}
	//cout<<k;
	for(i=1;i<=n;i++)
	{
		if(ar[i]==k){cout<<i<<e; goto end;}
	}
	cout<< -1 <<e;
	end:;
}

int main()
{
    //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int t; cin>>t; while(t--)solve();
    //solve();
}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int x[n];
        for(int i=0; i<n; i++) cin>>x[i];
        vector<int> cnt(n+1), pos(n);
        for(int i=0; i<n; i++){
            cnt[x[i]]++;
            pos[x[i]]=i;
        }
        bool b=false;
        for(int i=1; i<=n; i++){
            if(cnt[i]==1){
                cout<<pos[i+1]<<endl;
                b=true;
                break;
            }
        }
        if(!b) cout<<-1<<endl;
    }
}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int x[n];
        for(int i=0; i<n; i++) cin>>x[i];
        vector<int> cnt(n+1), pos(n);
        for(int i=0; i<n; i++){
            cnt[x[i]]++;
            pos[x[i]]=i+1;
        }
        int ans=-1;
        for(int i=1; i<=n; i++){
            if(cnt[i]==1){
                ans=pos[i];
            }
        }
        cout<<ans<<endl;
    }
}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int x[n];
        vector<int> cnt(n+1), pos(n+1);
        for(int i=0; i<n; i++){
            cin>>x[i];
            cnt[x[i]]++;
            pos[x[i]]=i+1;
        }
        int ans=-1;
        for(int i=1; i<=n; i++){
            if(cnt[i]==1){
                ans=pos[i];
                break;
            }
        }
        cout<<ans<<endl;
    }
}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int x[n];
        vector<int> cnt(n+1), pos(n);
        for(int i=0; i<n; i++){
            cin>>x[i];
            cnt[x[i]]++;
            pos[x[i]]=i+1;
        }
        int ans=-1;
        for(int i=1; i<=n; i++){
            if(cnt[i]==1){
                ans=pos[i];
                break;
            }
        }
        cout<<ans<<endl;
    }
}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int x[n];
        for(int i=0; i<n; i++) cin>>x[i];
        vector<int> cnt(n+1), pos(n);
        for(int i=0; i<n; i++){
            cnt[x[i]]++;
            pos[x[i]]=i+1;
        }
        int ans=-1;
        for(int i=1; i<=n; i++){
            if(cnt[i]==1){
                ans=pos[i];
                break;
            }
        }
        cout<<ans<<endl;
    }
}


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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int x[n];
        vector<int> cnt(n+1), pos(n);
        for(int i=0; i<n; i++){
            cin>>x[i];
            cnt[x[i]]++;
            pos[x[i]]=i+1;
        }
        int ans=-1;
        for(int i=1; i<=n; i++){
            if(cnt[i]==1){
                ans=pos[i];
                break;
            }
        }
        cout<<ans<<endl;
    }
}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int x[n];
        for(int i=0; i<n; i++) cin>>x[i];
        vector<int> cnt(n+1), pos(n);
        for(int i=0; i<n; i++){
            cnt[x[i]]++;
            pos[x[i]]=i+1;
        }
        bool b=false;
        for(int i=1; i<=n; i++){
            if(cnt[i]==1){
                cout<<pos[i]<<endl;
                b=true;
                break;
            }
        }
        if(!b) cout<<-1<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
const int N=21e5+1;
struct node
{
    int t,id;
}e[N];
bool cmp(node a,node b)
{
    return a.t<b.t;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>e[i].t;
            e[i].id=i;
        }
        sort(e+1,e+1+n,cmp);
        int f=1;
        for(int i=1;i<=n;i++)
        {
            if(e[i].t!=e[i+1].t&&e[i].t!=e[i-1].t)
            {
                f=0;
                cout<<e[i].id<<endl;
                break;
            }
        }
        if(f) cout<<-1<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=21e5+1;
int a[N],b[N];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,y,f=0;
        cin>>n;
        for(int i=1; i<=n; i++)
        {
            cin>>a[i];
            b[i]=a[i];
        }
        sort(a+1,a+1+n);
        a[0]=0,a[n+1]=0;
        for(int i=1; i<=n; i++)
        {
            if(a[i]!=a[i+1]&&a[i]!=a[i-1])
            {
                y=a[i];
                f=1;
                break;
            }
        }
        if(f==0)
            cout<<-1<<endl;
        else
        {
            for(int i=1; i<=n; i++)
            {
                if(b[i]==y)
                    cout<<i<<endl;
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e+5;
struct node
{
    int t, z;
} e[maxn];
bool cmp(node a,node b)
{
    return a.t < b.t;
}
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        for (int i = 1; i <= n;i++)
        {
            cin >> e[i].t;
            e[i].z = i;
        }
        sort(e + 1, e + 1 + n,cmp);
        int f = 1;
        e[n + 1].t = 0;
        e[0].t = 0;
        for (int i = 1; i <= n;i++)
        {
            if(e[i].t!=e[i+1].t&&e[i].t!=e[i-1].t)
            {
                cout << e[i].z << endl;
                f = 0;
                break;
            }
        }
        if(f)
            cout << -1 << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=21e5+1;
int a[N],b[N];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,y,f=0;
        cin>>n;
        for(int i=1; i<=n; i++)
        {
            cin>>a[i];
            b[i]=a[i];
        }
        sort(a+1,a+1+n);
        for(int i=1; i<=n; i++)
        {
            if(a[i]!=a[i+1]&&a[i]!=a[i-1])
            {
                y=a[i];
                f=1;
                break;
            }
        }
        if(f==0)
            cout<<-1<<endl;
        else
        {
            for(int i=1; i<=n; i++)
            {
                if(b[i]==y)
                    cout<<i<<endl;
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=21e5+5;
struct node
{
    int t,id;
}e[N];
bool cmp(node a,node b)
{
    return a.t<b.t;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>e[i].t;
            e[i].id=i;
        }
        sort(e+1,e+1+n,cmp);
        int f=1;
        e[n+1].t=0;
        e[0].t=0;
        for(int i=1;i<=n;i++)
        {
            if(e[i].t!=e[i+1].t&&e[i].t!=e[i-1].t)
            {
                f=0;
                cout<<e[i].id<<endl;
                break;
            }
        }
        if(f) cout<<-1<<endl;
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)

char T[1 << 20];
int n, lim, fri[22][22], sat[22][22], sum[22], binomial[22][22], dp[524524];

int H(int bitmask, int c){
    int y, x = bitmask, res = sum[c];

    while (x){
        y = (-x & x);
        res += binomial[n - T[y] - 1][c--];
        x ^= y;
    }
    return res;
}

int F(int bitmask, int c){
    if (bitmask == lim) return 0;
    int h = H(bitmask, c);
    if (dp[h] != -1) return dp[h];

    int i, j, z, y, yy, xx, cost, res = 0, x = bitmask ^ lim;
    while (x){
        y = (-x & x);
        i = T[y];
        x ^= y;

        xx = bitmask ^ lim;
        while (xx){
            yy = (-xx & xx);
            j = T[yy];
            xx ^= yy;

            if (i != j){
                int nextmask = bitmask;
                nextmask |= (1 << i);
                nextmask |= (1 << j);
                cost = F(nextmask, c + 2);

                z = fri[c][i] + sat[c + 1][j] + cost;
                if (z > res) res = z;
                z = sat[c][i] + fri[c + 1][j] + cost;
                if (z > res) res = z;
                z = fri[c + 1][i] + sat[c][j] + cost;
                if (z > res) res = z;
                z = sat[c + 1][i] + fri[c][j] + cost;
                if (z > res) res = z;
            }
        }
    }

    return (dp[h] = res);
}

int main(){
    int i, j, k, res;
    for (i = 0; i < 20; i++) T[1 << i] = i;

    for (i = 0; i < 22; i++){
        for (j = 0; j <= i; j++){
            if (i == j || j == 0) binomial[i][j] = 1;
            else binomial[i][j] = (binomial[i - 1][j] + binomial[i - 1][j - 1]);
        }
    }

    while (scanf("%d", &n) != EOF){
        for (i = 0; i < n; i++){
            for (j = 0; j < n; j++){
                scanf("%d", &fri[i][j]);
            }
        }
        for (i = 0; i < n; i++){
            for (j = 0; j < n; j++){
                scanf("%d", &sat[i][j]);
            }
        }

        /*n = 20;
        for (i = 0; i < n; i++){
            for (j = 0; j < n; j++){
                fri[i][j] = rand();
                sat[i][j] = rand();
            }
        }*/

        sum[0] = 0;
        for (i = 2; i <= n; i++) sum[i] = sum[i - 2] + binomial[n][i - 2];

        lim = (1 << n) - 1;
        memset(dp, -1, sizeof(dp));
        printf("%d\n", F(0, 0));
    }
    return 0;
}


#include <map>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cmath>
#include <set>
#include <stdio.h>
#include <ctime>
using namespace std;
template <class T>inline bool rd(T &ret) {
   char c;
   int sgn;
   if (c = getchar(), c == EOF) return 0;
   while (c != '-' && (c<'0' || c>'9')) c = getchar();
   sgn = (c == '-') ? -1 : 1;
   ret = (c == '-') ? 0 : (c - '0');
   while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
   ret *= sgn;
   return 1;
}
template <class T>inline void pt(T x) {
   if (x < 0) {
      putchar('-');
      x = -x;
   }
   if (x > 9) pt(x / 10);
   putchar(x % 10 + '0');
}
typedef pair<int, int> pii;
const int inf = 1e9;
const int N = 22;
const int M = N;
int ans;
struct KM {
   int n, w[M][M];
   int lx[M], ly[M], match[M], slack[M];
   bool s[M], t[M];
   inline bool hungary(int x) {
      s[x] = true;
      for (int y = 0; y < n; ++y)			if (!t[y] && lx[x] + ly[y] == w[x][y]) {
            t[y] = true;
            if (match[y] == -1 || hungary(match[y])) {
               match[y] = x;
               return true;
            }
         }			else slack[y] = min(slack[y], lx[x] + ly[y] - w[x][y]);
      return false;
   }	inline int rediculous() {
      int magic;
      magic = 0;
      for (int i = 0; i < n; ++i) {
         lx[i] = *max_element(w[i], w[i] + n);
         magic += lx[i];
      }
      if (magic <= ans) return 0;
      memset(match, -1, n << 2);
      memset(ly, 0, n << 2);
      for (register int i = 0; i < n; ++i) {
         for (;;) {
            memset(s, 0, n);
            memset(t, 0, n);
            memset(slack, 0x7f, n << 2);
            if (hungary(i)) break;
            else {
               int a = inf;
               for (register int j = 0; j < n; ++j) if (!t[j] && slack[j] < a)						a = slack[j];
               magic = 0;
               for (register int j = 0; j < n; ++j) {
                  if (s[j]) lx[j] -= a;
                  if (t[j]) ly[j] += a;
                  magic += lx[j] + ly[j];
               }
               if (magic <= ans) {
                  return 0;
               }
            }
         }
      }
      ans = magic;
      return magic;
   }
} S;
int n;
int a[N][N], b[N][N];
int CNT(int x) {
   int siz = 0;
   while (x) {
      siz += x & 1;
      x >>= 1;
   }
   return siz;
}
int main() {
   clock_t lim = clock() + 1.9*CLOCKS_PER_SEC;
   rd(n);
   S.n = n;
   for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)rd(a[i][j]);
   for (int i = 0; i < n; i++)for (int j = 0; j < n; j++) rd(b[i][j]);
   ans = 0;
   for (int i = 0; i < 1 << n; ++i)		if (CNT(i) == (n >> 1))		{
         for (int j = 0; j < n; j++)				if (i&(1 << j))					memcpy(S.w[j], a[j], n<<2);
            else					memcpy(S.w[j], b[j], n<<2);
         ans = max(ans, S.rediculous());
         if (clock() >= lim)break;
      }
   pt(ans);
   return 0;
}


#include <bits/stdc++.h>

int const N = 42;

    int p1[N], p2[N], row[N], col[N], mind[N], from[N], was[N];
    int a[N][N], b[N][N], c[N][N];
    int n;
    int nn;

    void getMatching() {
        for (int i = 0; i < n; i++) {
            if ((nn >> i) & 1) {
                p2[p1[i]] = -1;
                p1[i] = -1;
                int allMin = 1 << 30;
                for (int j = 0; j < n; j++) {
                    allMin = std::min(allMin, a[i][j] - col[j]);
                }
                row[i] = allMin;
            }
        }
        for (int i = 0; i < n; i++) {
            if (p1[i] >= 0) {
                continue;
            }
            std::fill(was, was + n, 0);
            std::fill(mind, mind + n, 1 << 30);
            int cur = i;
            int curPair = -1;
            do {
                if (curPair >= 0) {
                    was[curPair] = true;
                    cur = p2[curPair];
                }
                int d = 1 << 30;
                int minPair = -1;
                for (int j = 0; j < n; j++) {
                    if (was[j]) {
                        continue;
                    }
                    int val = a[cur][j] - row[cur] - col[j];
                    if (val < mind[j]) {
                        mind[j] = val;
                        from[j] = curPair;
                    }
                    if (mind[j] < d) {
                        d = mind[j];
                        minPair = j;
                    }
                }
                row[i] += d;
                for (int j = 0; j < n; j++) {
                    if (was[j]) {
                        col[j] -= d;
                        row[p2[j]] += d;
                    } else {
                        mind[j] -= d;
                    }
                }
                curPair = minPair;
            } while (p2[curPair] >= 0);
            while (from[curPair] >= 0) {
                int prev = from[curPair];
                p2[curPair] = p2[prev];
                p1[p2[prev]] = curPair;
                curPair = prev;
            }
            p2[curPair] = i;
            p1[i] = curPair;
        }
    }

    int main() {
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                scanf("%d", c[i] + j);
                c[i][j] = -c[i][j];
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                scanf("%d", b[i] + j);
                b[i][j] = -b[i][j];
            }
        }
        std::fill(p1, p1 + n, -1);
        std::fill(p2, p2 + n, -1);
        int ans = 1 << 30;
        int lastmask = -1;
        for (int mask = 0; mask < 1 << n; mask++) {
            if (__builtin_popcount(mask) != n / 2) continue;
            if (lastmask >= 0) {
                nn = lastmask ^ mask;
            } else nn = -1;
            for (int i = 0; i < n; i++) {
                if (((mask >> i) & 1) == 0) {
                    for (int j = 0; j < n; j++) a[i][j] = b[i][j];
                } else {
                    for (int j = 0; j < n; j++) a[i][j] = c[i][j];
                }
            }
            getMatching();
            int sum = 0;
            for (int i = 0; i < n; i++) sum += a[i][p1[i]];
            ans = std::min(ans, sum);
            lastmask = mask;
        }
        printf("%d\n", -ans);
    }


#include<cstdio>
#include<ctime>
#define rep(i,x,y) for(int i=(x);i<=(y);i++)
#define inf 1000000000
#define ne 1000000
using namespace std;
int n,ans,c[30],a[30][30],b[30][30];
int nn,s,t,al,ff,dl[200],v[50],fr[50],d[50],w[50][50],f[50][50];
inline void add(int x,int y,int z){
    w[x][y]=z;f[x][y]=1;w[y][x]=-z;f[y][x]=0;
}
inline void flow(){
    for(;;){
        rep(i,2,nn)d[i]=inf;d[1]=0;
        s=1;t=1;dl[1]=1;v[1]=1;ff=inf;
        while(s<=t){
            int ss=dl[s];
            rep(i,1,nn)if(f[ss][i]&&d[ss]+w[ss][i]<d[i]){
                fr[i]=ss;d[i]=d[ss]+w[ss][i];
                if(!v[i])dl[++t]=i,v[i]=1;
            }
            s++;v[ss]=0;
        }
        if(d[nn]==inf){
            al=ne*n-al;if(al>ans)ans=al;al=0;return;
        }
        for(int i=nn;i>1;i=fr[i])
            if(f[fr[i]][i]<ff)ff=f[fr[i]][i];
        for(int i=nn;i>1;i=fr[i])
            f[fr[i]][i]-=ff,f[i][fr[i]]+=ff;
        al+=ff*d[nn];
    }
}
int main(){
    scanf("%d",&n);nn=n+n+2;
    rep(i,1,n)rep(j,1,n)scanf("%d",&a[i][j]);
    rep(i,1,n)rep(j,1,n)scanf("%d",&b[i][j]);
    int tot=0;
    while(!c[n+1]){
        rep(i,1,n)add(1,i+1,0);
        rep(i,1,n)rep(j,1,n)
            if(!c[i])add(i+1,j+n+1,ne-a[i][j]);
            else add(i+1,j+n+1,ne-b[i][j]);
        rep(i,1,n)add(i+n+1,nn,0);
        flow();int j=1;for(;c[j];j++)c[j]=0;c[j]=1;
//      if  (clock() / (double)(CLOCKS_PER_SEC) >= 1.9)  break;
        ++tot;
        if(tot>=1e5) break;
    }
    printf("%d\n",ans);
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define CH (ch=getchar())
#define dprintf(...) fprintf(stderr,__VA_ARGS__)
#define rep(i,V)     for(__typeof(*V.begin()) i:  V)
#define For(i,a,b)   for(int i=(int)a;i<=(int)b;i++)
#define Rep(i,a,b)   for(int i=(int)a;i< (int)b;i++)
#define Forn(i,a,b)  for(int i=(int)a;i>=(int)b;i--)
#define pend(x)      ((x)=='\n'||(x)=='\r'||(x)=='\t'||(x)==' ')
using namespace std;
typedef double      db;
typedef long long   ll;
const   int N=25;
const   int mo=1000000007;
const   int inf=(int)1e9;

inline int IN(){
    char ch;CH; int f=0,x=0;
    for(;pend(ch);CH); if(ch=='-')f=1,CH;
    for(;!pend(ch);CH) x=x*10+ch-'0';
    return (f)?(-x):(x);
}

int Pow(int x,int y,int p){
    int A=1;
    for(;y;y>>=1,x=(ll)x*x%p) if(y&1) A=(ll)A*x%p;
    return A;
}

int n,m,laba[N],labb[N],visa[N],visb[N],tim,mat[N],ans;
int w[N][N],a[N][N],b[N][N],c[N][N],tot_done;

bool extend(int x){
    if(!x) return 1;
    visa[x]=tim;
    For(i,1,n){
        if(visb[i]!=tim&&laba[x]+labb[i]==w[x][i]){
            visb[i]=tim;
            if(extend(mat[i])){
                mat[i]=x;
                return 1;
            }
        }
    }
    return 0;
}

void change(){
    int delta=inf;
    For(i,1,n) if(visa[i]==tim)
    For(j,1,n) if(visb[j]!=tim){
        delta=min(delta,laba[i]+labb[j]-w[i][j]);
    }

    For(i,1,n) if(visa[i]==tim) laba[i]-=delta;
    For(i,1,n) if(visb[i]==tim) labb[i]+=delta;
}

int KM(){
    For(i,1,n) visa[i]=visb[i]=mat[i]=0;
    For(i,1,n) laba[i]=w[i][0],labb[i]=0;
    tim=0;
    For(i,1,n){
        for(;;){
            ++tim;
            if(extend(i))break;
            change();
        }
    }

    int re=0;
    For(i,1,n) re+=w[mat[i]][i];
    return re;
}

void dfs(int x,int cnt){
    int now=KM();
    if(now<=ans) return;
    if(x==n){
        ans=now;
        return;
    }
    ++tot_done;
    if(tot_done>=1e5)return;
    ++x;
    if(m-cnt<n-x+1){
        For(i,0,n) w[x][i]=a[x][i];
        dfs(x,cnt);
    }
    if(cnt<m){
        For(i,0,n) w[x][i]=b[x][i];
        dfs(x,cnt+1);
    }
    For(i,0,n) w[x][i]=c[x][i];
}

int main(){
    scanf("%d",&n); m=n>>1;
    For(i,1,n) For(j,1,n){
        a[i][j]=IN();
        a[i][0]=max(a[i][0],a[i][j]);
    }
    For(i,1,n) For(j,1,n){
        b[i][j]=IN();
        b[i][0]=max(b[i][0],b[i][j]);
    }
    For(i,1,n) For(j,1,n){
        w[i][j]=c[i][j]=max(a[i][j],b[i][j]);
        w[i][0]=c[i][0]=max(w[i][0],c[i][j]);
    }
    dfs(0,0);
    printf("%d\n",ans);
    return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define rint register int
#define ll long long
#define ld long double
#define For(i, a, b) for (int i = a, __end = b; i <= __end; ++i)
#define Ford(i, a, b) for (int i = a, __end = b; i >= __end; --i)
using namespace std;

const int MaxN = 22, inf = 1 << 30;
int Map[MaxN][MaxN], N, val[2][MaxN][MaxN], ans, pre[MaxN];

void augment(int u, int pl[], int pr[]) {
    while (u) {
        int v = pl[pre[u]];
        pr[u] = pre[u];
        pl[pre[u]] = u;
        u = v;
    }
}

void KM(int ind, int labl[], int labr[], int pl[], int pr[]) {
    static int Q[MaxN], slack[MaxN];
    static bool vis[MaxN];
    memset(slack, 63, sizeof(slack));
    memset(vis, 0, sizeof(vis));
    int l = 0, r = 0;
    Q[++r] = ind;
    vis[ind] = 1;
    while (1) {
        while (l != r) {
            int u = Q[++l];
            For(v, 1, N)
                if (!vis[pr[v]]) {
                    if (labl[u] + labr[v] == Map[u][v]) {
                        pre[v] = u;
                        if (!pr[v]) return augment(v, pl, pr);
                        vis[pr[v]] = 1;
                        Q[++r] = pr[v];
                    } else if (slack[v] > labl[u] + labr[v] - Map[u][v]) {
                        slack[v] = labl[u] + labr[v] - Map[u][v];
                        pre[v] = u;
                    }
                }
        }
        int delta = inf;
        For(i, 1, N)
            if (!vis[pr[i]]) delta = min(delta, slack[i]);
        For(i, 1, N) {
            if (vis[i]) labl[i] -= delta;
            if (vis[pr[i]]) labr[i] += delta;
            else slack[i] -= delta;
        }
        For(i, 1, N)
            if (!vis[pr[i]] && !slack[i]) {
                if (!pr[i]) return augment(i, pl, pr);
                Q[++r] = pr[i];
                vis[pr[i]] = 1;
            }
    }
}

void Dfs(int ind, int arr[4][MaxN], int cnt) {
    if (ind == N + 1) {
        int tmp = 0;
        For(i, 1, N) tmp += arr[0][i] + arr[1][i];
        ans = max(ans, tmp);
        return;
    }
    for (int k = 0; k != 2; k++) {
        if ((cnt + k > N / 2) || (cnt + k + (N - ind) < N / 2)) continue;
        int narr[4][MaxN];
        memcpy(narr, arr, sizeof(narr));
        For(i, 1, N) {
            Map[ind][i] = val[k][i][ind];
            narr[0][ind] = max(narr[0][ind], Map[ind][i]);
        }
        KM(ind, narr[0], narr[1], narr[2], narr[3]);
        Dfs(ind + 1, narr, cnt + k);
    }
}

int main()
{
    scanf("%d", &N);
    For(k, 0, 1)
        For(i, 1, N)
            For(j, 1, N) scanf("%d", &val[k][i][j]);
    int arr[4][MaxN] = {};
    Dfs(1, arr, 0);
    cout << ans << endl;
    return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define rint register int
#define ll long long
#define ld long double
#define For(i, a, b) for (int i = a, __end = b; i <= __end; ++i)
#define Ford(i, a, b) for (int i = a, __end = b; i >= __end; --i)
using namespace std;

const int MaxN = 22, inf = 1 << 30;
int Map[MaxN][MaxN], N, val[2][MaxN][MaxN], ans;

void augment(int u, int pre[], int pl[], int pr[]) {
	while (u) {
		int v = pl[pre[u]];
		pr[u] = pre[u];
		pl[pre[u]] = u;
		u = v;
	}
}

void KM(int ind, int labl[], int labr[], int pl[], int pr[]) {
	static int Q[MaxN], pre[MaxN], slack[MaxN];
	static bool vis[MaxN];
	memset(slack, 63, sizeof(slack));
	memset(vis, 0, sizeof(vis));
	int l = 0, r = 0;
	Q[++r] = ind;
	vis[ind] = 1;
	while (1) {
		while (l++ != r) {
			int u = Q[l];
			For(v, 1, N)
				if (!vis[pr[v]]) {
					if (labl[u] + labr[v] == Map[u][v]) {
						pre[v] = u;
						if (!pr[v]) return augment(v, pre, pl, pr);
						vis[pr[v]] = 1;
						Q[++r] = pr[v];
					} else if (slack[v] > labl[u] + labr[v] - Map[u][v]) {
						slack[v] = labl[u] + labr[v] - Map[u][v];
						pre[v] = u;
					}
				}
		}
		int delta = inf;
		For(i, 1, N)
			if (!vis[pr[i]]) delta = min(delta, slack[i]);
		For(i, 1, N) {
			if (vis[i]) labl[i] -= delta;
			if (vis[pr[i]]) labr[i] += delta;
			else slack[i] -= delta;
		}
		For(i, 1, N)
			if (!vis[pr[i]] && !slack[i]) {
				if (!pr[i]) return augment(i, pre, pl, pr);
				Q[++r] = pr[i];
				vis[pr[i]] = 1;
			}
	}
}

void Dfs(int ind, int arr[4][MaxN]) {
	if (ind == N + 1) {
		int tmp = 0;
		For(i, 1, N) tmp += arr[0][i] + arr[1][i];
		ans = max(ans, tmp);
		return;
	}
	for (int k = 0; k != 2; k++) {
		int narr[4][MaxN];
		memcpy(narr, arr, sizeof(narr));
		For(i, 1, N) {
			Map[ind][i] = val[k][i][ind];
			narr[0][ind] = max(narr[0][ind], Map[ind][i]);
		}
		KM(ind, narr[0], narr[1], narr[2], narr[3]);
		Dfs(ind + 1, narr);
	}
}

int main()
{
	scanf("%d", &N);
	For(k, 0, 1)
		For(i, 1, N)
			For(j, 1, N) scanf("%d", &val[k][i][j]);
	int arr[4][MaxN] = {};
	Dfs(1, arr);
	cout << ans << endl;
	return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define rint register int
#define ll long long
#define ld long double
#define For(i, a, b) for (int i = a, __end = b; i <= __end; ++i)
#define Ford(i, a, b) for (int i = a, __end = b; i >= __end; --i)
using namespace std;

const int MaxN = 22, inf = 1 << 30;
int Map[MaxN][MaxN], N, val[2][MaxN][MaxN], ans;

void augment(int u, int pre[], int pl[], int pr[]) {
    while (u) {
        int v = pl[pre[u]];
        pr[u] = pre[u];
        pl[pre[u]] = u;
        u = v;
    }
}

void KM(int ind, int labl[], int labr[], int pl[], int pr[]) {
    static int Q[MaxN], pre[MaxN], slack[MaxN];
    static bool vis[MaxN];
    memset(slack, 63, sizeof(slack));
    memset(vis, 0, sizeof(vis));
    int l = 0, r = 0;
    Q[++r] = ind;
    vis[ind] = 1;
    while (1) {
        while (l++ != r) {
            int u = Q[l];
            For(v, 1, N)
                if (!vis[pr[v]]) {
                    if (labl[u] + labr[v] == Map[u][v]) {
                        pre[v] = u;
                        if (!pr[v]) return augment(v, pre, pl, pr);
                        vis[pr[v]] = 1;
                        Q[++r] = pr[v];
                    } else if (slack[v] > labl[u] + labr[v] - Map[u][v]) {
                        slack[v] = labl[u] + labr[v] - Map[u][v];
                        pre[v] = u;
                    }
                }
        }
        int delta = inf;
        For(i, 1, N)
            if (!vis[pr[i]]) delta = min(delta, slack[i]);
        For(i, 1, N) {
            if (vis[i]) labl[i] -= delta;
            if (vis[pr[i]]) labr[i] += delta;
            else slack[i] -= delta;
        }
        For(i, 1, N)
            if (!vis[pr[i]] && !slack[i]) {
                if (!pr[i]) return augment(i, pre, pl, pr);
                Q[++r] = pr[i];
                vis[pr[i]] = 1;
            }
    }
}

void Dfs(int ind, int arr[4][MaxN], int cnt) {
    if (ind == N + 1) {
        int tmp = 0;
        For(i, 1, N) tmp += arr[0][i] + arr[1][i];
        ans = max(ans, tmp);
        return;
    }
    for (int k = 0; k != 2; k++) {
        if ((cnt + k > N / 2) || (cnt + k + (N - ind) < N / 2)) continue;
        int narr[4][MaxN];
        memcpy(narr, arr, sizeof(narr));
        For(i, 1, N) {
            Map[ind][i] = val[k][i][ind];
            narr[0][ind] = max(narr[0][ind], Map[ind][i]);
        }
        KM(ind, narr[0], narr[1], narr[2], narr[3]);
        Dfs(ind + 1, narr, cnt + k);
    }
}

int main()
{
    scanf("%d", &N);
    For(k, 0, 1)
        For(i, 1, N)
            For(j, 1, N) scanf("%d", &val[k][i][j]);
    int arr[4][MaxN] = {};
    Dfs(1, arr, 0);
    cout << ans << endl;
    return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORd(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

#define maxn 21
#define inf 1000000000
struct Hungary {
    int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    bool used[maxn];
    void init(int _nx, int _ny) {
        nx = _nx; ny = _ny; memset(fx, 0, sizeof(fx)); memset(fy, 0, sizeof(fy)); memset(maty, 0, sizeof(maty)); memset(which, 0, sizeof(which)); memset(dist, 0, sizeof(dist));
        for (int i = 0; i <= nx; i++) for (int j = 0; j <= ny; j++) cost[i][j] = inf;
    }
    void add(int x, int y, int c) {cost[x][y] = c;}
    int mincost() {
        for (int x = 1; x <= nx; x++) {
            int y0 = 0; maty[0] = x;
            for (int y = 0; y <= ny; y++) {dist[y] = inf + 1; used[y] = false;}
            do {
                used[y0] = true;
                int x0 = maty[y0], delta = inf + 1, y1;
                for (int y = 1; y <= ny; y++) if (!used[y]) {
                    int curdist = cost[x0][y] - fx[x0] - fy[y];
                    if (curdist < dist[y]) {
                        dist[y] = curdist;
                        which[y] = y0;
                    }
                    if (dist[y] < delta) {
                        delta = dist[y];
                        y1 = y;
                    }
                }
                for (int y = 0; y <= ny; y++) if (used[y]) {
                    fx[maty[y]] += delta;
                    fy[y] -= delta;
                } else dist[y] -= delta;
                y0 = y1;
            }
            while (maty[y0] != 0);
            do {
                int y1 = which[y0];
                maty[y0] = maty[y1];
                y0 = y1;
            }
            while (y0);
        }
        int ret = 0;
        for (int y = 1; y <= ny; y++) {
            int x = maty[y];
            if (cost[x][y] < inf) ret += cost[x][y];
        }
        return ret;
    }
} hungary;

int n;
int a[maxn][maxn];
int b[maxn][maxn];

void solve() {
    scanf("%d", &n);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &a[i][j]);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &b[i][j]);
    int ans = 0;
    FORd(i, 1 << n, 0) {
        if (clock() > 1990) break;
        bool flag[maxn]; ms(flag, 0); int cnt = 0;
        FOR(j, 0, n) if (i & 1 << j) {flag[j] = 1; cnt++;}
        if (cnt != n >> 1) continue;
        hungary.init(n, n);
        FOR(j, 0, n) FOR(k, 0, n) {
            if (flag[k]) hungary.add(j + 1, k + 1, -a[j][k]);
            else hungary.add(j + 1, k + 1, -b[j][k]);
        }
        ans = min(ans, hungary.mincost());
    }
    printf("%d", -ans);
}

int main() {
    //ios_base::sync_with_stdio(0); cin.tie(NULL);
#ifdef _LOCAL_
    freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
    solve();
#ifdef _LOCAL_
    printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
    return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORd(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

#define maxn 21
#define inf 1000000000
struct Hungary {
    int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    bool used[maxn];
    void init(int _nx, int _ny) {
        nx = _nx; ny = _ny; memset(fx, 0, sizeof(fx)); memset(fy, 0, sizeof(fy)); memset(maty, 0, sizeof(maty)); memset(which, 0, sizeof(which)); memset(dist, 0, sizeof(dist));
        for (int i = 0; i <= nx; i++) for (int j = 0; j <= ny; j++) cost[i][j] = inf;
    }
    void add(int x, int y, int c) {cost[x][y] = c;}
    int mincost() {
        for (int x = 1; x <= nx; x++) {
            int y0 = 0; maty[0] = x;
            for (int y = 0; y <= ny; y++) {dist[y] = inf + 1; used[y] = false;}
            do {
                used[y0] = true;
                int x0 = maty[y0], delta = inf + 1, y1;
                for (int y = 1; y <= ny; y++) if (!used[y]) {
                    int curdist = cost[x0][y] - fx[x0] - fy[y];
                    if (curdist < dist[y]) {
                        dist[y] = curdist;
                        which[y] = y0;
                    }
                    if (dist[y] < delta) {
                        delta = dist[y];
                        y1 = y;
                    }
                }
                for (int y = 0; y <= ny; y++) if (used[y]) {
                    fx[maty[y]] += delta;
                    fy[y] -= delta;
                } else dist[y] -= delta;
                y0 = y1;
            }
            while (maty[y0] != 0);
            do {
                int y1 = which[y0];
                maty[y0] = maty[y1];
                y0 = y1;
            }
            while (y0);
        }
        int ret = 0;
        for (int y = 1; y <= ny; y++) {
            int x = maty[y];
            if (cost[x][y] < inf) ret += cost[x][y];
        }
        return ret;
    }
} hungary;

int n;
int a[maxn][maxn];
int b[maxn][maxn];

void solve() {
    scanf("%d", &n);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &a[i][j]);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &b[i][j]);
    int ans = 0;
    FOR(i, 0, 1 << n) {
        if (clock() > 1.90) break;
        bool flag[maxn]; ms(flag, 0); int cnt = 0;
        FOR(j, 0, n) if (i & 1 << j) {flag[j] = 1; cnt++;}
        if (cnt != n >> 1) continue;
        hungary.init(n, n);
        FOR(j, 0, n) FOR(k, 0, n) {
            if (flag[k]) hungary.add(j + 1, k + 1, -a[j][k]);
            else hungary.add(j + 1, k + 1, -b[j][k]);
        }
        ans = min(ans, hungary.mincost());
    }
    printf("%d", -ans);
}

int main() {
    //ios_base::sync_with_stdio(0); cin.tie(NULL);
#ifdef _LOCAL_
    freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
    solve();
#ifdef _LOCAL_
    printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
    return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORd(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

#define maxv 100
#define maxe 10000
#define linf 1000000000000000000LL
struct MincostFlow {
    int n, s, t, E, adj[maxe], next[maxe], last[maxv], which[maxv];
    long long totalCost, totalFlow, cap[maxe], flow[maxe], cost[maxe], pot[maxe], dist[maxv];
    void init(int _n, int _s, int _t) {
        n = _n; s = _s; t = _t;
        memset(last, -1, sizeof(last)); E = 0;
    }
    void add(int u, int v, long long ca, long long co) {
        adj[E] = v; cap[E] = ca; flow[E] = 0; cost[E] = +co; next[E] = last[u]; last[u] = E++;
        adj[E] = u; cap[E] =  0; flow[E] = 0; cost[E] = -co; next[E] = last[v]; last[v] = E++;
    }
    void bellman() {
        bool stop = false;
        memset(pot, 0, sizeof(pot));
        while (!stop) {
            stop = true;
            for (int u = 0; u < n; u++) for (int e = last[u]; e != -1; e = next[e]) if (flow[e] < cap[e]) {
                int v = adj[e];
                if (pot[v] > pot[u] + cost[e]) {
                    pot[v] = pot[u] + cost[e];
                    stop = false;
                }
            }
        }
    }
    bool dijkstra() {
        typedef pair<long long, int> node;
        priority_queue<node, vector<node>, greater<node> > que;
        for (int u = 0; u < n; u++) dist[u] = linf;
        dist[s] = 0;
        que.push(make_pair(0, s));
        while (!que.empty()) {
            long long dnow = que.top().first;
            int u = que.top().second;
            que.pop();
            if (dnow > dist[u]) continue;
            for (int e = last[u]; e != -1; e = next[e]) if (flow[e] < cap[e]) {
                int v = adj[e];
                long long dnext = dnow + cost[e] + pot[u] - pot[v];
                if (dist[v] > dnext) {
                    dist[v] = dnext;
                    which[v] = e;
                    que.push(make_pair(dnext, v));
                }
            }
        }
        return dist[t] < linf;
    }
    bool maxflow(long long desireFlow = linf) {
        totalCost = totalFlow = 0;
        bellman();
        while (totalFlow < desireFlow) {
            if (!dijkstra()) return false;
            long long delta = desireFlow - totalFlow;
            for (int v = t, e = which[v]; v != s; v = adj[e ^ 1], e = which[v]) delta = min(delta, cap[e] - flow[e]);
            for (int v = t, e = which[v]; v != s; v = adj[e ^ 1], e = which[v]) {
                flow[e] += delta;
                flow[e ^ 1] -= delta;
            }
            totalFlow += delta;
            totalCost += delta * (dist[t] - pot[s] + pot[t]);
            for (int u = 0; u < n; u++) pot[u] += dist[u];
        }
        return true;
    }
} mcf;

const int maxn = 21;
int n;
int a[maxn][maxn];
int b[maxn][maxn];

void solve() {
    scanf("%d", &n);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &a[i][j]);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &b[i][j]);
    mcf.init(4 * n + 4, 4 * n + 3, 4 * n + 2);
    FOR(i, 0, n) mcf.add(4 * n + 3, i, 1, 0);
    FOR(i, 0, n) FOR(j, 0, n) mcf.add(i, j + n, 1, 0);
    FOR(i, 0, n) FOR(j, 0, n){
        mcf.add(i + n, j + 2 * n, 1, -a[i][j]);
        mcf.add(i + n, j + 3 * n, 1, -b[i][j]);
    }
    FOR(i, 0, n) {
        mcf.add(i + 2 * n, 4 * n, 1, 0);
        mcf.add(i + 3 * n, 4 * n + 1, 1, 0);
    }
    mcf.add(4 * n, 4 * n + 2, n >> 1, 0);
    mcf.add(4 * n + 1, 4 * n + 2, n >> 1, 0);
    mcf.maxflow();
    cout<<-mcf.totalCost;
}

int main() {
    //ios_base::sync_with_stdio(0); cin.tie(NULL);
#ifdef _LOCAL_
    freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
    solve();
#ifdef _LOCAL_
    printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
    return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORd(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

#define maxn 21
#define inf 1000000000
struct Hungary {
    int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    bool used[maxn];
    void init(int _nx, int _ny) {
        nx = _nx; ny = _ny; memset(fx, 0, sizeof(fx)); memset(fy, 0, sizeof(fy)); memset(maty, 0, sizeof(maty)); memset(which, 0, sizeof(which));
        for (int i = 0; i <= nx; i++) for (int j = 0; j <= ny; j++) cost[i][j] = inf;
    }
    void add(int x, int y, int c) {cost[x][y] = c;}
    void update(int x) {
        int y0 = 0; maty[0] = x;
        for (int y = 0; y <= ny; y++) {dist[y] = inf + 1; used[y] = false;}
        do {
            used[y0] = true;
            int x0 = maty[y0], delta = inf + 1, y1;
            for (int y = 1; y <= ny; y++) if (!used[y]) {
                int curdist = cost[x0][y] - fx[x0] - fy[y];
                if (curdist < dist[y]) {
                    dist[y] = curdist;
                    which[y] = y0;
                }
                if (dist[y] < delta) {
                    delta = dist[y];
                    y1 = y;
                }
            }
            for (int y = 0; y <= ny; y++) if (used[y]) {
                fx[maty[y]] += delta;
                fy[y] -= delta;
            }
            else dist[y] -= delta;
            y0 = y1;
        }
        while (maty[y0] != 0);
        do {
            int y1 = which[y0];
            maty[y0] = maty[y1];
            y0 = y1;
        }
        while (y0);
    }
    Hungary operator = (const Hungary& h) {
        nx = h.nx; ny = h.ny;
        FOR(i, 0, maxn) {
            fx[i] = h.fx[i];
            fy[i] = h.fy[i];
            //maty[i] = h.maty[i];
            //which[i] = h.which[i];
            //dist[i] = h.dist[i];
        }
        return h;
    }
} hungary;

int n;
int a[maxn][maxn];
int b[maxn][maxn];
int ans;

void go(int pos, int bit, int cnt) {
    Hungary tmp = hungary;
    FOR(i, 0, n) if (bit == 1) hungary.add(pos + 1, i + 1, -a[pos][i]); else hungary.add(pos + 1, i + 1, -b[pos][i]);
    hungary.update(pos + 1);
    if (pos == n - 1) {
        if (cnt == (n >> 1)) {
            int tot = 0;
            for (int y = 1; y <= hungary.ny; y++) {
                int x = hungary.maty[y];
                if (hungary.cost[x][y] < inf) tot -= hungary.cost[x][y];
            }
            ans = max(ans, tot);
        }
        hungary = tmp; FOR(i, 0, n) hungary.add(pos + 1, i + 1, inf);
        return;
    }
    if (cnt + n - pos - 1 >= (n >> 1)) go(pos + 1, 0, cnt);
    if (cnt + 1 <= (n >> 1)) go(pos + 1, 1, cnt + 1);
    hungary = tmp;
    FOR(i, 0, n) hungary.add(pos + 1, i + 1, inf);
}

void solve() {
    scanf("%d", &n);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &a[i][j]);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &b[i][j]);
    ans = 0;
    hungary.init(n, n); FOR(i, 0, n) hungary.add(1, i + 1, -a[0][i]);
    go(0, 0, 0);
    hungary.init(n, n); FOR(i, 0, n) hungary.add( 1, i + 1, -b[0][i]);
    go(0, 1, 1);
    printf("%d", ans);
}

int main() {
    //ios_base::sync_with_stdio(0); cin.tie(NULL);
#ifdef _LOCAL_
    freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
    solve();
#ifdef _LOCAL_
    printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
    return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORd(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

#define maxn 21
#define inf 1000000000
struct Hungary {
    int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    bool used[maxn];
    void init(int _nx, int _ny) {
        nx = _nx; ny = _ny; memset(fx, 0, sizeof(fx)); memset(fy, 0, sizeof(fy)); memset(maty, 0, sizeof(maty)); memset(which, 0, sizeof(which)); memset(dist, 0, sizeof(dist));
        for (int i = 0; i <= nx; i++) for (int j = 0; j <= ny; j++) cost[i][j] = inf;
    }
    void add(int x, int y, int c) {cost[x][y] = c;}
    int mincost() {
        for (int x = 1; x <= nx; x++) {
            int y0 = 0; maty[0] = x;
            for (int y = 0; y <= ny; y++) {dist[y] = inf + 1; used[y] = false;}
            do {
                used[y0] = true;
                int x0 = maty[y0], delta = inf + 1, y1;
                for (int y = 1; y <= ny; y++) if (!used[y]) {
                    int curdist = cost[x0][y] - fx[x0] - fy[y];
                    if (curdist < dist[y]) {
                        dist[y] = curdist;
                        which[y] = y0;
                    }
                    if (dist[y] < delta) {
                        delta = dist[y];
                        y1 = y;
                    }
                }
                for (int y = 0; y <= ny; y++) if (used[y]) {
                    fx[maty[y]] += delta;
                    fy[y] -= delta;
                } else dist[y] -= delta;
                y0 = y1;
            }
            while (maty[y0] != 0);
            do {
                int y1 = which[y0];
                maty[y0] = maty[y1];
                y0 = y1;
            }
            while (y0);
        }
        int ret = 0;
        for (int y = 1; y <= ny; y++) {
            int x = maty[y];
            if (cost[x][y] < inf) ret += cost[x][y];
        }
        return ret;
    }
} hungary;

int n;
int a[maxn][maxn];
int b[maxn][maxn];

void solve() {
    scanf("%d", &n);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &a[i][j]);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &b[i][j]);
    int ans = 0;
    hungary.init(n, n);
    FOR(i, 0, 1 << n) {
        bool flag[maxn]; ms(flag, 0); int cnt = 0;
        FOR(j, 0, n) if (i & 1 << j) {flag[j] = 1; cnt++;}
        if (cnt != n >> 1) continue;

        int tot = 0;
        FOR(j, 0, n) {
            int tmp = 0;
            FOR(k, 0, n) {
                if (flag[k]) {
                    hungary.add(j + 1, k + 1, -a[j][k]);
                    tmp = max(tmp, a[j][k]);
                }
                else {
                    hungary.add(j + 1, k + 1, -b[j][k]);
                    tmp = max(tmp, b[j][k]);
                }
            }
            tot += tmp;
        }
        if (tot <= -ans) continue;
        ans = min(ans, hungary.mincost());
    }
    printf("%d", -ans);
}

int main() {
    //ios_base::sync_with_stdio(0); cin.tie(NULL);
#ifdef _LOCAL_
    freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
    solve();
#ifdef _LOCAL_
    printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
    return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORd(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

#define maxn 21
#define inf 1000000000
struct Hungary {
    int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    bool used[maxn];
    void init(int _nx, int _ny) {
        nx = _nx; ny = _ny; memset(fx, 0, sizeof(fx)); memset(fy, 0, sizeof(fy)); memset(maty, 0, sizeof(maty)); memset(which, 0, sizeof(which));
        for (int i = 0; i <= nx; i++) for (int j = 0; j <= ny; j++) cost[i][j] = inf;
    }
    void add(int x, int y, int c) {cost[x][y] = c;}
    void update(int x) {
        //for (int x = 1; x <= nx; x++) {
        int y0 = 0; maty[0] = x;
        for (int y = 0; y <= ny; y++) {dist[y] = inf + 1; used[y] = false;}
        do {
            used[y0] = true;
            int x0 = maty[y0], delta = inf + 1, y1;
            for (int y = 1; y <= ny; y++) if (!used[y]) {
                int curdist = cost[x0][y] - fx[x0] - fy[y];
                if (curdist < dist[y]) {
                    dist[y] = curdist;
                    which[y] = y0;
                }
                if (dist[y] < delta) {
                    delta = dist[y];
                    y1 = y;
                }
            }
            for (int y = 0; y <= ny; y++) if (used[y]) {
                fx[maty[y]] += delta;
                fy[y] -= delta;
            }
            else dist[y] -= delta;
            y0 = y1;
        }
        while (maty[y0] != 0);
        do {
            int y1 = which[y0];
            maty[y0] = maty[y1];
            y0 = y1;
        }
        while (y0);
        //}
        //int ret = 0;
        //for (int y = 1; y <= ny; y++) {
        //int x = maty[y];
        //if (cost[x][y] < inf) ret += cost[x][y];
        //}
        //return ret;
    }
} hungary;

int n;
int a[maxn][maxn];
int b[maxn][maxn];
int ans = 0;

void go(int pos, int bit, int cnt) {

    Hungary tmp; //int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    tmp.nx = hungary.nx;
    tmp.ny = hungary.ny;
    FOR(i, 0, maxn) {
        tmp.fx[i] = hungary.fx[i];
        tmp.fy[i] = hungary.fy[i];
        tmp.maty[i] = hungary.maty[i];
        tmp.which[i] = hungary.which[i];
        tmp.dist[i] = hungary.dist[i];
        //FOR(j, 0, maxn) tmp.cost[i][j] = hungary.cost[i][j];
    }
    FOR(i, 0, n) if (bit == 1) hungary.add(pos + 1, i + 1, -a[pos][i]); else hungary.add(pos + 1, i + 1, -b[pos][i]);
    hungary.update(pos + 1);

    if (pos == n - 1) {
        if (cnt == (n >> 1)) {
            int tot = 0;
            for (int y = 1; y <= hungary.ny; y++) {
                int x = hungary.maty[y];
                if (hungary.cost[x][y] < inf) tot -= hungary.cost[x][y];
            }
            ans = max(ans, tot);
        }
        hungary.nx = tmp.nx;
    hungary.ny = tmp.ny;
    FOR(i, 0, maxn) {
        hungary.fx[i] = tmp.fx[i];
        hungary.fy[i] = tmp.fy[i];
        hungary.maty[i] = tmp.maty[i];
        hungary.which[i] = tmp.which[i];
        hungary.dist[i] = tmp.dist[i];
        //FOR(j, 0, maxn) hungary.cost[i][j] = tmp.cost[i][j];
    }
    FOR(i, 0, n) if (bit == 1) hungary.add(pos + 1, i + 1, inf); else hungary.add(pos + 1, i + 1, inf);
        return;
    }

    if (cnt + n - pos - 1 >= (n >> 1)) go(pos + 1, 0, cnt);
    if (cnt + 1 <= (n >> 1)) go(pos + 1, 1, cnt + 1);
    hungary.nx = tmp.nx;
    hungary.ny = tmp.ny;
    FOR(i, 0, maxn) {
        hungary.fx[i] = tmp.fx[i];
        hungary.fy[i] = tmp.fy[i];
        hungary.maty[i] = tmp.maty[i];
        hungary.which[i] = tmp.which[i];
        hungary.dist[i] = tmp.dist[i];
        //FOR(j, 0, maxn) hungary.cost[i][j] = tmp.cost[i][j];
    }
    FOR(i, 0, n) if (bit == 1) hungary.add(pos + 1, i + 1, inf); else hungary.add(pos + 1, i + 1, inf);
}

void solve() {
    scanf("%d", &n);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &a[i][j]);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &b[i][j]);
    hungary.init(n, n);
    FOR(i, 0, n) hungary.add(1, i + 1, -a[0][i]);
    //go(0, 0, 0);
    go(0, 1, 1);
    cout<<ans;
}

int main() {
    //ios_base::sync_with_stdio(0); cin.tie(NULL);
#ifdef _LOCAL_
    freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
    solve();
#ifdef _LOCAL_
    printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
    return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORd(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

#define maxn 21
#define inf 1000000000
struct Hungary {
    int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    bool used[maxn];
    void init(int _nx, int _ny) {
        nx = _nx; ny = _ny; memset(fx, 0, sizeof(fx)); memset(fy, 0, sizeof(fy)); memset(maty, 0, sizeof(maty)); memset(which, 0, sizeof(which));
        for (int i = 0; i <= nx; i++) for (int j = 0; j <= ny; j++) cost[i][j] = inf;
    }
    void add(int x, int y, int c) {cost[x][y] = c;}
    void update(int x) {
        //for (int x = 1; x <= nx; x++) {
        int y0 = 0; maty[0] = x;
        for (int y = 0; y <= ny; y++) {dist[y] = inf + 1; used[y] = false;}
        do {
            used[y0] = true;
            int x0 = maty[y0], delta = inf + 1, y1;
            for (int y = 1; y <= ny; y++) if (!used[y]) {
                int curdist = cost[x0][y] - fx[x0] - fy[y];
                if (curdist < dist[y]) {
                    dist[y] = curdist;
                    which[y] = y0;
                }
                if (dist[y] < delta) {
                    delta = dist[y];
                    y1 = y;
                }
            }
            for (int y = 0; y <= ny; y++) if (used[y]) {
                fx[maty[y]] += delta;
                fy[y] -= delta;
            }
            else dist[y] -= delta;
            y0 = y1;
        }
        while (maty[y0] != 0);
        do {
            int y1 = which[y0];
            maty[y0] = maty[y1];
            y0 = y1;
        }
        while (y0);
        //}
        //int ret = 0;
        //for (int y = 1; y <= ny; y++) {
        //int x = maty[y];
        //if (cost[x][y] < inf) ret += cost[x][y];
        //}
        //return ret;
    }
} hungary;

int n;
int a[maxn][maxn];
int b[maxn][maxn];
int ans = 0;

void go(int pos, int bit, int cnt) {
    if (pos == n) {
        if (cnt == (n >> 1)) {
            int tot = 0;
            for (int y = 1; y <= hungary.ny; y++) {
                int x = hungary.maty[y];
                if (hungary.cost[x][y] < inf) tot -= hungary.cost[x][y];
            }
            ans = max(ans, tot);
        }
        return;
    }
    Hungary tmp; //int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    tmp.nx = hungary.nx;
    tmp.ny = hungary.ny;
    FOR(i, 0, maxn) {
        tmp.fx[i] = hungary.fx[i];
        tmp.fy[i] = hungary.fy[i];
        tmp.maty[i] = hungary.maty[i];
        tmp.which[i] = hungary.which[i];
        tmp.dist[i] = hungary.dist[i];
        //FOR(j, 0, maxn) tmp.cost[i][j] = hungary.cost[i][j];
    }
    FOR(i, 0, n) if (bit) hungary.add(pos + 1, i + 1, -a[pos][i]); else hungary.add(pos + 1, i + 1, -b[pos][i]);
    hungary.update(pos + 1);
    if (cnt + n - pos - 1 >= (n >> 1)) go(pos + 1, 0, cnt);
    if (cnt + 1 <= (n >> 1)) go(pos + 1, 1, cnt + 1);
    hungary.nx = tmp.nx;
    hungary.ny = tmp.ny;
    FOR(i, 0, maxn) {
        hungary.fx[i] = tmp.fx[i];
        hungary.fy[i] = tmp.fy[i];
        hungary.maty[i] = tmp.maty[i];
        hungary.which[i] = tmp.which[i];
        hungary.dist[i] = tmp.dist[i];
        //FOR(j, 0, maxn) hungary.cost[i][j] = tmp.cost[i][j];
    }
    FOR(i, 0, n) if (bit == 1) hungary.add(pos + 1, i + 1, inf); else hungary.add(pos + 1, i + 1, inf);
}

void solve() {
    scanf("%d", &n);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &a[i][j]);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &b[i][j]);
    hungary.init(n, n);
    go(0, 0, 0);
    go(0, 1, 1);
    cout<<ans;
}

int main() {
    //ios_base::sync_with_stdio(0); cin.tie(NULL);
#ifdef _LOCAL_
    freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
    solve();
#ifdef _LOCAL_
    printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
    return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORd(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

#define maxn 21
#define inf 1000000000
struct Hungary {
    int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    bool used[maxn];
    void init(int _nx, int _ny) {
        nx = _nx; ny = _ny; memset(fx, 0, sizeof(fx)); memset(fy, 0, sizeof(fy));
        ms(maty, 0); ms(which, 0); ms(dist, 0);
        for (int i = 0; i <= nx; i++) for (int j = 0; j <= ny; j++) cost[i][j] = inf;
    }
    void add(int x, int y, int c) {cost[x][y] = c;}
    int mincost() {
        for (int x = 1; x <= nx; x++) {
            int y0 = 0; maty[0] = x;
            for (int y = 0; y <= ny; y++) {dist[y] = inf + 1; used[y] = false;}
            do {
                used[y0] = true;
                int x0 = maty[y0], delta = inf + 1, y1;
                for (int y = 1; y <= ny; y++) if (!used[y]) {
                    int curdist = cost[x0][y] - fx[x0] - fy[y];
                    if (curdist < dist[y]) {
                        dist[y] = curdist;
                        which[y] = y0;
                    }
                    if (dist[y] < delta) {
                        delta = dist[y];
                        y1 = y;
                    }
                }
                for (int y = 0; y <= ny; y++) if (used[y]) {
                    fx[maty[y]] += delta;
                    fy[y] -= delta;
                } else dist[y] -= delta;
                y0 = y1;
            }
            while (maty[y0] != 0);
            do {
                int y1 = which[y0];
                maty[y0] = maty[y1];
                y0 = y1;
            }
            while (y0);
        }
        int ret = 0;
        for (int y = 1; y <= ny; y++) {
            int x = maty[y];
            if (cost[x][y] < inf) ret += cost[x][y];
        }
        return ret;
    }
} hungary;

int n;
int a[maxn][maxn];
int b[maxn][maxn];

void solve() {
    scanf("%d", &n);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &a[i][j]);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &b[i][j]);
    int ans = 0;
    FOR(i, 0, 1 << n) {
        bool flag[maxn]; ms(flag, 0); int cnt = 0;
        FOR(j, 0, n) if (i & 1 << j) {flag[j] = 1; cnt++;}
        if (!(n & 1) && cnt != (n >> 1)) continue;
        if ((n & 1) && (cnt < (n >> 1) || cnt > (n >> 1) + 1)) continue;
        if (flag[0]) continue;
        hungary.init(n, n);
        FOR(j, 0, n) FOR(k, 0, n) {
            if (flag[k]) hungary.add(j + 1, k + 1, -a[j][k]);
            else hungary.add(j + 1, k + 1, -b[j][k]);
        }
        ans = min(ans, hungary.mincost());
        hungary.init(n, n);
        FOR(j, 0, n) FOR(k, 0, n) {
            if (flag[k]) hungary.add(j + 1, k + 1, -b[j][k]);
            else hungary.add(j + 1, k + 1, -a[j][k]);
        }
        ans = min(ans, hungary.mincost());
    }
    printf("%d", -ans);
}

int main() {
    //ios_base::sync_with_stdio(0); cin.tie(NULL);
#ifdef _LOCAL_
    freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
    solve();
#ifdef _LOCAL_
    printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
    return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORd(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

#define maxn 21
#define inf 1000000000
struct Hungary {
    int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    bool used[maxn];
    void init(int _nx, int _ny) {
        nx = _nx; ny = _ny; memset(fx, 0, sizeof(fx)); memset(fy, 0, sizeof(fy)); memset(maty, 0, sizeof(maty)); memset(which, 0, sizeof(which));
        for (int i = 0; i <= nx; i++) for (int j = 0; j <= ny; j++) cost[i][j] = inf;
    }
    void add(int x, int y, int c) {cost[x][y] = c;}
    void update(int x) {
        int y0 = 0; maty[0] = x;
        for (int y = 0; y <= ny; y++) {dist[y] = inf + 1; used[y] = false;}
        do {
            used[y0] = true;
            int x0 = maty[y0], delta = inf + 1, y1;
            for (int y = 1; y <= ny; y++) if (!used[y]) {
                int curdist = cost[x0][y] - fx[x0] - fy[y];
                if (curdist < dist[y]) {
                    dist[y] = curdist;
                    which[y] = y0;
                }
                if (dist[y] < delta) {
                    delta = dist[y];
                    y1 = y;
                }
            }
            for (int y = 0; y <= ny; y++) if (used[y]) {
                fx[maty[y]] += delta;
                fy[y] -= delta;
            }
            else dist[y] -= delta;
            y0 = y1;
        }
        while (maty[y0] != 0);
        do {
            int y1 = which[y0];
            maty[y0] = maty[y1];
            y0 = y1;
        }
        while (y0);
    }
    Hungary operator = (const Hungary& h) {
        nx = h.nx; ny = h.ny;
        FOR(i, 0, maxn) {
            fx[i] = h.fx[i];
            fy[i] = h.fy[i];
            //maty[i] = h.maty[i];
            which[i] = h.which[i];
            //dist[i] = h.dist[i];
        }
        return h;
    }
} hungary;

int n;
int a[maxn][maxn];
int b[maxn][maxn];
int ans;

void go(int pos, int bit, int cnt) {
    Hungary tmp = hungary;
    FOR(i, 0, n) if (bit == 1) hungary.add(pos + 1, i + 1, -a[pos][i]); else hungary.add(pos + 1, i + 1, -b[pos][i]);
    hungary.update(pos + 1);
    if (pos == n - 1) {
        if (cnt == (n >> 1)) {
            int tot = 0;
            for (int y = 1; y <= hungary.ny; y++) {
                int x = hungary.maty[y];
                if (hungary.cost[x][y] < inf) tot -= hungary.cost[x][y];
            }
            ans = max(ans, tot);
        }
        hungary = tmp; FOR(i, 0, n) hungary.add(pos + 1, i + 1, inf);
        return;
    }
    if (cnt + n - pos - 1 >= (n >> 1)) go(pos + 1, 0, cnt);
    if (cnt + 1 <= (n >> 1)) go(pos + 1, 1, cnt + 1);
    hungary = tmp;
    FOR(i, 0, n) hungary.add(pos + 1, i + 1, inf);
}

void solve() {
    scanf("%d", &n);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &a[i][j]);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &b[i][j]);
    ans = 0;
    hungary.init(n, n); FOR(i, 0, n) hungary.add(1, i + 1, -a[0][i]);
    go(0, 0, 0);
    hungary.init(n, n); FOR(i, 0, n) hungary.add( 1, i + 1, -b[0][i]);
    go(0, 1, 1);
    printf("%d", ans);
}

int main() {
    //ios_base::sync_with_stdio(0); cin.tie(NULL);
#ifdef _LOCAL_
    freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
    solve();
#ifdef _LOCAL_
    printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
    return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORd(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

#define maxn 21
#define inf 1000000000
struct Hungary {
    int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    bool used[maxn];
    void init(int _nx, int _ny) {
        nx = _nx; ny = _ny; memset(fx, 0, sizeof(fx)); memset(fy, 0, sizeof(fy)); memset(maty, 0, sizeof(maty)); memset(which, 0, sizeof(which)); memset(dist, 0, sizeof(dist));
        for (int i = 0; i <= nx; i++) for (int j = 0; j <= ny; j++) cost[i][j] = inf;
    }
    void add(int x, int y, int c) {cost[x][y] = c;}
    int mincost() {
        for (int x = 1; x <= nx; x++) {
            int y0 = 0; maty[0] = x;
            for (int y = 0; y <= ny; y++) {dist[y] = inf + 1; used[y] = false;}
            do {
                used[y0] = true;
                int x0 = maty[y0], delta = inf + 1, y1;
                for (int y = 1; y <= ny; y++) if (!used[y]) {
                    int curdist = cost[x0][y] - fx[x0] - fy[y];
                    if (curdist < dist[y]) {
                        dist[y] = curdist;
                        which[y] = y0;
                    }
                    if (dist[y] < delta) {
                        delta = dist[y];
                        y1 = y;
                    }
                }
                for (int y = 0; y <= ny; y++) if (used[y]) {
                    fx[maty[y]] += delta;
                    fy[y] -= delta;
                } else dist[y] -= delta;
                y0 = y1;
            }
            while (maty[y0] != 0);
            do {
                int y1 = which[y0];
                maty[y0] = maty[y1];
                y0 = y1;
            }
            while (y0);
        }
        int ret = 0;
        for (int y = 1; y <= ny; y++) {
            int x = maty[y];
            if (cost[x][y] < inf) ret += cost[x][y];
        }
        return ret;
    }
} hungary;

int n;
int a[maxn][maxn];
int b[maxn][maxn];

void solve() {
    scanf("%d", &n);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &a[i][j]);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &b[i][j]);
    int ans = 0;
    FORd(i, 1 << n, 0) {
        if (clock() > 1900) break;
        bool flag[maxn]; ms(flag, 0); int cnt = 0;
        FOR(j, 0, n) if (i & 1 << j) {flag[j] = 1; cnt++;}
        if (cnt != n >> 1) continue;
        hungary.init(n, n);
        FOR(j, 0, n) FOR(k, 0, n) {
            if (flag[k]) hungary.add(j + 1, k + 1, -a[j][k]);
            else hungary.add(j + 1, k + 1, -b[j][k]);
        }
        ans = min(ans, hungary.mincost());
    }
    printf("%d", -ans);
}

int main() {
    //ios_base::sync_with_stdio(0); cin.tie(NULL);
#ifdef _LOCAL_
    freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
    solve();
#ifdef _LOCAL_
    printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
    return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORd(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

#define maxn 21
#define inf 1000000000
struct Hungary {
    int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    bool used[maxn];
    void init(int _nx, int _ny) {
        nx = _nx; ny = _ny; memset(fx, 0, sizeof(fx)); memset(fy, 0, sizeof(fy)); memset(maty, 0, sizeof(maty)); memset(which, 0, sizeof(which));
        for (int i = 0; i <= nx; i++) for (int j = 0; j <= ny; j++) cost[i][j] = inf;
    }
    void add(int x, int y, int c) {cost[x][y] = c;}
    void update(int x) {
        //for (int x = 1; x <= nx; x++) {
        int y0 = 0; maty[0] = x;
        for (int y = 0; y <= ny; y++) {dist[y] = inf + 1; used[y] = false;}
        do {
            used[y0] = true;
            int x0 = maty[y0], delta = inf + 1, y1;
            for (int y = 1; y <= ny; y++) if (!used[y]) {
                int curdist = cost[x0][y] - fx[x0] - fy[y];
                if (curdist < dist[y]) {
                    dist[y] = curdist;
                    which[y] = y0;
                }
                if (dist[y] < delta) {
                    delta = dist[y];
                    y1 = y;
                }
            }
            for (int y = 0; y <= ny; y++) if (used[y]) {
                fx[maty[y]] += delta;
                fy[y] -= delta;
            }
            else dist[y] -= delta;
            y0 = y1;
        }
        while (maty[y0] != 0);
        do {
            int y1 = which[y0];
            maty[y0] = maty[y1];
            y0 = y1;
        }
        while (y0);
        //}
        //int ret = 0;
        //for (int y = 1; y <= ny; y++) {
        //int x = maty[y];
        //if (cost[x][y] < inf) ret += cost[x][y];
        //}
        //return ret;
    }
} hungary;

int n;
int a[maxn][maxn];
int b[maxn][maxn];
int ans = 0;

void go(int pos, int bit, int cnt) {
    if (pos == n) {
        if (cnt == (n >> 1)) {
            int tot = 0;
            for (int y = 1; y <= hungary.ny; y++) {
                int x = hungary.maty[y];
                if (hungary.cost[x][y] < inf) tot -= hungary.cost[x][y];
            }
            ans = max(ans, tot);
        }
        return;
    }
    Hungary tmp; //int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    tmp.nx = hungary.nx;
    tmp.ny = hungary.ny;
    FOR(i, 0, maxn) {
        tmp.fx[i] = hungary.fx[i];
        tmp.fy[i] = hungary.fy[i];
        tmp.maty[i] = hungary.maty[i];
        tmp.which[i] = hungary.which[i];
        tmp.dist[i] = hungary.dist[i];
        //FOR(j, 0, maxn) tmp.cost[i][j] = hungary.cost[i][j];
    }
    FOR(i, 0, n) if (bit) hungary.add(pos + 1, i + 1, -b[pos][i]); else hungary.add(pos + 1, i + 1, -a[pos][i]);
    hungary.update(pos + 1);
    go(pos + 1, 0, cnt);
    if (cnt + 1 <= (n >> 1)) go(pos + 1, 1, cnt + 1);
    hungary.nx = tmp.nx;
    hungary.ny = tmp.ny;
    FOR(i, 0, maxn) {
        hungary.fx[i] = tmp.fx[i];
        hungary.fy[i] = tmp.fy[i];
        hungary.maty[i] = tmp.maty[i];
        hungary.which[i] = tmp.which[i];
        hungary.dist[i] = tmp.dist[i];
        //FOR(j, 0, maxn) hungary.cost[i][j] = tmp.cost[i][j];
    }
    FOR(i, 0, n) if (bit == 1) hungary.add(pos + 1, i + 1, inf); else hungary.add(pos + 1, i + 1, inf);
}

void solve() {
    scanf("%d", &n);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &a[i][j]);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &b[i][j]);
    hungary.init(n, n);
    go(0, 0, 0);
    go(0, 1, 1);
    cout<<ans;
}

int main() {
    //ios_base::sync_with_stdio(0); cin.tie(NULL);
#ifdef _LOCAL_
    freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
    solve();
#ifdef _LOCAL_
    printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
    return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORd(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

#define maxn 21
#define inf 1000000000
struct Hungary {
    int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    bool used[maxn];
    void init(int _nx, int _ny) {
        nx = _nx; ny = _ny; memset(fx, 0, sizeof(fx)); memset(fy, 0, sizeof(fy)); memset(maty, 0, sizeof(maty)); memset(which, 0, sizeof(which));
        for (int i = 0; i <= nx; i++) for (int j = 0; j <= ny; j++) cost[i][j] = inf;
    }
    void add(int x, int y, int c) {cost[x][y] = c;}
    void update(int x) {
        int y0 = 0; maty[0] = x;
        for (int y = 0; y <= ny; y++) {dist[y] = inf + 1; used[y] = false;}
        do {
            used[y0] = true;
            int x0 = maty[y0], delta = inf + 1, y1;
            for (int y = 1; y <= ny; y++) if (!used[y]) {
                int curdist = cost[x0][y] - fx[x0] - fy[y];
                if (curdist < dist[y]) {
                    dist[y] = curdist;
                    which[y] = y0;
                }
                if (dist[y] < delta) {
                    delta = dist[y];
                    y1 = y;
                }
            }
            for (int y = 0; y <= ny; y++) if (used[y]) {
                fx[maty[y]] += delta;
                fy[y] -= delta;
            }
            else dist[y] -= delta;
            y0 = y1;
        }
        while (maty[y0] != 0);
        do {
            int y1 = which[y0];
            maty[y0] = maty[y1];
            y0 = y1;
        }
        while (y0);
    }
    Hungary operator = (const Hungary& h) {
        nx = h.nx; ny = h.ny;
        FOR(i, 0, maxn) {
            fx[i] = h.fx[i];
            fy[i] = h.fy[i];
            maty[i] = h.maty[i];
            which[i] = h.which[i];
            dist[i] = h.dist[i];
        }
        return h;
    }
} hungary;

int n;
int a[maxn][maxn];
int b[maxn][maxn];
int ans;

void go(int pos, int bit, int cnt) {
    Hungary tmp = hungary;
    FOR(i, 0, n) if (bit == 1) hungary.add(pos + 1, i + 1, -a[pos][i]); else hungary.add(pos + 1, i + 1, -b[pos][i]);
    hungary.update(pos + 1);
    if (pos == n - 1) {
        if (cnt == (n >> 1)) {
            int tot = 0;
            for (int y = 1; y <= hungary.ny; y++) {
                int x = hungary.maty[y];
                if (hungary.cost[x][y] < inf) tot -= hungary.cost[x][y];
            }
            ans = max(ans, tot);
        }
        hungary = tmp; FOR(i, 0, n) hungary.add(pos + 1, i + 1, inf);
        return;
    }
    if (cnt + n - pos - 1 >= (n >> 1)) go(pos + 1, 0, cnt);
    if (cnt + 1 <= (n >> 1)) go(pos + 1, 1, cnt + 1);
    hungary = tmp;
    FOR(i, 0, n) hungary.add(pos + 1, i + 1, inf);
}

void solve() {
    scanf("%d", &n);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &a[i][j]);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &b[i][j]);
    ans = 0;
    hungary.init(n, n);
    FOR(i, 0, n) hungary.add(1, i + 1, -a[0][i]);
    go(0, 0, 0);
    hungary.init(n, n);
    FOR(i, 0, n) hungary.add( 1, i + 1, -b[0][i]);
    go(0, 1, 1);
    printf("%d", ans);
}

int main() {
    //ios_base::sync_with_stdio(0); cin.tie(NULL);
#ifdef _LOCAL_
    freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
    solve();
#ifdef _LOCAL_
    printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
    return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORd(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

#define maxn 21
#define inf 1000000000
struct Hungary {
    int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    bool used[maxn];
    void init(int _nx, int _ny) {
        nx = _nx; ny = _ny; memset(fx, 0, sizeof(fx)); memset(fy, 0, sizeof(fy)); memset(maty, 0, sizeof(maty)); memset(which, 0, sizeof(which));
        for (int i = 0; i <= nx; i++) for (int j = 0; j <= ny; j++) cost[i][j] = inf;
    }
    void add(int x, int y, int c) {cost[x][y] = c;}
    void update(int x) {
        //for (int x = 1; x <= nx; x++) {
        int y0 = 0; maty[0] = x;
        for (int y = 0; y <= ny; y++) {dist[y] = inf + 1; used[y] = false;}
        do {
            used[y0] = true;
            int x0 = maty[y0], delta = inf + 1, y1;
            for (int y = 1; y <= ny; y++) if (!used[y]) {
                int curdist = cost[x0][y] - fx[x0] - fy[y];
                if (curdist < dist[y]) {
                    dist[y] = curdist;
                    which[y] = y0;
                }
                if (dist[y] < delta) {
                    delta = dist[y];
                    y1 = y;
                }
            }
            for (int y = 0; y <= ny; y++) if (used[y]) {
                fx[maty[y]] += delta;
                fy[y] -= delta;
            }
            else dist[y] -= delta;
            y0 = y1;
        }
        while (maty[y0] != 0);
        do {
            int y1 = which[y0];
            maty[y0] = maty[y1];
            y0 = y1;
        }
        while (y0);
        //}
        //int ret = 0;
        //for (int y = 1; y <= ny; y++) {
        //int x = maty[y];
        //if (cost[x][y] < inf) ret += cost[x][y];
        //}
        //return ret;
    }
} hungary;

int n;
int a[maxn][maxn];
int b[maxn][maxn];
int ans = 0;

void go(int pos, int bit, int cnt) {
    if (cnt > (n >> 1)) return;
    if (pos == n) {
        if (cnt == (n >> 1)) {
            int tot = 0;
            for (int y = 1; y <= hungary.ny; y++) {
                int x = hungary.maty[y];
                if (hungary.cost[x][y] < inf) tot -= hungary.cost[x][y];
            }
            ans = max(ans, tot);
        }
        return;
    }
    Hungary tmp; //int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    tmp.nx = hungary.nx;
    tmp.ny = hungary.ny;
    FOR(i, 0, maxn) {
        tmp.fx[i] = hungary.fx[i];
        tmp.fy[i] = hungary.fy[i];
        tmp.maty[i] = hungary.maty[i];
        tmp.which[i] = hungary.which[i];
        tmp.dist[i] = hungary.dist[i];
        //FOR(j, 0, maxn) tmp.cost[i][j] = hungary.cost[i][j];
    }
    FOR(i, 0, n) if (bit == 1) hungary.add(pos + 1, i + 1, -a[pos][i]); else hungary.add(pos + 1, i + 1, -b[pos][i]);
    hungary.update(pos + 1);
    go(pos + 1, 0, cnt);
    if (cnt + 1 <= (n >> 1))
    go(pos + 1, 1, cnt + 1);
    hungary.nx = tmp.nx;
    hungary.ny = tmp.ny;
    FOR(i, 0, maxn) {
        hungary.fx[i] = tmp.fx[i];
        hungary.fy[i] = tmp.fy[i];
        hungary.maty[i] = tmp.maty[i];
        hungary.which[i] = tmp.which[i];
        hungary.dist[i] = tmp.dist[i];
        //FOR(j, 0, maxn) hungary.cost[i][j] = tmp.cost[i][j];
    }
    FOR(i, 0, n) if (bit == 1) hungary.add(pos + 1, i + 1, inf); else hungary.add(pos + 1, i + 1, inf);
}

void solve() {
    scanf("%d", &n);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &a[i][j]);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &b[i][j]);
    hungary.init(n, n);
    go(0, 0, 0);
    go(0, 1, 1);
    cout<<ans;
}

int main() {
    //ios_base::sync_with_stdio(0); cin.tie(NULL);
#ifdef _LOCAL_
    freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
    solve();
#ifdef _LOCAL_
    printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
    return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORd(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

#define maxn 21
#define inf 1000000000
struct Hungary {
    int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    bool used[maxn];
    void init(int _nx, int _ny) {
        nx = _nx; ny = _ny; memset(fx, 0, sizeof(fx)); memset(fy, 0, sizeof(fy)); memset(maty, 0, sizeof(maty)); memset(which, 0, sizeof(which)); memset(dist, 0, sizeof(dist));
        for (int i = 0; i <= nx; i++) for (int j = 0; j <= ny; j++) cost[i][j] = inf;
    }
    void add(int x, int y, int c) {cost[x][y] = c;}
    int mincost() {
        for (int x = 1; x <= nx; x++) {
            int y0 = 0; maty[0] = x;
            for (int y = 0; y <= ny; y++) {dist[y] = inf + 1; used[y] = false;}
            do {
                used[y0] = true;
                int x0 = maty[y0], delta = inf + 1, y1;
                for (int y = 1; y <= ny; y++) if (!used[y]) {
                    int curdist = cost[x0][y] - fx[x0] - fy[y];
                    if (curdist < dist[y]) {
                        dist[y] = curdist;
                        which[y] = y0;
                    }
                    if (dist[y] < delta) {
                        delta = dist[y];
                        y1 = y;
                    }
                }
                for (int y = 0; y <= ny; y++) if (used[y]) {
                    fx[maty[y]] += delta;
                    fy[y] -= delta;
                } else dist[y] -= delta;
                y0 = y1;
            }
            while (maty[y0] != 0);
            do {
                int y1 = which[y0];
                maty[y0] = maty[y1];
                y0 = y1;
            }
            while (y0);
        }
        int ret = 0;
        for (int y = 1; y <= ny; y++) {
            int x = maty[y];
            if (cost[x][y] < inf) ret += cost[x][y];
        }
        return ret;
    }
} hungary;

int n;
int a[maxn][maxn];
int b[maxn][maxn];

void solve() {
    scanf("%d", &n);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &a[i][j]);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &b[i][j]);
    int ans = 0;
    FOR(i, 0, 1 << n) {
        if (clock() > 1900) break;
        bool flag[maxn]; ms(flag, 0); int cnt = 0;
        FOR(j, 0, n) if (i & 1 << j) {flag[j] = 1; cnt++;}
        if (cnt != n >> 1) continue;
        hungary.init(n, n);
        FOR(j, 0, n) FOR(k, 0, n) {
            if (flag[k]) hungary.add(j + 1, k + 1, -a[j][k]);
            else hungary.add(j + 1, k + 1, -b[j][k]);
        }
        ans = min(ans, hungary.mincost());
    }
    printf("%d", -ans);
}

int main() {
    //ios_base::sync_with_stdio(0); cin.tie(NULL);
#ifdef _LOCAL_
    freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
    solve();
#ifdef _LOCAL_
    printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
    return 0;
}


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

#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORd(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define sz(a) int((a).size())
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
ll gcd(ll a, ll b) {ll r; while (b != 0) {r = a % b; a = b; b = r;} return a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

#define maxn 21
#define inf 1000000000
struct Hungary {
    int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    bool used[maxn];
    void init(int _nx, int _ny) {
        nx = _nx; ny = _ny; memset(fx, 0, sizeof(fx)); memset(fy, 0, sizeof(fy)); memset(maty, 0, sizeof(maty)); memset(which, 0, sizeof(which));
        for (int i = 0; i <= nx; i++) for (int j = 0; j <= ny; j++) cost[i][j] = inf;
    }
    void add(int x, int y, int c) {cost[x][y] = c;}
    void update(int x) {
        //for (int x = 1; x <= nx; x++) {
        int y0 = 0; maty[0] = x;
        for (int y = 0; y <= ny; y++) {dist[y] = inf + 1; used[y] = false;}
        do {
            used[y0] = true;
            int x0 = maty[y0], delta = inf + 1, y1;
            for (int y = 1; y <= ny; y++) if (!used[y]) {
                int curdist = cost[x0][y] - fx[x0] - fy[y];
                if (curdist < dist[y]) {
                    dist[y] = curdist;
                    which[y] = y0;
                }
                if (dist[y] < delta) {
                    delta = dist[y];
                    y1 = y;
                }
            }
            for (int y = 0; y <= ny; y++) if (used[y]) {
                fx[maty[y]] += delta;
                fy[y] -= delta;
            }
            else dist[y] -= delta;
            y0 = y1;
        }
        while (maty[y0] != 0);
        do {
            int y1 = which[y0];
            maty[y0] = maty[y1];
            y0 = y1;
        }
        while (y0);
        //}
        //int ret = 0;
        //for (int y = 1; y <= ny; y++) {
        //int x = maty[y];
        //if (cost[x][y] < inf) ret += cost[x][y];
        //}
        //return ret;
    }
} hungary;

int n;
int a[maxn][maxn];
int b[maxn][maxn];
int ans = 0;

void go(int pos, int bit, int cnt, Hungary hungary) {
    //cout<<pos<<" "<<bit<<" "<<cnt<<"\n";
    if (pos == n) {
        if (cnt == (n >> 1)) {
            int tot = 0;
            for (int y = 1; y <= hungary.ny; y++) {
                int x = hungary.maty[y];
                if (hungary.cost[x][y] < inf) tot -= hungary.cost[x][y];
            }
            ans = max(ans, tot);
        }
        return;
    }
    if (bit == 1) FOR(j, 0, n) hungary.add(pos + 1, j + 1, -a[pos][j]);
    else FOR(j, 0, n) hungary.add(pos + 1, j + 1, -b[pos][j]);
    hungary.update(pos + 1);
    //return;
    Hungary tmp; //int nx, ny, cost[maxn][maxn], fx[maxn], fy[maxn], maty[maxn], which[maxn], dist[maxn];
    tmp.nx = hungary.nx;
    tmp.ny = hungary.ny;
    FOR(i, 0, maxn) {
        tmp.fx[i] = hungary.fx[i];
        tmp.fy[i] = hungary.fy[i];
        tmp.maty[i] = hungary.maty[i];
        tmp.which[i] = hungary.which[i];
        tmp.dist[i] = hungary.dist[i];
        FOR(j, 0, maxn) tmp.cost[i][j] = hungary.cost[i][j];
    }
    go(pos + 1, 0, cnt, tmp);
    go(pos + 1, 1, cnt + 1, tmp);
}

void solve() {
    scanf("%d", &n);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &a[i][j]);
    FOR(i, 0, n) FOR(j, 0, n) scanf("%d", &b[i][j]);
    hungary.init(n, n);
    go(0, 0, 0, hungary);
    go(0, 1, 1, hungary);
    cout<<ans;
}

int main() {
    //ios_base::sync_with_stdio(0); cin.tie(NULL);
#ifdef _LOCAL_
    freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
    solve();
#ifdef _LOCAL_
    printf("\nTime elapsed: %dms", 1000 * clock() / CLOCKS_PER_SEC);
#endif
    return 0;
}