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

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

    int n,m, br=0;;
    cin>>n>>m;
    vector<int> a(n), b(m);
    vector<tuple<int, int, int>> rs;
    for(int i=0;i<n;i++)
        cin>>a[i]>>a[i];
    for(int i=0;i<m;i++)
        cin>>b[i]>>b[i];
    for(int i=0;i<m;i++)
    {
        int t=b[i];
        for(int j=0;t && j<n;j++)
        {
            if(a[j]<=t)
            {
                if(a[j])
                rs.push_back(make_tuple(j,i,a[j]));
                t-=a[j];
                br+=a[j];
                a[j]=0;
            }
            else
            {
                if(t)
                rs.push_back(make_tuple(j,i,t));
                a[j]-=t;
                br+=t;
                t=0;
            }
        }
    }
    cout<<br<<'\n'<<rs.size()<<'\n';
    for(auto z:rs)
        cout<<get<0>(z)+1<<' '<<get<1>(z)+1<<' '<<get<2>(z)<<'\n';
    return 0;
}