#include<bits/stdc++.h>
using namespace std;
struct cpp1
{
int x;
int id;
}cow[50005];
unordered_map<int,int> cnt;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
int len = 50005;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> cow[i].x >> cow[i].id;
cnt[cow[i].id]++;
}
sort(cow + 1, cow + n + 1, [](const cpp1& a, const cpp1& b) {
return a.x < b.x;
});
deque<int> window;
for (int i = 1; i <= n; i++) {
cnt[cow[i].id]--;
window.push_back(cow[i].id);
while (!window.empty() && cnt[window.front()] > 0) {
cnt[window.front()]--;
window.pop_front();
}
if (window.size() >= 2) {
len = min(len, cow[i].x - cow[i - window.size() + 1].x);
}
}
cout << len;
return 0;
}