using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Krompiri
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] unos = Console.ReadLine().Split();
            long n = int.Parse(unos[0]);
            long m = int.Parse(unos[1]);
            long[,] a = new long[2, n];
            for (long i = 0; i < n; i++)
            {
                unos = Console.ReadLine().Split();
                a[0, i] = Int32.Parse(unos[0]);
                a[1, i] = Int32.Parse(unos[1]);
            }
            long[,] b = new long[2, m];
            for (long i = 0; i < m; i++)
            {
                unos = Console.ReadLine().Split();
                b[0, i] = Int32.Parse(unos[0]);
                b[1, i] = Int32.Parse(unos[1]);
            }
            long[,] matrica = new long[n, m];
            for (long i = 0; i < n; i++)
            {
                for (long j = 0; j < m; j++)
                {
                    matrica[i, j] = Math.Min(a[1, i], b[1, i]);
                    a[1,i]-=matrica[i,j];
                    b[1, i] -= matrica[i, j];
                    while (a[1, i] < 0|| b[1, j]<0)
                    {
                        a[1, i]++;
                        b[1, i]++;
                        matrica[i, j]--;
                    }
                }
            }
            long zbir = 0;
            List<long[]> pozitivan = new List<long[]>();
            long sada = 0;
            for (long i = 0; i < n; i++)
            {
                for (long j = 0; j < m; j++)
                {
                    if (matrica[i, j] != 0)
                    {
                        zbir += matrica[i, j];
                        long[] niz = new long[3];
                        niz[0] = i+1;
                        niz[1] = j+1;
                        niz[2] = matrica[i, j];
                        pozitivan.Add(niz);
                        sada++;
                    }
                }
            }
            Console.WriteLine(zbir);
            Console.WriteLine(sada);
            for (int i = 0; i < sada; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.Write(pozitivan[i][j] +" ");
                }
                Console.WriteLine();
            }
        }
    }
}