#include<bits/stdc++.h>
using namespace std;
struct work{
long long time,s;
}w[10005];
long long t,n,ans;
bool cmp(work a,work b){
return a.s<b.s;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>w[i].time>>w[i].s;
}
sort(w+1,w+n+1,cmp);
ans=t=w[1].s-w[1].time;
for(int i=1;i<=n;i++){
if(t+w[i].time>w[i].s){
int a=t=w[i].time-w[i].s;
if(ans>=a){
ans-=a;
t-=a;
}else{
printf("-1");
return 0;
}
}
t+=w[i].time;
}
printf("%lld",ans);
return 0;
}