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

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


    unsigned long long pr=1000000001;
    int n,m;
    cin>>n>>m;
    //vector<vector<int>> a(n);
    unordered_map<unsigned long long, int> mps;
    vector<tuple<int, int, int>> ro;
    /*for(auto &v:a)
        for(int i=0;i<m;i++)
            v.push_back(0);*/
    for(int i=0;i<n;i++)
    {
        int l,r;
        cin>>l>>r;
        ro.push_back(make_tuple(r,l,i));
    }
    sort(ro.begin(), ro.end());
    vector<pair<int, int>> ko(m);
    for(int i=0;i<m;i++)
        cin>>ko[i].first>>ko[i].second;
    int br=0;

    for(int i=0;i<m;i++)
    {
        int j=0;
        int t=ko[i].first;
        while(t>0 && j<n)
        {
            if(get<0>(ro[j])<=t)
            {
                t-=get<0>(ro[j]);
                //a[get<2>(ro[j])][i]+=get<0>(ro[j]);
                mps[(unsigned long long)get<2>(ro[j])*pr+i]+=get<0>(ro[j]);
                br+=get<0>(ro[j]);
                get<0>(ro[j])=0;
            }
            else
            {
                get<0>(ro[j])-=t;
                mps[(unsigned long long)get<2>(ro[j])*pr+i]+=t;
                br+=t;
                t=0;
            }
            j++;
        }
    }
    for(int i=0;i<m;i++)
    {
        int j=0;
        int t=ko[i].second-ko[i].first;
        while(t>0 && j<n)
        {
            if(get<0>(ro[j])<=t)
            {
                t-=get<0>(ro[j]);
                mps[(unsigned long long)get<2>(ro[j])*pr+i]+=get<0>(ro[j]);
                br+=get<0>(ro[j]);
                get<0>(ro[j])=0;
            }
            else
            {
                get<0>(ro[j])-=t;
                mps[(unsigned long long)get<2>(ro[j])*pr+i]+=t;
                br+=t;
                t=0;
            }
            j++;
        }
    }
    /*int k=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(a[i][j])
                k++;
    cout<<br<<'\n'<<k<<'\n';

    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(a[i][j])
                cout<<i+1<<' '<<j+1<<' '<<a[i][j]<<'\n';*/
    cout<<br<<'\n'<<mps.size()<<'\n';
    for(auto z:mps)
    {
        cout<<z.first/pr+1<<' '<<z.first%pr+1<<' '<<z.second<<'\n';
    }
    return 0;
}