#include <bits/stdc++.h>

using namespace std;
struct polje
{
    int x;
    int y;
    long long vr;
};
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m;
    cin>>n>>m;
    long long k=0,v=0;
    vector<long long> a(n);
    vector<long long> b(n);
    vector<long long> c(m);
    vector<long long> d(m);
    vector<polje> rez(1000000);
    bool ab=true;
    int pb=0;
    for(int i=0;i<n;i++)
    {
        cin>>a[i]>>b[i];
        if(a[i]!=b[i]) ab=false;
        k+=b[i];
    }
    int nule=0;
    int zm=0;
    for(int i=0;i<m;i++)
    {
        cin>>c[i]>>d[i];
        if(d[i]==0) nule++;
        v+=d[i];
        zm+=c[i];
    }
    if(n==m)
    {
        cout<<k<<endl;
        cout<<n<<endl;
        for(int i=0;i<n;i++) cout<<i+1<<" "<<i+1<<" "<<b[i]<<endl;
    }
    else if(n==1)
    {
        if(k>=v)
        {
            cout<<v<<endl;
            cout<<m-nule<<endl;
            for(int i=0;i<m;i++) cout<<1<<" "<<i+1<<" "<<d[i]<<endl;

        }
        else
        {
            cout<<k<<endl;
            for(int i=0;i<m;i++)
            {
                if(zm==b[0])
                {
                    if(b[0]!=0)
                    {
                        rez[pb].x=1;
                        rez[pb].y=i+1;
                        rez[pb].vr=c[i];
                        pb++;
                        b[0]-=c[i];
                        zm-=c[i];
                    }

                }
                else
                {
                    if(b[0]!=0)
                    {
                        rez[pb].x=1;
                        rez[pb].y=i+1;
                        long long t=0;
                        if(b[0]-zm>=d[i]-c[i])
                        {
                            t+=d[i];
                            rez[pb].vr=t;
                            pb++;
                            b[0]-=d[i];
                            zm-=c[i];
                        }
                        else
                        {
                            rez[pb].vr=c[i]+b[0]-zm;
                            pb++;
                            zm-=c[i];
                            b[0]=zm;
                        }
                    }

                }
            }
            cout<<pb<<endl;
            for(int i=0;i<pb;i++) cout<<rez[i].x<<" "<<rez[i].y<<" "<<rez[i].vr<<endl;
        }
    }
    else if(ab)
    {
        if(v>=k)
        {
            int l=0;
            cout<<k<<endl;
            for(int i=0;i<n;i++)
            {
                for(int j=l;j<m;j++)
                {
                    if(a[i]<c[j])
                    {
                        if(a[i]!=0)
                        {
                            rez[pb].x=i+1;
                            rez[pb].y=j+1;
                            rez[pb].vr=a[i];

                            c[j]-=a[i];
                            a[i]=0;
                            pb++;
                            j=m;
                        }

                    }
                    else if(a[i]==b[j])
                    {
                        if(a[i]!=0)
                        {
                            rez[pb].x=i+1;
                            rez[pb].y=j+1;
                            rez[pb].vr=a[i];
                            a[i]=0;
                            c[j]=0;
                            pb++;
                            l++;
                            j=m;
                        }

                    }
                    else
                    {
                        if(c[i]!=0)
                        {
                            rez[pb].x=i+1;
                            rez[pb].y=j+1;
                            rez[pb].vr=c[i];
                            a[i]-=c[j];
                            c[j]=0;
                            pb++;
                            l++;
                        }


                    }
                }
            }
            for(int i=0;i<pb;i++) cout<<rez[i].x<<" "<<rez[i].y<<" "<<rez[i].vr<<endl;
        }
    }
}