#include <bits/stdc++.h>
#define il inline
#define ll long long
const int N=2e5+5;

int n,s[2],o,v[N]; ll ans; struct node{int v,id,o;}d[N];

il bool cmp(node x,node y){return x.v<y.v;}

int main()
{
	scanf("%d",&n); int i;
	for (i=1; i<=n; i++)
		scanf("%d%d",&d[i+i-1].v,&d[i+i].v),
		d[i+i-1].id=d[i+i].id=i,d[i+i].o=1;
	std::sort(d+1,d+n+n+1,cmp);
	
	for (i=1; i<=n; i++) ans+=(ll)d[i].v,s[d[i].o]++,o|=++v[d[i].id]>1;
	if (o||s[0]==n||s[1]==n) return printf("%lld",ans),0;
	ans+=(ll)d[n+1].v-d[n].v,s[d[n].o]--,s[d[n+1].o]++;
	if (d[n].id!=d[n+1].id||s[0]==n||s[1]==n) return printf("%lld",ans),0;
	printf("%lld",ans+(ll)std::min(d[n+2].v-d[n+1].v,d[n].v-d[n-1].v));
	
	return 0;
}