#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

struct Person {
    unsigned height;
    unsigned head;
    unsigned id;
    bool female = true;
};

bool heightCompRev(const Person& a, const Person& b) {
    if (a.height > b.height) return true;
    if (a.height < b.height) return false;
    if (a.head > b.head) return true;
    if (a.head < b.head) return false;
    if (a.id < b.id) return true;
    return false;
}

bool headComp(const Person& a, const Person& b) {
    if (a.head < b.head) return true;
    if (a.head > b.head) return false;
    if (a.height > b.height) return true;
    if (a.height < b.height) return false;
    if (a.id < b.id) return true;
    return false;
}

int main() {
    int n;
    cin>>n;
    vector<Person> people;
    people.resize(2*n);
    set<Person,bool(*)(const Person&,const Person&)> men(headComp);
   
    for (int i=0; i<2*n; i++) {
        cin>>people[i].height;
        cin>>people[i].head;
        people[i].id = i;
        people[i].female = (i>=n);
    }

    sort(people.begin(),people.end(),heightCompRev);
    unsigned long long result = 0;
    for (const Person &p : people) {
        if (p.female) {
            auto matching_man = men.upper_bound(p);
            if (matching_man!=men.end()) {
                result += (matching_man->height - p.height) + (matching_man->head - p.head);
                men.erase(matching_man);
            } else {
                cout<<"NIE"<<endl;
                return 0;
            }
        } else {
            men.insert(p);
        }
    }
    cout <<result<<endl;
}