#include<stdio.h>
#include<vector>
#include<algorithm>

using namespace std;

const int MAX = 50010;

long long r[MAX];
long long R[MAX];
long long c[MAX];
long long C[MAX];



void resi(long long* R, long long* C, int n, int m)
{
    vector<int> row;
    vector<int> col;
    vector<int> val;

    int pR = 0, pC = 0;

    while(pR != n && pC != m)
    {
        int najmanja = min(R[pR], C[pC]);
        val.push_back(najmanja);
        col.push_back(pC);
        row.push_back(pR);

        R[pR] -= najmanja;
        C[pC] -= najmanja;
        if(R[pR] == 0) pR++;
        if(C[pC] == 0) pC++;
    }

    printf("%d\n", val.size());
    for(int i = 0; i < val.size(); i++)
    {
        printf("%d %d %d\n", col[i]+1, row[i]+1, val[i]);
    }
    return;
}

int main()
{
    int n, m;

    long long sumR = 0;
    long long sumC = 0;

    scanf("%d%d", &n, &m);

    for(long long i = 0; i < n; i++)
    {
       scanf("%lld%lld", &r[i], &R[i]);
       sumR += R[i];
    }


    for(long long i = 0; i < m; i++)
    {
       scanf("%lld%lld", &c[i], &C[i]);
        sumC += C[i];
    }

    //ispisi(R, C);
    if (sumC > sumR)
    {
        int i = 0;
        while(sumC > sumR)
        {
            long long sumCNew = sumC - C[i] + max(c[i], sumR + C[i] - sumC);

            C[i] = max(c[i], C[i] - (sumC - sumCNew));
            i++;
            sumC = sumCNew;
        }
    }

    if (sumR > sumC)
    {
        int i = 0;
        while(sumR > sumC)
        {
            long long sumRNew = sumR - R[i] + max(r[i], sumC + R[i] - sumR);

            R[i] = max(r[i], R[i] - (sumR - sumRNew));
            i++;
            sumR = sumRNew;
        }
    }

    printf("%d\n", sumR);
    resi(R, C, n, m);
    /*for(int i = 0; i < n; i++)
        printf("%d ", R[i]);

    printf("\n");
    for(int i = 0; i < n; i++)
        printf("%d ", C[i]);
    printf("\n");*/



    return 0;
}

/*
5 5

2 4
0 5
3 9
5 6
0 1

2 2
7 9
3 6
7 9
0 8



*/