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

namespace Petar_Nesic_III_1
{
    class Program
    {
        static void A(int x, int y, int[] R, int[] K, int k, int N, int M)
        {
            if (x >= N)
            {
                Console.WriteLine(k);
            }
            else
            {
                int z = Math.Min(R[x], K[y]);
                R[x] -= z;
                K[y] -= z;
                if (R[x] == 0 && K[y] == 0)
                {
                    A(x + 1, y + 1, R, K, k + 1, N, M);
                }
                else if (R[x] == 0)
                {
                    A(x + 1, y, R, K, k + 1, N, M);
                }
                else if (K[y] == 0)
                {
                    A(x, y + 1, R, K, k + 1, N, M);
                }
                Console.WriteLine("{0} {1} {2}", x + 1, y + 1, z);
            }
        }
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int N = int.Parse(s[0]);
            int M = int.Parse(s[1]);
            int[] MinR = new int[50000];
            int[] MaxR = new int[50000];
            int[] MinK = new int[50000];
            int[] MaxK = new int[50000];
            int m = 0;
            for (int i = 0; i < N; i++)
            {
                s = Console.ReadLine().Split();
                MinR[i] = int.Parse(s[0]);
                MaxR[i] = int.Parse(s[1]);
                m += MaxR[i];
            }
            int mm = 0;
            for (int i = 0; i < M; i++)
            {
                s = Console.ReadLine().Split();
                MinK[i] = int.Parse(s[0]);
                MaxK[i] = int.Parse(s[1]);
                mm += MaxK[i];
            }
            int mmmm = m;
            if (m < mm)
            {
                m = mm - m;
                int mmm = m;
                int i = 0;
                while (mmm > 0)
                {
                    int z = Math.Min(MaxK[i] - MinK[i], mmm);
                    mmm -= z;
                    MaxK[i] -= z;
                    i++;
                }
            }
            else if (mm < m)
            {
                mmmm = mm;
                m -= mm;
                int mmm = m;
                int i = 0;
                while (mmm > 0)
                {
                    int z = Math.Min(MaxR[i] - MinR[i], mmm);
                    mmm -= z;
                    MaxR[i] -= z;
                    i++;
                }
            }
            Console.WriteLine(mmmm);
            A(0, 0, MaxR, MaxK, 0, N, M);
            Console.ReadLine();
        }
    }
}