#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m;
    cin>>n>>m;
    int br1=0,br2=0;
    int a[n],b[n];
    int c[m],d[m];
    for(int i=0;i<n;i++)
    {
        cin>>a[i]>>b[i];
        br1+=b[i];
    }
    for(int i=0;i<m;i++)
    {
        cin>>c[i]>>d[i];
        br2+=d[i];
    }
    int uporedi=min(br1,br2);
    pair<int,int> polja[max(n,m)];
    int br[max(n,m)];
    int s=0;    
    cout<<uporedi<<endl;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(uporedi<=0 || b[i]<=0)
                {
                    break;
                }
                else
                {
                    int mini=min(b[i],d[j]);
                    polja[s].first=i+1;
                    polja[s].second=j+1;
                    br[s]=mini;
                    uporedi-=mini;
                    b[i]-=mini;
                    d[j]-=mini;
                    if(br[s]!=0) s++;
                }
            }
            if(uporedi<=0) break;
        }
        cout<<s<<endl;
        for(int i=0;i<s;i++)
        {
            if(br[i]!=0 && polja[i].first!=0)
            {
                cout<<polja[i].first<<" "<<polja[i].second<<" "<<br[i]<<endl;
            }
        }
    return 0;
}