#include <bits/stdc++.h>

using namespace std;

vector<int> a;
vector<int> b;
vector<int> c;
vector<int> d;

vector<int> solx;
vector<int> soly;
vector<int> soln;

int main()
{
    int n;
    int m;

    cin >> n;
    cin >> m;

    long long asum = 0;
    long long bsum = 0;
    long long csum = 0;
    long long dsum = 0;

    for(int i = 0; i < n; i++)
    {
        int au;
        int bu;

        cin >> au;
        cin >> bu;

        asum += au;
        bsum += bu;

        a.push_back(au);
        b.push_back(bu);
    }

    for(int i = 0; i < m; i++)
    {
        int cu;
        int du;

        cin >> cu;
        cin >> du;

        csum += cu;
        dsum += du;

        c.push_back(cu);
        d.push_back(du);
    }

    long long dg = max(asum, csum);
    long long gg = min(bsum, dsum);

    int numr = 0;
    int k = 0;

    if(dsum > bsum)
    {
        // po redovima
        long long btr = 0;
        while(btr < csum)
        {
            btr += b[numr];
            numr++;
        }

        long long raz = csum - btr;

        int i = 0;
        int j = 0;

        while(i < numr && j < m)
        {
            if(b[i] != 0)
            {
                if(b[i] > c[j])
                {
                    solx.push_back(i);
                    soly.push_back(j);
                    soln.push_back(c[j]);

                    b[i] -= c[j];
                    d[j] -= c[j];
                    c[j] = 0;
                    j++;
                }
                else if(b[i] < c[j])
                {
                    solx.push_back(i);
                    soly.push_back(j);
                    soln.push_back(c[j]);

                    c[j] -= b[i];
                    d[j] -= b[i];
                    b[i] = 0;
                    i++;
                }
                else
                {
                    solx.push_back(i);
                    soly.push_back(j);
                    soln.push_back(c[j]);

                    d[j] -= c[j];
                    b[i] == 0;
                    c[j] == 0;

                    j++;
                    i++;
                }
            }
            else
                i++;
        }

        i = numr - 1;
        j = 0;

        while(i < n)
        {
            if(b[i] != 0)
            {
                    if(b[i] > d[j])
                {
                    solx.push_back(i);
                    soly.push_back(j);
                    soln.push_back(d[j]);

                    b[i] -= d[j];
                    d[j] = 0;
                    j++;
                }
                else if(b[i] < d[j])
                {
                    solx.push_back(i);
                    soly.push_back(j);
                    soln.push_back(d[j]);

                    d[j] -= b[i];
                    b[i] = 0;
                    i++;
                }
                else
                {
                    solx.push_back(i);
                    soly.push_back(j);
                    soln.push_back(d[j]);

                    b[i] == 0;
                    d[j] == 0;

                    j++;
                    i++;
                }
            }
            else
                i++;
        }


    }
    else
    {
        // po kolonama
        long long dtr = 0;
        while(dtr < asum)
        {
            dtr += d[numr];
            numr++;
        }

        long long raz = asum - dtr;

        int i = 0;
        int j = 0;

        while(i < numr && j < n)
        {
            if(d[i] != 0)
            {
                if(d[i] > a[j])
                {
                    solx.push_back(i);
                    soly.push_back(j);
                    soln.push_back(a[j]);

                    d[i] -= a[j];
                    b[j] -= a[j];
                    a[j] = 0;
                    j++;
                }
                else if(d[i] < a[j])
                {
                    solx.push_back(i);
                    soly.push_back(j);
                    soln.push_back(a[j]);

                    a[j] -= d[i];
                    b[j] -= d[i];
                    d[i] = 0;
                    i++;
                }
                else
                {
                    solx.push_back(i);
                    soly.push_back(j);
                    soln.push_back(a[j]);

                    b[j] -= a[j];
                    d[i] == 0;
                    a[j] == 0;

                    j++;
                    i++;
                }
            }
            else
                i++;
        }

        i = numr - 1;
        j = 0;

        while(i < m)
        {
            if(d[i] != 0)
            {
                    if(d[i] > b[j])
                {
                    solx.push_back(i);
                    soly.push_back(j);
                    soln.push_back(b[j]);

                    d[i] -= b[j];
                    b[j] = 0;
                    j++;
                }
                else if(d[i] < b[j])
                {
                    solx.push_back(i);
                    soly.push_back(j);
                    soln.push_back(b[j]);

                    b[j] -= d[i];
                    d[i] = 0;
                    i++;
                }
                else
                {
                    solx.push_back(i);
                    soly.push_back(j);
                    soln.push_back(b[j]);

                    d[i] == 0;
                    b[j] == 0;

                    j++;
                    i++;
                }
            }
            else
                i++;
        }


    }

    cout << gg << '\n' << solx.size() << '\n';

    for(int i = 0; i < solx.size(); i++)
    {
        cout << soly[i] + 1 << " " << solx[i] + 1 << " " << soln[i] << '\n';
    }


    return 0;
}