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

#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
typedef long long ll;
typedef pair<int,int> pii;

const int maxn=2000005;
int n,sz;
int a[maxn],b[maxn],col[maxn],deg[maxn],tot[maxn];
vector<int>ver[maxn],num;
bool ok;

namespace Dsu{
    int f[maxn],s[maxn];
    void init(){
        rep(i,sz)f[i]=i,s[i]=1;
    }
    int find(int x){
        return f[x]==x?x:f[x]=find(f[x]);
    }
    void merge(int x,int y){
        x=find(x);y=find(y);
        if(x==y)return;
        f[x]=y;
        s[y]+=s[x];
    }
}

void color(int x,int val){
    if(col[x]>=0){
        if(col[x]!=val)ok=false;
        return;
    }
    col[x]=val;
    rep(i,int(ver[val].size())){
        int y=ver[val][i];
        int nxtval=a[y]==val?b[y]:a[y];
        color(y,nxtval);
    }
}

bool check(int lim){
    Dsu::init();
    rep(i,n)col[i]=-1;
    ok=true;
    rep(i,n){
        if(a[i]>lim)return false;
        if(b[i]>lim)color(i,a[i]);
    }
    if(!ok)return false;
    rep(i,sz)deg[i]=tot[i]=0;
    rep(i,n)if(col[i]<0){
        Dsu::merge(a[i],b[i]);
        deg[a[i]]++;
        deg[b[i]]++;
    }
    rep(i,sz)tot[Dsu::find(i)]+=deg[i];
    rep(i,sz)if(Dsu::find(i)==i){
        if(tot[i]/2>Dsu::s[i])return false;
    }
    return true;
}

int main(){
    scanf("%d",&n);
    rep(i,n){
        scanf("%d%d",&a[i],&b[i]);
        num.pb(a[i]);num.pb(b[i]);
    }
    sort(num.begin(),num.end());
    num.resize(unique(num.begin(),num.end())-num.begin());
    sz=int(num.size());
    rep(i,n){
        a[i]=lower_bound(num.begin(),num.end(),a[i])-num.begin();
        b[i]=lower_bound(num.begin(),num.end(),b[i])-num.begin();
        ver[a[i]].pb(i);
        ver[b[i]].pb(i);
    }
    int l=-1,r=sz;
    while(r-l>1){
        int mid=l+r>>1;
        if(check(mid))r=mid;
        else l=mid;
    }
    if(r==sz)puts("-1");
    else printf("%d\n",num[r-1]);
    return 0;
}