#include <stdio.h>
#include <stdbool.h>

int main()
{
    int a[50000], b[50000], c[50000], d[50000];
    int mtr[500][500];
    int n, m, i, j, k, l, suma_reda, krozn;

    bool puna_kolona[50000], pun_red[50000], dodavanje;

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

    for (i = 0; i < n; i++)
        scanf("%d%d", a + i, b + i);
    
    for (i = 0; i < m; i++)
        scanf("%d%d", c + i, d + i);

    for (i = 0; i < n; i++)
    {
        for (j = 0, suma_reda = 0, krozn = b[i] / n; j < m; j++)
        {
            mtr[i][j] = krozn + 1;
            suma_reda += krozn + 1;

            if (suma_reda > b[i]) {
                mtr[i][j] -= suma_reda - b[i];
                break;
            }
        }

        pun_red[i] = true;
    }

    for (i = 0; i < m; i++)
    {
        for (j = 0, suma_reda = 0; j < n; j++)  
        {
            suma_reda += mtr[j][i];
        }

        if (suma_reda == d[i])
            puna_kolona[i] = true;
        
        if (suma_reda > d[i]) 
        {
            for (j = 0; j < n; j++) 
            {
                if (mtr[j][i] - suma_reda + d[i] >= 0) {
                    mtr[j][i] -= suma_reda - d[i];
                    puna_kolona[i] = true;
                    pun_red[j] = false;
                    break;
                }
            }   
        }
    }

    for (i = 0, suma_reda = 0; i < m; i++)
    {
        if (puna_kolona[i])  continue;

        for (j = 0; j < n; j++)
        {
            suma_reda += mtr[j][i];
        }

        while (suma_reda < c[i])
        {
            int suma_k = 0;

            for (k = 0; k < m; k++)
            {
                for (l = 0; l < n; l++)
                {
                    suma_k += mtr[l][k]; 

                    if (suma_k > c[k])
                    {
                        for (l = 0; l < n; l++)
                        {
                            if (mtr[l][k] != 0)
                            {
                                mtr[l][k]--;
                                mtr[l][i]++;
                                suma_reda++;
                            } 
                        }
                    }
                }
            }
        }
    }

    dodavanje = true;

    while (dodavanje)
    {
        dodavanje = false;

        for (i = 0; i < n; i++)
        {
            if (pun_red[i])
                continue;

            for (j = 0; j < m; j++)
            {
                if (puna_kolona[j])
                    continue;

                dodavanje = true;

                while (!pun_red[i] && !puna_kolona[j])
                {
                    mtr[i][j]++;

                    for (k = 0, suma_reda = 0; k < m; k++)
                        suma_reda += mtr[i][k];

                    if (suma_reda == b[i])
                        pun_red[i] = true;

                    for (k = 0, suma_reda = 0; k < n; k++)
                        suma_reda += mtr[k][j];

                    if (suma_reda == d[j])
                        puna_kolona[j] = true;
                }
            }
        }
    }

    suma_reda = 0;

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)  
        {
            suma_reda += mtr[i][j];
        }
    }

    printf("%d\n", suma_reda);

    k = 0;

    for (i = 0; i < n; i++)
        for (j = 0; j < m; j++)
            if (mtr[i][j] != 0)  k++;

    printf("%d\n", k);

    for (i = 0; i < n; i++)
        for (j = 0; j < m; j++)
            if (mtr[i][j] != 0)
                printf("%d %d %d\n", i + 1, j + 1, mtr[i][j]);
}