using System;

namespace ConsoleApp3
{
    internal class Program
    {
        static int Max(int[] niz)
        {
            int max = niz[0], indeks = 0;
            for (int i = 1; i < niz.Length; i++)
            {
                if (niz[i] > max)
                {
                    max = niz[i];
                    indeks = i;
                }
            }
            return indeks;
        }
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int n = int.Parse(s[0]), m = int.Parse(s[1]);
            int[] a = new int[n];
            int[] b = new int[n];
            int[] c = new int[m];
            int[] d = new int[m];
            long zbira = 0, zbirb = 0, zbirc = 0, zbird = 0;
            for (int i = 0; i < n; i++)
            {
                s = Console.ReadLine().Split();
                a[i] = int.Parse(s[0]);
                zbira += a[i];
                b[i] = int.Parse(s[1]);
                zbirb += b[i];
            }
            for (int i = 0; i < m; i++)
            {
                s = Console.ReadLine().Split();
                c[i] = int.Parse(s[0]);
                zbirc += c[i];
                d[i] = int.Parse(s[1]);
                zbird += d[i];
            }
            long brmax = Math.Min(zbird, zbirb), brmin;
            Console.WriteLine(brmax);
            int[,] matrica = new int[n, m];
            if (zbira >= zbirc)
            {
                brmin = zbira;
                for (int i = 0; i < n; i++) b[i] -= a[i];
                for (int i = 0; i < n; i++)
                {
                    for (; a[i] > 0; a[i]--)
                    {
                        int max = Max(d);
                        if (d[max] <= 0) break;
                        matrica[i, max]++;
                        d[max]--;
                    }
                }
            }
            else
            {
                brmin = zbirc;
                for (int i = 0; i < m; i++) d[i] -= c[i];
                for (int i = 0; i < m; i++)
                {
                    for (; c[i] > 0; c[i]--)
                    {
                        int max = Max(b);
                        if (b[max] <= 0) break;
                        matrica[max, i]++;
                        b[max]--;
                    }
                }
            }
            long br = brmax - brmin;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (br > 0 && b[i] > 0 && d[j] > 0) 
                    {
                        int t = Math.Min(b[i], d[j]);
                        if (t <= br)
                        {
                            matrica[i, j] += t;
                            b[i] -= t;
                            d[j] -= t;
                            br -= t;
                        }
                        else
                        {
                            int br1 = Convert.ToInt32(br);
                            matrica[i, j] += br1;
                            b[i] -= br1;
                            d[j] -= br1;
                            br = 0;
                        }
                    }
                }
            }
            br = 0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (matrica[i, j] != 0) br++;
                }
            }
            Console.WriteLine(br);
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (matrica[i, j] != 0) Console.WriteLine("{0} {1} {2}", i + 1, j + 1, matrica[i, j]);
                }
            }
            
        }
    }
}