#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<long long> row;
    vector<long long> col;
    vector<long long> val;

    long long pR = 0, pC = 0;

    while(pR != n && pC != m)
    {
        long long 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("%lld\n", val.size());
    for(long long i = 0; i < val.size(); i++)
    {
        printf("%lld %lld %lld\n", row[i]+1, col[i]+1, val[i]);
    }
    return;
}

int main()
{
    long long n, m;

    long long sumR = 0;//  %d
    long long sumC = 0;

    scanf("%lld%lld", &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)
    {
        long long 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)
    {
        long long 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("%lld\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



*/