#include<iostream> #include<vector> #include<set> #include<algorithm> using namespace std; bool cmp(pair<int,int> a,pair<int,int> b){ return a.first<b.first; } main() { int n; cin>>n; vector<pair<int,int>> data(n,pair<int,int>()); for(auto& current:data){ cin>>current.first>>current.second; } sort(data.begin(),data.end(),cmp); multiset<int> l; l.insert(data[0].first); l.insert(data[0].second); for(int i=1;i<data.size();i++){ pair<int,int> current = data[i]; auto iter = l.end(); iter--; if(*iter<current.first){ l.insert(current.first); l.insert(current.second); } else{ if(*iter>current.second){ continue; } l.erase(iter); l.insert(current.second); } } int sum=0; int num1=0,num2=0; for(auto iter=l.begin();iter!=l.end();iter++){ if(!num1){ num1=*iter; } else if(!num2){ num2=*iter; sum += num2-num1; num2=0; num1=0; } } cout<<sum; return 0; }