#include <bits/stdc++.h>

using namespace std;

int main()
{   ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m;
    cin>>n>>m;
    vector<long long>a(n),b(n),c(m),d(m);
    for(int i=0;i<n;i++)
        cin>>a[i]>>b[i];
    for(int i=0;i<m;i++)
        cin>>c[i]>>d[i];
    bool jednakiab=true;
    for(int i=0;i<n;i++)
        if(a[i]!=b[i])
            jednakiab=false;
    bool jednakicd=true;
    for(int i=0;i<m;i++)
        if(c[i]!=d[i])
            jednakicd=false;
    long long sum=0;
    if(n==1)
    {
        for(int i=0;i<m;i++)
            sum+=d[i];
        long long res=min(sum,b[0]);
        cout << res << endl;
        cout << m << endl;
        for(int i=0;i<m;i++)
        {
            long long vr=0;
            if(sum-res>0)
            {
                vr=min(d[i]-c[i],sum-res);
                sum-=vr;
            }
            cout << 1 << " " << i+1 << " " << d[i]-vr << endl;
        }
        return 0;
    }
    if(n==m)
    {
        for(int i=0;i<n;i++)
            sum+=b[i];
        cout << sum << endl;
        cout << n << endl;
        for(int i=0;i<n;i++)
            cout << i+1 << " " << i+1 << " " << b[i] << endl;
        return 0;
    }
    vector<vector<long long>> v(n,vector<long long> (m));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            v[i][j]=min(b[i],d[j]);
            b[i]-=v[i][j];
            d[i]-=v[i][j];
            sum+=v[i][j];
        }
    }
    cout << sum << endl;
    cout << n*m << endl;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            cout << i+1 << " " << j+1 << " " << v[i][j] << endl;
    return 0;
}