#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];
    }
    vector<int> xk,yk,vr;
    int zn[50001],zm[50001];
    for(int i=0;i<50001;i++)
    {
        zn[i]=0;
        zm[i]=0;
    }
    int i=0;
    int j=0;
    while(i<n and j<m)
    {
        if(b[i]-zn[i]<d[j]-zm[j])
        {
            xk.push_back(j);
            yk.push_back(i);
            vr.push_back(b[i]-zn[i]);
            zm[j]=zm[j]+b[i]-zn[i];
            i=i+1;
        }
        else if(b[i]-zn[i]>d[j]-zm[j])
        {
            xk.push_back(j);
            yk.push_back(i);
            vr.push_back(d[j]-zm[j]);
            zn[i]=zn[i]+d[j]-zm[j];
            j=j+1;
        }
        else
        {
            xk.push_back(j);
            yk.push_back(i);
            vr.push_back(b[i]-zn[i]);
            i=i+1;
            j=j+1;
        }
    }
    long long zbir=0;
    int dv=xk.size();

    for(int p=0;p<dv;p++)
    {
        zbir=zbir+(long long)vr[p];
    }

    cout<<(long long)zbir<<endl<<dv<<endl;

    for(int p=0;p<dv;p++)
    {
        cout<<yk[p]+1<<" "<<xk[p]+1<<" "<<vr[p]<<endl;
    }

    return 0;
}