#include <math.h>
#include <string.h>
#include <algorithm>
#include <map>
#include <deque>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
const int N=1150050;
map<long long,long long> m;
long long sum,mark[N];
struct cow{
long long x,id;
}c[1150050];
deque<long long> q;
bool cmp(cow a,cow b){
return a.x<b.x;
}
int main(int argc, char** argv) {
long long n;
scanf("%lld",&n);
for(int i=1;i<=n;i++){
scanf("%lld %lld",&c[i].x,&c[i].id);
if(m[c[i].id]==0){
sum++;
m[c[i].id]=1;
}
}
sort(c+1,c+n+1,cmp);
long long f=0,ans=1000000000;
for(int i=1;i<=n;i++)
{
if(mark[c[i].id]==0)f++;
mark[c[i].id]++;
q.push_front(i);
while(q.size()&&mark[c[q.back()].id]>1)mark[c[q.back()].id]--,q.pop_back();
if(f==sum) ans=min(ans,c[q.front()].x-c[q.back()].x);
}
printf("%lld",ans);
return 0;
}