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

namespace ConsoleApplication287
{
    class Program
    {
        struct polje
        {
            public int x;
            public int y;
            public long brk;
        }
        static void nje1(long[,] mat, long[] a, long[] b, long[] c, long[] d, int m)
        {
            for(int i = 0; i<m;i++)
            {
                mat[0, i] = c[i];
                b[0] -= mat[0, i];
                d[i] -= mat[0, i];
            }
            for (int i = 0; i < m && b[0]!=0; i++)
            {
                long k = Math.Min(d[i], b[0]);
                mat[0, i] += k;
                b[0] -= k;
            }
        }
        static void aic0(long[,] mat, long[] a, long[] b, long[] c, long[] d, int n, int m)
        {
            for(int i = 0; i<n;i++)
            {
                for(int j = 0; j<m && b[i]>0;j++)
                {
                    mat[i, j] = Math.Min(b[i], d[j]);
                    b[i] -= mat[i, j];
                    d[j] -= mat[i, j];
                }
            }
        }
        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];
            bool ta = true;
            for(int i = 0; i<n;i++)
            {
                str = Console.ReadLine().Split();
                a[i] = int.Parse(str[0]);
                b[i] = int.Parse(str[1]);
                if (a[i] != 0) ta = false;
            }
            long[] c = new long[m];
            long[] d = new long[m];
            bool tc = true;
            for (int i = 0; i < m; i++)
            {
                str = Console.ReadLine().Split();
                c[i] = int.Parse(str[0]);
                d[i] = int.Parse(str[1]);
                if (c[i] != 0) tc = false;
            }
            long[,] mat = new long[n, m];
            for(int i = 0; i<n;i++)
            {
                for(int j = 0; j<m;j++)
                {
                    mat[i, j] = 0;
                }
            }
            if (n == 1) nje1(mat, a, b, c, d, m);
            else
            {
                if(ta&&tc) aic0(mat, a, b, c, d, n, m);
            }
            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();
            }
        }
    }
}