using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace MyApp
{
    internal class Program
    {
        public struct Tip
        {
            public int x;
            public int y;
            public int vr;
        }
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int N = int.Parse(s[0]);
            int M = int.Parse(s[1]);
            int[] Ai = new int[N];
            int[] Bi = new int[N];
            int[] Cj = new int[M];
            int[] Dj = new int[M];
            int[] ZbirC = new int[M];
            int K = 0;int BROJnula=0;
            for (int i = 0; i < N; i++)
            {
                string[] sN = Console.ReadLine().Split();
                Ai[i] = int.Parse(sN[0]); Bi[i] = int.Parse(sN[1]);
                K += Bi[i];
            }
            for (int j = 0; j < M; j++)
            {
                string[] sM = Console.ReadLine().Split();
                Cj[j] = int.Parse(sM[0]); Dj[j] = int.Parse(sM[1]);
                if (Cj[j] == 0 && Dj[j] == 0) BROJnula++;
                //ZbirC[j] = Cj[j];
            }
            if (N == 1 && M!=1)
            {
                int[] arr = new int[M];
                int suma = 0;int brojpolja=0;
                for (int i = 0; i < M; i++)
                {
                    arr[i] = Cj[i];
                    suma += arr[i];
                }
                if (suma < Bi[0])
                {
                    for (int i = 0; i < M; i++)
                    {
                        if (arr[i] < Dj[i] && suma + Dj[i] - arr[i] <= Bi[0])
                        {
                            suma += -arr[i] + Dj[i];
                            arr[i] = Dj[i];

                        }
                        else if (suma < Bi[0] && arr[i] < Dj[i])
                        {
                            arr[i] += Bi[0] - suma;
                            suma += arr[i]-Cj[i];

                        }
                    }
                }
                
                Console.WriteLine(suma);
                
                    for (int j = 0; j < M; j++)
                    {
                    if (arr[j] != 0) brojpolja++;
                    }
                Console.WriteLine(brojpolja);
                for (int i = 0; i < M; i++) 
                {
                    if (arr[i] != 0)
                    Console.WriteLine(1+" "+(i+1)+" "+arr[i]); 
                }
            }
            else if (N == M)
            {
                
                Console.WriteLine(K);
                Console.WriteLine(N-BROJnula);
                for (int i = 0; i < N; i++)
                {
                    if (Dj[i]!=0)  
                        Console.WriteLine((i + 1) + " " + (i + 1) + " " + Bi[i]);
                        
                   
                }
            }
            else if (Ai[0] == Bi[0] && Cj[0] == Dj[0])
            {
                int[,] mat = new int[N, M];

               
                Console.WriteLine(K);
                Console.WriteLine(N*M-BROJnula);
                for (int i = 0; i < N; i++)
                {
                    for (int j = 0; j < M; j++)
                    {
                        if (mat[i,j] != 0)
                        Console.WriteLine((i+1)+" "+(j+1)+" " + Dj[j]);
                    }
                }
            }
            else if (Ai[0] == Cj[0] && Ai[0] == 0)
            {
                int sum = 0;
                int kolicina = 0;
                Tip[] matr = new Tip[N*M];
                int index=0;
                for (int i = 0; i < N; i++)
                {
                    for (int j = 0; j < M; j++)
                    {
                        if (Dj[j] <= Bi[i] && Dj[j]!=0)
                        {
                            sum += Dj[j];
                            matr[index].x = i;
                            matr[index].y = j;
                            matr[index].vr = Dj[j];
                            kolicina++;
                            index++;
                        } 
                        else if (Dj[j] != 0)
                        {
                            if (Bi[i]-sum <= Dj[i])
                            {
                                sum += Dj[j];
                                matr[index].x = i;
                                matr[index].y = j;
                                matr[index].vr = Dj[j];
                                kolicina++;
                                index++;
                            }
                        }
                    }
                }
                Console.WriteLine(sum);
                Console.WriteLine(kolicina);
                for(int i = 0; i < index; i++)
                {
                    Console.WriteLine(matr[i].x+" " + matr[i].x+" ")
                }
            }
            //else if (Ai[0] == Bi[0])
            //{
            //    Console.WriteLine(K);
            //    for (int i = 0; i < N; i++)
            //    {
            //        int lol = Bi[i];
            //        for (int j = 0; j < M; j++)
            //        {
            //            if (lol - ZbirC[i])

            //        }
            //    }
            //}

        }
    }
}