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

namespace MyApp
{
    internal class Program
    {
        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];
            for (int i = 0; i < N; i++)
            {
                string[] sN = Console.ReadLine().Split();
                Ai[i] = int.Parse(sN[0]); Bi[i] = int.Parse(sN[1]);
            }
            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 (N == 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)
            {
                int suma = 0;
                for(int k = 0; k< N; k++) 
                {
                    suma += Bi[k];
                }
                Console.WriteLine(suma);
                Console.WriteLine(N);
                for (int i = 0; i < N; i++)
                {
                    for (int j = 0; j < M; j++)
                    {
                        if (j == i) Console.WriteLine((i + 1) + " " + (j + 1) + " " + Dj[i]);
                        
                    }
                }
            }
            else if (Ai[0] == Bi[0] && Cj[0] == Dj[0])
            {
                int[,] mat = new int[N, M];

                int suma = 0;
                for (int k = 0; k < N; k++)
                {
                    suma += Bi[k];
                }
                Console.WriteLine(suma);
                Console.WriteLine(N*M);
                for (int i = 0; i < N; i++)
                {
                    for (int j = 0; j < M; j++)
                    {
                        Console.WriteLine((i+1)+" "+(j+1)+" " + Dj[j]);
                    }
                }
            }


        }
    }
}