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

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

    int n,m;
    int a[50001],b[50001],c[50001],d[50001];
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        cin>>a[i]>>b[i];
    }
    for(int j=0;j<m;j++)
    {
        cin>>c[j]>>d[j];
    }
    if(n==1)
    {
        int niz[50001];
        long long z=0;
        for(int i=0;i<m;i++)
        {
            niz[i]=d[i];
            z=z+d[i];
            //cout<<niz[i]<<" ";
        }
        //cout<<z<<endl;

        if(b[0]>=z)
        {
            cout<<z<<endl;
            cout<<m<<endl;
            for(int i=0;i<m;i++)
            {
                cout<<1<<" "<<i+1<<" "<<niz[i]<<endl;
            }
            return 0;
        }

        for(int i=0;i<m;i++)
        {
            niz[i]=niz[i]-min((long long)(d[i]-c[i]),(z-b[0]));
            z=z-min((long long)(d[i]-c[i]),(z-b[0]));
            //cout<<d[i]<<" "<<c[i]<<" "<<z<<" "<<b[0]<<endl;
            //cout<<niz[i]<<endl;
        }
        //cout<<endl;
        int bpp=0;

        for(int i=0;i<m;i++)
        {
            if(niz[i]!=0)
            {
                bpp=bpp+1;
            }
        }

        cout<<z<<endl;
        cout<<bpp<<endl;
        for(int i=0;i<m;i++)
        {
            //cout<<niz[i]<<endl;
            if(niz[i]!=0)
            {
                cout<<1<<" "<<i+1<<" "<<niz[i]<<endl;
            }
        }
    }

    if(n==m)
    {
        int z=0;
        for(int i=0;i<n;i++)
        {
            z=z+b[i];
        }
        cout<<z<<endl;
        cout<<n<<endl;
        for(int i=0;i<n;i++)
        {
            cout<<i+1<<" "<<i+1<<" "<<b[i]<<endl;
        }
    }

    return 0;
}