#include <bits/stdc++.h>

using namespace std;

#define ll long long

#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))

bool prime(ll a) { if (a==1) return 0; for (int i=2;i<=round(sqrt(a));++i) if (a%i==0) return 0; return 1; }
ll min(ll a,int b) { if (a<b) return a; return b; }
ll min(int a,ll b) { if (a<b) return a; return b; }
ll max(ll a,int b) { if (a>b) return a; return b; }
ll max(int a,ll b) { if (a>b) return a; return b; }

ll sumarray(ll i,ll n, ll redovi[][2]){
    ll res=0;
    for(ll j =0;j<n;++j)
    {
        res+=redovi[j][1];
    }
    return res;
}

bool hasNoZero(ll n,ll m,ll **filled)
{
    bool hasnozero=true;
    for(ll i = 0; i<n ; ++i)
            {
                for(ll j = 0; j<m ; ++j)
                {
                    if(filled[i][j]==0)
                        hasnozero=false;
                }
            }
    return hasnozero;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    // INPUT:

    ll n,m;
    cin>>n>>m;
    ll redovi[n][2];
    ll kolone[m][2];
    for(ll i=0;i<n;++i)
        cin>>kolone[i][0]>>kolone[i][1];
    for(ll i=0;i<m;++i)
        cin>>redovi[i][0]>>kolone[i][1];

    // CHECK CASE:

    bool acIs0 = true;
    for(ll i=0;i<n;++i)
    {
        if(kolone[i][0]!=0)
            acIs0=false;
    }
    for(ll i=0;i<m;++i)
    {
        if(redovi[i][0]!=0)
            acIs0=false;
    }

    bool Nis1 = ((n==1)?true:false);

    // SOLUTION:
    ll res=0;
    if(acIs0)
    {
        ll filled[n][m];
        ll biggest=0;
        ll maxpair[2];
        maxpair[0]=0;
        maxpair[1]=0;
        ll redoviZbirovi[n];
        for(ll i = 0; i<n; ++i)
            redoviZbirovi[i]=sumarray(i,m,kolone);
        ll koloneZbirovi[m];
        for(ll i = 0; i<m; ++i)
            koloneZbirovi[i]=sumarray(i,n,redovi);
        //findmax:
        while(hasNoZero(n,m,filled))
        {
            for(ll i = 0; i<n ; ++i)
            {
                for(ll j = 0; j<m ; ++j)
                {
                    if((redovi[i][1]+kolone[j][1])>biggest && filled[i][j]==0)
                    {
                        biggest=(redovi[i][1]+kolone[j][1]);
                        maxpair[0]=i;
                        maxpair[1]=j;
                    }

                }
            }
            if(redoviZbirovi[maxpair[0]>0 && redoviKolone[maxpair[1]>0 && (filled[maxpair[0]][maxpair[1]]==0))
                filled[maxpair[0]][maxpair[1]]=biggest;
            else
                filled[maxpair[0]][maxpair[1]]=-1;


        }
        for(ll i = 0; i<n ; ++i)
            {
                for(ll j = 0; j<m ; ++j)
                {
                    res+=filled[i][j];

                }
            }
        cout<<res;
    }

    return 0;
}