#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
ll a[50050], b[50050], c[50050], d[50050];
vector<pair<pair<ll, ll>, pair<ll, bool> > >mins, makss;
vector<pair<pair<ll, ll>, ll> >rez;
bool st1=true, st5=true;
int main()
{
    ll n, m;
    cin>>n>>m;
    if(n!=m)st1=false;
    for(int i=1; i<=n; i++)
    {
        cin>>a[i]>>b[i];
        if(a[i]>0)st5=false;
    }
    for(int i=1; i<=m; i++)
    {
        cin>>c[i]>>d[i];
        if(c[i]>0)st5=false;
        if(c[i]<=a[i] or d[i]<=b[i])st1=false;
    }

    if(st5)
    {
        ll xx=1, yy=1, brk=0;
        while(xx<=n and yy<=m)
        {
            if(b[xx]<=d[yy])
            {
                makss.push_back({{xx, yy}, {b[xx], false}});
                brk+=b[xx];
                d[yy]-=b[xx];
                b[xx]=0; xx++;
            }
            else
            {
                makss.push_back({{xx, yy}, {d[yy], false}});
                brk+=d[yy];
                b[xx]-=d[yy];
                d[yy]=0; yy++;
            }
        }
        cout<<brk<<"\n"<<makss.size()<<"\n";
        for(int i=0; i<makss.size(); i++)cout<<makss[i].first.first<<" "<<makss[i].first.second<<" "<<makss[i].second.first<<"\n";
        return 0;
    }
    if(st1)
    {
        ll brk=0;
        for(int i=1; i<=n; i++)
        {
            rez.push_back({{i, i}, b[i]});
            brk+=b[i];
        }
        cout<<brk<<"\n"<<n<<"\n";
        for(int i=0; i<rez.size(); i++)if(rez[i].second!=0)cout<<rez[i].first.first<<" "<<rez[i].first.second<<" "<<rez[i].second<<"\n";
        return 0;
    }
    ll xx=1, yy=1;
    while(xx<=n and yy<=m)
    {
        if(a[xx]<=c[yy])
        {
            mins.push_back({{xx, yy}, {a[xx], false}});
            c[yy]-=a[xx]; d[yy]-=a[xx];
            b[xx]-=a[xx]; a[xx]=0;
            xx++;
        }
        else
        {
            mins.push_back({{xx, yy}, {c[yy], false}});
            a[xx]-=c[yy]; b[xx]-=c[yy];
            d[yy]-=c[yy]; c[yy]=0;
            yy++;
        }
    }
    if(xx<=n)
    {
        ll zz=1;
        while(xx<=n)
        {
            if(a[xx]<=d[zz])
            {
                mins.push_back({{xx, zz}, {a[xx], false}});
                d[zz]-=a[xx]; b[xx]-=a[xx];
                a[xx]=0; xx++;
            }
            else
            {
                mins.push_back({{xx, zz}, {d[zz], false}});
                a[xx]-=d[zz]; b[xx]-=d[zz];
                d[zz]=0; zz++;
            }
        }
    }
    if(yy<=m)
    {
        ll zz=1;
        while(yy<=m)
        {
            if(c[yy]<=b[zz])
            {
                mins.push_back({{zz, yy}, {c[yy], false}});
                b[zz]-=c[yy]; d[yy]-=c[yy];
                c[yy]=0; yy++;
            }
            else
            {
                mins.push_back({{zz, yy}, {b[zz], false}});
                c[yy]-=b[zz]; d[yy]-=b[zz];
                b[zz]=0; zz++;
            }
        }
    }

    xx=1; yy=1;
    while(xx<=n and yy<=m)
    {
        if(b[xx]<=d[yy])
        {
            makss.push_back({{xx, yy}, {b[xx], false}});
            d[yy]-=b[xx];
            b[xx]=0; xx++;
        }
        else
        {
            makss.push_back({{xx, yy}, {d[yy], false}});
            b[xx]-=d[yy];
            d[yy]=0; yy++;
        }
    }

    sort(mins.begin(), mins.end());
    sort(makss.begin(), makss.end());
    ll ii=0, jj=0;
    ll k=0, znj=0;
    while(ii<mins.size() and jj<makss.size())
    {
        if(mins[ii].first.first==makss[jj].first.first and mins[ii].first.second==makss[jj].first.second)
        {
            rez.push_back({{mins[ii].first.first, mins[ii].first.second},mins[ii].second.first+makss[jj].second.first});
            k+=mins[ii].second.first+makss[jj].second.first;
            if(mins[ii].second.first+makss[jj].second.first!=0)znj++;
            mins[ii].second.second=true;
            makss[jj].second.second=true;
            jj++; ii++;
        }
        else if(mins[ii].first.first==makss[jj].first.first and mins[ii].first.second<makss[jj].first.second)ii++;
        else if(mins[ii].first.first==makss[jj].first.first and mins[ii].first.second>makss[jj].first.second)jj++;
        else if(mins[ii].first.first<makss[jj].first.first)ii++;
        else if(mins[ii].first.first>makss[jj].first.first)jj++;
    }
    for(int i=0; i<mins.size(); i++)
    {
        if(!mins[i].second.second)
        {rez.push_back({{mins[i].first.first, mins[i].first.second},mins[i].second.first});
        k+=mins[i].second.first;
        if(mins[i].second.first!=0)znj++;}
    }
    for(int i=0; i<makss.size(); i++)
    {
        if(!makss[i].second.second)
        {rez.push_back({{makss[i].first.first, makss[i].first.second},makss[i].second.first});
        k+=makss[i].second.first;
        if(makss[i].second.first!=0)znj++;}
    }
    cout<<k<<"\n"<<znj<<"\n";
    for(int i=0; i<rez.size(); i++)if(rez[i].second!=0)cout<<rez[i].first.first<<" "<<rez[i].first.second<<" "<<rez[i].second<<"\n";
    return 0;
}