#include <bits/stdc++.h>
using namespace std;
long long b[50009];
long long ans = 9999999999;
int w,t,i,n,s = 1;
int v[50009];
struct node {
long long x;
long long y;
}a[50009],p[50009];
bool cmp(node k,node z) {
return k.x < z.x;
}
int main() {
scanf("%d",&n);
for (i = 1; i <= n; i++) {
scanf("%lld %lld",&a[i].x,&a[i].y);
b[i] = a[i].y;
}
sort(a+1,a+n+1,cmp);
sort(b+1,b+n+1);
int num = unique(b+1,b+n+1) - b - 1;
for (i = 1; i <= n; i++) a[i].y = lower_bound(b+1,b+num+1,a[i].y) - b;
t = w = i = 1 ;
p[t].x = a[1].x; p[t].y = a[1].y;
v[p[t].y]++;
for (int i = 2; i <= n; i++) {
w++, p[w].x = a[i].x, v[a[i].y]++, p[w].y = a[i].y;
if (v[p[w].y] == 1) s++;
while (v[p[t].y] > 1) {
v[p[t].y]--,t++;
}
if (s == num) {
long long L = p[w].x - p[t].x; ans = min(ans,L);
}
}
printf("%lld",ans);
return 0;
}
/**************************************************************
Problem: 1769
User: admin
Language: C++
Result: Accepted
Time:7 ms
Memory:4228 kb
****************************************************************/