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

namespace ConsoleApplication9
{
    class Program
    {
        static int resenje(int[,] res, int n, int m, int[,] redovi, int[,] kolone)
        {
            int suma = 0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (res[i, j] > kolone[j, 1])
                    {
                        int dodajemo = res[i, j] - kolone[j, 1];
                        res[i, j] -= dodajemo;
                        for (int j2 = j + 1; j2 < m; j++)
                        {
                            if (res[i, j2] < kolone[j2, 1])
                            {
                                if (dodajemo > (kolone[j2, 1] - res[i, j2]))
                                {
                                    int dodajemo2 = dodajemo;
                                    res[i, j2] -= dodajemo2 - kolone[j2, 1];
                                    dodajemo -= (kolone[j2, 1] - res[i, j2]);
                                }
                                else
                                {
                                    int dodajemo2 = dodajemo;
                                    res[i, j2] -= dodajemo;
                                    dodajemo = 0;
                                }
                            }

                        }

                    }
                }
            }
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    suma += res[i, j];
                }
            }
            return suma;

        }
        static void Main(string[] args)
        {
            string[] pred = Console.ReadLine().Split();
            int n = int.Parse(pred[0]);
            int m = int.Parse(pred[1]);
            int[,] redovi = new int[n, 2];
            int[,] kolone = new int[m, 2];
            for (int i = 0; i < n; i++)
            {
                string[] dred = Console.ReadLine().Split();
                redovi[i, 0] = int.Parse(dred[0]);
                redovi[i, 1] = int.Parse(dred[1]);

            }
            for (int i = 0; i < m; i++)
            {
                string[] dred = Console.ReadLine().Split();
                kolone[i, 0] = int.Parse(dred[0]);
                kolone[i, 1] = int.Parse(dred[1]);

            }

            int[,] res = new int[n, m];

            for (int i = 0; i < n; i++)
            {
                int dodatak = redovi[i, 1] / m;
                for (int j = 0; j < m; j++)
                {

                    res[i, j] = dodatak;
                }
                res[i, n - 1] = dodatak + redovi[i, 1] % m;
            }

            int res2 = resenje(res, n,m, redovi, kolone);
            Console.WriteLine(res2);
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (res[i, j] > 0)
                    {
                        Console.Write(i.ToString() + " " + j.ToString() + " " + res[i, j].ToString());
                    }
                }
                Console.WriteLine();
            }
            Console.Read();

        }
    }
}