using System;

namespace ZADATAK_2_TAKMICENJE_POKUSAJ_2
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] unos = Console.ReadLine().Split();
            int n = int.Parse(unos[0]), m = int.Parse(unos[1]), k = 0;
            int[,] matrica = new int[n, m];
            int[] a = new int[n], b = new int[n], c = new int[m], d = new int[m];
            int[] capR = new int[n], capK = new int[m];
            for (int i = 0; i < n; i++)
            {
                unos = Console.ReadLine().Split();
                a[i] = int.Parse(unos[0]);
                b[i] = int.Parse(unos[1]);
            }
            for (int i = 0; i < m; i++)
            {
                unos = Console.ReadLine().Split();
                c[i] = int.Parse(unos[0]);
                d[i] = int.Parse(unos[1]);
            }
            int ukupno = 0;
            if (n == 1)
            {
                for (int i = 0; i < m; i++)
                {
                    matrica[0, i] = c[i];
                    capR[0] += c[i];
                    capK[i] = c[i];
                    //if(c[i] > 0)
                        //k++;
                    //ukupno += matrica[0, i];
                }
                int j = 0;

                while (capR[0] < b[0] && j < m)
                {
                    if (capR[0] + d[j] <= b[0] && capK[j] < d[j])
                    {
                        //if (capK[j] == 0 && d[j] > 0)
                            //k++;
                        matrica[0, j] = d[j];
                        capR[0] += d[j];
                        //ukupno += d[j];
                        capK[j] = d[j];
                    }
                    else if (capR[0] + d[j] > b[0] && capK[j] < d[j])
                    {
                        //if (capK[j] == 0 && d[j] > 0)
                            //k++;
                        matrica[0, j] += b[0] - capR[0];
                        capK[j] += b[0] - capR[0];
                        //ukupno += b[0] - capR[0];
                        capR[0] = b[0];
                    }
                    j++;
                }
            }
            for (int i = 0; i < m; i++)
            {
                ukupno += matrica[0, i];
                if (matrica[0, i] > 0)
                    k++;
            }
            Console.WriteLine(ukupno);
            Console.WriteLine(k);
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if(matrica[i, j] > 0)
                        Console.WriteLine((i + 1) + " " + (j + 1) + " " + matrica[i, j]);
                }
            }
        }
    }
}