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

namespace Krompiri
{
    internal class Program
    {
        struct polje
        {
            public long prvi;
            public long drugi;
        }
        static void Main(string[] args)
        {
            int pocI = 1;
            long br = 0;
            int pocJ = 1;
            string[] u = Console.ReadLine().Split(' ');
            long M = long.Parse(u[1]);
            long N = long.Parse(u[0]);
            polje[,] mars = new polje[N+1, M+1];
            for(int i = 1; i <= N; i++)
            {
                string[] u2 = Console.ReadLine().Split(' ');
                mars[i, 0].prvi = long.Parse(u2[0]);
                mars[i, 0].drugi = long.Parse(u2[1]);
            }
            for (int j = 1; j <= M; j++)
            {
                string[] u2 = Console.ReadLine().Split(' ');
                mars[0, j].prvi = long.Parse(u2[0]);
                mars[0, j].drugi = long.Parse(u2[1]);
            }
            for(int i = 1;i<= N; i++)
            {
                for(int j = 1;j<=M;j++)
                {
                    
                    if (mars[0, j].drugi <= 0)
                    {
                        continue;
                    }
                    else
                    {
                        Console.WriteLine(i + " | " + j);
                        Console.WriteLine(mars[i, 0].drugi + " | " + mars[0, j].drugi);
                        Console.WriteLine("=========");
                        long konst = Math.Min(mars[i, 0].drugi, mars[0, j].drugi);
                        if(konst > 0)
                        {
                            br++;
                        }
                        mars[i, j].prvi = konst;
                        mars[0, 0].prvi += konst;
                        mars[i, 0].drugi -= konst;
                        mars[0, j].drugi -= konst;

                        if (mars[i, 0].drugi <= 0)
                        {
                            //break;
                        }
                    }
                }
            }

            Console.WriteLine(mars[0, 0].prvi);
            Console.WriteLine(br);
            for (int i = 1; i <= N; i++)
            {
                for (int j = 1; j <= M; j++)
                {
                    if (mars[i,j].prvi > 0)
                    {
                        Console.WriteLine("{0} {1} {2}",i,j,mars[i,j].prvi);
                    }
                }
            }
            Console.ReadLine();
        }
    }
}