#include <bits/stdc++.h>
// #include <atcoder/all>
// #include "icld.cpp"
using namespace std;
using ll = long long int;
using vi = vector<int>;
using si = set<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using ss = string;
using db = double;
template<typename T> using minpq = priority_queue <T,vector<T>,greater<T>>;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
#define V vector
#define P pair<int,int>
#define PLL pair<ll,ll>
#define rep(i,s,n) for(int i=(s);i<(int)(n);i++)
#define rev(i,s,n) for(int i=(s);i>=(int)(n);i--)
#define reciv(v,n) vi (v)((n)); rep(i,0,(n))cin>>v[i]
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ci(x) cin >> x
#define cii(x) ll x;cin >> x
#define cci(x,y) ll x,y;cin >> x >> y
#define co(x) cout << x << endl
#define pb push_back
#define eb emplace_back
#define rz resize
#define pu push
#define sz(x) int(x.size())
#define vij v[i][j]
// ll p = 1e9+7;
// ll p = 998244353;
// n do -> n*pi/180
#define yn cout<<"Yes"<<endl;else cout<<"No"<<endl
#define YN cout<<"YES"<<endl;else cout<<"NO"<<endl
template<class T>void chmax(T &x,T y){x=max(x,y);}
template<class T>void chmin(T &x,T y){x=min(x,y);}

int main(){
  cci(h,w);
  vvi v(h+2,vi(w+2,-1));
  rep(i,1,h+1){
      rep(j,1,w+1)vij=0;
  }
  cci(n,m);

  vvi lt(n,vi(2)),bl(m,vi(2));
  rep(i,0,n){
      rep(j,0,2)ci(lt[i][j]);
  }
  rep(i,0,m){
      rep(j,0,2)ci(bl[i][j]);
      v[bl[i][0]][bl[i][1]]=-1;
  }

    vvi tt=v,yk=v;
  rep(i,0,n){
      int x=lt[i][0];
      int y=lt[i][1];

      int nx=x,ny=y;
      tt[x][y]=yk[x][y]=1;
      while(tt[nx-1][y]==0)tt[--nx][y]=1;
      while(yk[x][ny-1]==0)yk[x][--ny]=1;

      nx=x,ny=y;
      while(tt[nx+1][y]==0)tt[++nx][y]=1;
      while(yk[x][ny+1]==0)yk[x][++ny]=1;
    }
/*
  rep(i,2,h+1){
      rep(j,1,w+1){
          if(yk[i][j]==-1)continue;
          chmax(yk[i][j],yk[i-1][j]);
      }
  }

  rep(i,2,w+1){
      rep(j,1,h+1){
          if(tt[j][i]==-1)continue;
          chmax(tt[j][i],tt[j][i-1]);
      }
  }
*/
    int ans=0;
    rep(i,1,h+1){
        rep(j,1,w+1){
            if(yk[i][j]==1 or tt[i][j]==1){
                // cout<<1<<" ";
                ans++;
            }
            // else if(vij==-1)cout<<'x'<<" ";
            // else cout<<0<<" ";
        }
        // co("");
    }

    co(ans);
}
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i < (n); ++i)
#define ll long long
#define pll pair<ll, ll>
using namespace std;
    
    
long memo[1001001][2];
vector<pll> colors;
    
// direction -> 1 = right; 0 = left;
long dp(long unsigned color, int direction){
    if (color >= colors.size()) return 0;
    
    if(memo[color][direction] != -1)
        return memo[color][direction];
    
    ll l = colors[color].first;
    ll r = colors[color].second;
    ll dist = r-l;
    
    ll prev_pos = direction ? 
        colors[color-1].second:
        colors[color-1].first;
    
    memo[color][direction] = min(
        dp(color+1, 0) + abs(prev_pos - r) + dist,
        dp(color+1, 1) + abs(prev_pos - l) + dist
    );
    
    return memo[color][direction];
}
    
int main(){
    int n;
    cin >> n;
    
    ll INF = LONG_LONG_MAX;
    vector<ll> l(n, INF);
    vector<ll> r(n, -INF);
    
    ll x, c;
    rep(i, n){
        cin >> x >> c;
        --c;
        l[c] = min(l[c], x);
        r[c] = max(r[c], x);
    }
    
    colors.emplace_back(0,0);
    rep(i, n){
        if (l[i] != INF)
            colors.emplace_back(l[i], r[i]);
    }
    colors.emplace_back(0,0);
    
    memset(memo, -1, sizeof(memo));
    cout << dp(1, 0) << endl;
    return 0;
}