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

namespace ConsoleApplication286
{
    class Program
    {
        struct polje
        {
            public int x;
            public int y;
            public long brk;
        }
        static void Main(string[] args)
        {
            string[] str = Console.ReadLine().Split();
            int n = int.Parse(str[0]);
            int m = int.Parse(str[1]);
            long[] a = new long[n];
            long[] b = new long[n];
            for(int i = 0; i<n;i++)
            {
                str = Console.ReadLine().Split();
                a[i] = int.Parse(str[0]);
                b[i] = int.Parse(str[1]);
            }
            long[] c = new long[m];
            long[] d = new long[m];
            for (int i = 0; i < m; i++)
            {
                str = Console.ReadLine().Split();
                c[i] = int.Parse(str[0]);
                d[i] = int.Parse(str[1]);
            }

            long[,] mat = new long[n, m];
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    mat[i, j] = 0;
                }
            }

            for(int i = 0; i<n;i++)
            {
                for(int j = 0; j<m && a[i]!=0;j++)
                {
                    mat[i, j] = Math.Min(a[i], c[j]);
                    a[i] -= mat[i, j];
                    c[j] -= mat[i, j];
                    b[i] -= mat[i, j];
                    d[i] -= mat[i, j];
                }
            }

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m && b[i] != 0; j++)
                {
                    long k = Math.Min(b[i], d[j]);
                    mat[i, j] += k;
                    b[i] -= k;
                    d[i] -= k;
                }
            }
            long s = 0;
            int brb = 0;
            List<polje> p = new List<polje>();
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if(mat[i, j]!=0)
                    {
                        s += mat[i, j];
                        brb++;
                        polje po;
                        po.x = i+1;
                        po.y = j+1;
                        po.brk = mat[i, j];
                        p.Add(po);
                    }
                }
            }
            Console.WriteLine(s);
            Console.WriteLine(brb);
            foreach(polje v in p)
            {
                Console.Write(v.x + " " + v.y + " " + v.brk);
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}