#include <iostream>
#include <bits/stdc++.h>
using namespace std;




int main()
{
    int n,m;
    cin>>n>>m;
    int ae[n+1];
    int av[n+1];
    int be[m+1];
    int bv[m+1];
    int o1=0,o2=0;
    for(int i=1;i<=n;i++)
        {
            cin>>ae[i];
            cin>>av[i];
            o1=o1+av[i];
        }
    for(int i=1;i<=m;i++)
        {
            cin>>be[i];
            cin>>bv[i];
            o2=o2+bv[i];
        }

    if(o1<o2)
        cout<<o1<<endl;
        else
            cout<<o2<<endl;

    int fold[n+1][m+1];
     for(int i=0;i<=n;i++)
    {
        for(int j=0;j<=m;j++)
           fold[i][j]=0;

    }

    int i=1;
    int j=1;
    while(i!=n+1 && j!=m+1)
    {
        if(av[i]==0 )
        {
            for(int k=1;k<n+1;k++)
                fold[i][k]=0;
        }
        if(bv[j]==0 )
        {
            for(int k=1;k<m+1;k++)
                fold[k][j]=0;
        }
        j++;
        i++;
    }
    i=1;
    j=1;


    /*if(n>m)
    {
    */
        while(i<=n)
        {
            while(j<=m)
            {
                int sor=0;
                int os=0;
                if(av[i]<bv[i])
                 {
                     int ii=i;
                     int jj=i;
                     while(ii!=0)
                     {
                       sor=sor+fold[ii][j];
                       ii--;
                       os=os+fold[i][jj];
                       jj--;
                     }
                  fold[i][j]=abs(av[i]-sor-os);
                 }
                if(av[i]>=bv[i])
                 {
                     int jj=i;
                     int ii=i;
                     while(jj!=0)
                      {
                        os=os+fold[i][jj];
                        jj--;
                        sor=sor+fold[ii][j];
                       ii--;
                      }
                  fold[i][j]=abs(bv[i]-os-sor);
                }
              j++;
            }
            j=1;
            i++;
        }
    /*}
    else
    {
        while(j<=m)
        {
            cucc(int ae[i], int av[i], int be[j], int bv[j],int i, int j, int fold[n+1][m+1])
            j++;
        }
    }
    */
    //cout << "Hello world!" << endl;
   /* for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
            cout<<fold[i][j]<<" ";
        cout<<endl;
    }*/
    return 0;
}