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

namespace ConsoleApplication23
{
    class Program
    {
        struct Polje
        {
            public int i, j, b;
            public Polje(int ii, int jj, int bb)
            {
                i = ii;
                j = jj;
                b = bb;
            }
        }
        static void Main(string[] args)
        {
            string[] s=Console.ReadLine().Split();
            int n=int.Parse(s[0]),m=int.Parse(s[1]);
            int[] minr = new int[n];
            int[] maxr = new int[n];
            int[] mink = new int[m];
            int[] maxk = new int[m];
            int[] zk = new int[m];
            int[] zr = new int[n];
            for (int i = 0; i < n; i++)
            {
                s=Console.ReadLine().Split();
                minr[i] = int.Parse(s[0]);
                maxr[i] = int.Parse(s[1]);
            }
            for (int i = 0; i < m; i++)
            {
                s = Console.ReadLine().Split();
                mink[i] = int.Parse(s[0]);
                maxk[i] = int.Parse(s[1]);
            }
            int[,] a = new int[n, m];
            int z = 0,b=0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    int f = Math.Min(mink[j]-zk[j], minr[i]-zr[i]);
                    if (f > 0)
                    {
                        a[i, j] = f;
                        zk[j] += a[i, j];
                        zr[i] += a[i, j];
                        z += a[i, j];
                    }
                }
            }
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    /*int p=Math.Min(maxk[j],maxr[i]),q=Math.Max(mink[j],minr[i]);
                    if (p >= q)
                    {
                        int h=Math.Max(p, q);
                        if (Math.Min(maxk[j] - zk[j], maxr[i] - zr[i]) >= h)
                            a[i, j] = h;
                        else if (Math.Min(maxk[j] - zk[j], maxr[i] - zr[i]) > 0)
                            a[i, j] = Math.Min(maxk[j] - zk[j], maxr[i] - zr[i]);
                        if (a[i, j] > 0)
                        {
                            z += a[i, j];
                            zk[j] += a[i, j];
                            zr[i] += a[i, j];
                            f.Add(new Polje(i + 1, j + 1, a[i, j]));
                        }
                    }*/
                    int g = Math.Min(maxk[j] - zk[j], maxr[i] - zr[i]);// -Math.Max(zk[j], zr[i]);
                    if (g > 0)
                    {
                        a[i, j] += g;
                        zk[j] += g;
                        zr[i] += g;
                        z += g;
                    }
                    if (a[i, j] > 0)
                        b++;
                }
            }
            Console.WriteLine(z);
            Console.WriteLine(b);
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (a[i,j] > 0)
                        Console.WriteLine((i+1)+" "+(j+1)+" "+a[i,j]);
                }
            }
        }
    }
}