# 【BZOJ4152】[AMPPZ2014]The Captain

5
2 2
1 1
4 5
7 1
6 7

## Sample Output

2

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <algorithm>
#include <utility>
#define mp(A,B) make_pair(A,B)
using namespace std;
const int maxn=200010;
typedef long long ll;
struct node
{
int x,y,org;
}p[maxn];
int n,cnt;
bool vis[maxn];
ll dis[maxn];
priority_queue<pair<ll,int> > q;
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
}
inline int rd()
{
int ret=0,f=1;	char gc=nc();
while(gc<'0'||gc>'9')	{if(gc=='-')	f=-f;	gc=nc();}
while(gc>='0'&&gc<='9')	ret=ret*10+gc-'0',gc=nc();
return ret*f;
}
bool cmpx(const node &a,const node &b)
{
return a.x<b.x;
}
bool cmpy(const node &a,const node &b)
{
return a.y<b.y;
}
inline void add(int a,int b,int c)
{
}
int main()
{
n=rd();
register int i,u;
for(i=1;i<=n;i++)	p[i].x=rd(),p[i].y=rd(),p[i].org=i;
sort(p+1,p+n+1,cmpx);
sort(p+1,p+n+1,cmpy);
memset(dis,0x3f,sizeof(dis));
dis[1]=0,q.push(mp(0,1));
while(!q.empty())
{
u=q.top().second,q.pop();
if(vis[u])	continue;
if(u==n)
{
printf("%lld\n",dis[n]);
return 0;
}
vis[u]=1;
}