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;
            public long v;
            public Polje(int ii, int jj, long vv)
            {
                i = ii; j = jj; v = vv;
            }
        }
        static void Main(string[] args)
        {
            string[] s=Console.ReadLine().Split();
            long n=long.Parse(s[0]),m=long.Parse(s[1]);
            long[] minr = new long[n];
            long[] maxr = new long[n];
            long[] mink = new long[m];
            long[] maxk = new long[m];
            long[] zk = new long[m];
            long[] zr = new long[n];
            for (int i = 0; i < n; i++)
            {
                s=Console.ReadLine().Split();
                minr[i] = long.Parse(s[0]);
                maxr[i] = long.Parse(s[1]);
            }
            for (int i = 0; i < m; i++)
            {
                s = Console.ReadLine().Split();
                mink[i] = long.Parse(s[0]);
                maxk[i] = long.Parse(s[1]);
            }
            long[,] a = new long[n, m];
            long z = 0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    long 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];
                    }
                }
            }
            List<Polje> u = new List<Polje>();
            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]));
                        }
                    }*/
                    long 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)
                        u.Add(new Polje(i + 1, j + 1, a[i, j]));
                }
            }
            Console.WriteLine(z);
            Console.WriteLine(u.Count);
            foreach (Polje q in u)
                Console.WriteLine(q.i+" "+q.j+" "+q.v);
        }
    }
}