using System;

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int n, m;
            string str = Console.ReadLine();
            string[] s = str.Split(' ');
            n = Convert.ToInt32(s[0]);
            m = Convert.ToInt32(s[1]);
            int i = 0;
            int[] minr = new int[n];
            int[] maxr = new int[n];
            int[] mink = new int[m];
            int[] maxk = new int[m];
            int minrsuma = 0;
            int maxrsuma = 0;
            int minksuma = 0;
            int maxksuma = 0;
            int[,] polja = new int[n, m];
            while (i < n)
            {
                str = Console.ReadLine();
                s = str.Split(' ');
                minr[i] = Convert.ToInt32(s[0]);
                maxr[i] = Convert.ToInt32(s[1]);
                minrsuma = minrsuma + minr[i];
                maxrsuma = maxrsuma + maxr[i];

                i++;
            }
            i = 0;
            while (i < m)
            {
                str = Console.ReadLine();
                s = str.Split(' ');
                mink[i] = Convert.ToInt32(s[0]);
                maxk[i] = Convert.ToInt32(s[1]);
                minksuma = minksuma + mink[i];
                maxksuma = maxksuma + maxk[i];
                i++;
            }
            int j = 0;
            if (n == 1)
            {
                i = 0;
                while (j < m)
                {
                    Console.WriteLine(j);
                    polja[i, j] = mink[j];
                    maxk[j] = maxk[j] - mink[j];
                    mink[j] = 0;
                    j++;

                }

                j = 0;
                while (j < m)
                {
                    Console.WriteLine(j);
                    polja[i, j] = polja[i, j] + Math.Min(maxk[j], maxr[i]);
                    int p = Math.Min(maxk[j], maxr[i]);
                    maxk[j] = maxk[j] - p;
                    maxr[i] = maxr[i] - p;
                    j++;

                }
            }
            else
            { while (1<2)
                {


                }
            }
        



      
           i = 0;
           j = 0;
           int ukupno = 0;
           int poljauk = 0;

           while (i < n)
           {
               while (j < m)
               {
                   //  Console.WriteLine(polja[i, j] + "    ");
                   if (polja[i, j] != 0)
                   {
                       ukupno = ukupno + polja[i, j];
                       poljauk++;
                       //Console.WriteLine((i+1)+ " " + (j+1) + " " + polja[i, j]);
                   }
                   j++;

               }

               j = 0;
               i++;
           }
           Console.WriteLine(ukupno);
           Console.WriteLine(poljauk);
           i = 0;
           j = 0;
           while (i < n)
           {
               while (j < m)
               {
                   //  Console.WriteLine(polja[i, j] + "    ");
                   if (polja[i, j] != 0)
                   {

                       Console.WriteLine((i + 1) + " " + (j + 1) + " " + polja[i, j]);
                   }
                   j++;

               }

               j = 0;
               i++;
           }




    }
    }
    }