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

namespace Zadatak2
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int n = int.Parse(s[0]);
            int m = int.Parse(s[1]);

            int[] v = new int[n];
            int[] a = new int[n]; int[] b = new int[n];
            for (int i = 0; i < n; i++)
            {
                s = Console.ReadLine().Split();
                a[i] = int.Parse(s[0]);
                b[i] = int.Parse(s[1]);
                v[i] = 0;
            }

            int[] k = new int[m];
            int[] c = new int[m]; int[] d = new int[m];
            for (int j = 0; j < m; j++)
            {
                s = Console.ReadLine().Split();
                c[j] = int.Parse(s[0]);
                d[j] = int.Parse(s[1]);
                k[j] = 0;
            }

            int[,] ij = new int[n, m];
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    ij[i, j] = 0;

            bool ok = true; int p = -1, br = 0;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    if ((a[i] > v[i] || c[j] > k[j]) && b[i] > v[i] && d[j] > k[j] && ok)
                    {
                        br++;
                        ij[i, j] = Math.Min(a[i], c[j]);
                        v[i] += Math.Min(a[i], c[j]);
                        k[j] += Math.Min(a[i], c[j]);
                    }
                    else if (a[i] == v[i] && c[j] > k[j] && ok)
                    {
                        p = j;
                        ok = false;
                    }
                    else if (p==j)
                    {
                        br++;
                        ij[i, j] = Math.Min(a[i], c[j]);
                        v[i] += Math.Min(a[i], c[j]);
                        k[j] += Math.Min(a[i], c[j]);
                        ok = true;
                    }

            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    if (b[i] > v[i] && d[j] > k[j])
                    {
                        if (ij[i, j] == 0) br++;
                        ij[i, j] += Math.Min(b[i] - v[i], d[i] - k[i]);
                        v[i] += Math.Min(b[i] - v[i], d[i] - k[i]);
                        k[j] += Math.Min(b[i] - v[i], d[i] - k[i]);
                    }

            Console.WriteLine(br);
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    if (ij[i, j] > 0)
                        Console.WriteLine("{0} {1} {2}", i + 1, j + 1, ij[i, j]);
        }
    }
}