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 p = 0;
            int j = 0;
            i = 0;
            if (minksuma < minrsuma)
            {
                while (j < m)
                {
                    
                  //  Console.WriteLine(polja[i, j]);
                //   Console.WriteLine(i + " " + j);
                    polja[i, j] = polja[i, j] + Math.Min(mink[j], maxr[i]);
                    p = Math.Min(mink[j], maxr[i]);
                    mink[j] = mink[j] - p;
                    maxk[j] = maxk[j] - p;
                    minr[i] = minr[i] - p;
                    maxr[i] = maxr[i] - p;
                    if (mink[j] > 0 && i != n-1) i++;
                    else
                    { j++; i = 0; }



                }
                j = 0;
                i = 0;
                
                while (i < n)
                {
              //      Console.WriteLine(polja[i, j]);
               //     Console.WriteLine(i + " " + j + " drugo ");
                    polja[i, j] = polja[i,j] +   Math.Min(minr[j], maxk[i]);
                    p = Math.Min(minr[j], maxk[i]);
                    mink[j] = mink[j] - p;
                    maxk[j] = maxk[j] - p;
                    minr[i] = minr[i] - p;
                    maxr[i] = maxr[i] - p;
                    if (minr[i] > 0 && j != m-1) j++;
                    else
                    { i++; j = 0; }

                }

            }
            else
            {
                while (i < n)
                {
                    polja[i, j] = polja[i, j] + Math.Min(minr[j], maxk[i]);
                    p = Math.Min(minr[j], maxk[i]);
                    mink[j] = mink[j] - p;
                    maxk[j] = maxk[j] - p;
                    minr[i] = minr[i] - p;
                    maxr[i] = maxr[i] - p;
                    if (minr[i] > 0 && j != m-1) j++;
                    else
                    { i++; j = 0; }

                }
                j = 0;
                i = 0;
                while (j < m)
                {
                    polja[i, j] = polja[i, j] + Math.Min(mink[j], maxr[i]);
                    p = Math.Min(mink[j], maxr[i]);
                    mink[j] = mink[j] - p;
                    maxk[j] = maxk[j] - p;
                    minr[i] = minr[i] - p;
                    maxr[i] = maxr[i] - p;
                    if (mink[j] > 0 && i != n-1) i++;
                    else
                    { j++; i = 0; }



                }
            }
            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++;
            }




        }
    }
}