using System;

namespace MyApp // Note: actual namespace depends on the project name.
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            long N = long.Parse(s[0]);
            long M = long.Parse(s[1]);
            long[,] memo = new long[N, M];
            List<long> Nkr = new List<long>();

            List<long> Mkr = new List<long>();

            List<long> resenja = new List<long>();
            List<long> prosliNmax = new List<long>();
            List<long> prosliMmax = new List<long>();

            for (int i = 0; i < N; i++)
            {
                string[] c = Console.ReadLine().Split();
                if (c[1] != c[0] && c[0] != "0")                
                    Nkr.Add(long.Parse(c[1]) - long.Parse(c[0]) + 1);
                else if (c[0] == "0")  Nkr.Add(long.Parse(c[1])); 
                else  Nkr.Add(long.Parse(c[1]));


            }
            for (int i = 0; i < M; i++)
            {
                string[] c = Console.ReadLine().Split();
                if (c[1] != c[0] && c[0] != "0")
                    Mkr.Add(long.Parse(c[1]) - long.Parse(c[0]) + 1);
                else if (c[0] == "0") Mkr.Add(long.Parse(c[1]));
                else Mkr.Add(long.Parse(c[1]));

            }
            long br = 0;
            long max = 0;
            if (N <= M)
            {
                while (Nkr.Count > 0) 
                {

                    long Nmax = Nkr.Max();
                    long Mmax = Mkr.Max();
                    /*
                    if (prosliMmax.Contains(Mmax) || prosliNmax.Contains(Nmax))
                    {
                        br++;
                    }
                    */
                        resenja.Add(Nkr.IndexOf(Nmax) + 1 + br);
                        resenja.Add(Mkr.IndexOf(Mmax) + 1 + br);
    

                    if (Nmax > Mmax)
                    {
                        Console.WriteLine("Mmax: " + Mmax + " indeks mmax: " + Mkr.IndexOf(Mmax) + "Nmax:" + Nmax + " Nmax indeks: " + Nkr.IndexOf(Nmax));
                        Mkr.Remove(Mmax);
                        Nkr[Nkr.IndexOf(Nmax)] -= Mmax;
                        max += Mmax;
                        resenja.Add(Mmax);

                    }
                    else if (Mmax > Nmax)
                    {
                        Console.WriteLine("Mmax: " + Mmax + " indeks mmax: " + Mkr.IndexOf(Mmax) + "Nmax:" + Nmax + " Nmax indeks: " + Nkr.IndexOf(Nmax));
                        Nkr.Remove(Nmax);
                        Mkr[Mkr.IndexOf(Mmax)] -= Nmax;
                        max += Nmax;
                        resenja.Add(Nmax);
                    }
                    else
                    {
                        Console.WriteLine("Mmax: " + Mmax + " indeks mmax: " + Mkr.IndexOf(Mmax) + "Nmax:" + Nmax + " Nmax indeks: " + Nkr.IndexOf(Nmax));
                        Mkr.Remove(Mmax);
                        Nkr.Remove(Nmax);
                        max += Nmax;
                        resenja.Add(Mmax);
                    }
                    prosliNmax.Add(Nmax);
                    prosliMmax.Add(Mmax);
                    br++;
                }
            }
            else
            {
                while (Mkr.Count > 0) 
                {
                    Console.WriteLine(" traganje");
                    long Nmax = Nkr.Max();
                    long Mmax = Mkr.Max();
                    /*
                    if (prosliMmax.Contains(Mmax) || prosliNmax.Contains(Nmax))
                    {
                        br++;
                    }
                    */

                    resenja.Add(Nkr.IndexOf(Nmax) + 1 + br);
                    resenja.Add(Mkr.IndexOf(Mmax) + 1 + br);


                    if (Nmax > Mmax)
                    {
                        Console.WriteLine("Mmax: " + Mmax + " indeks mmax: " + Mkr.IndexOf(Mmax) + "Nmax:" + Nmax + " Nmax indeks: " +Nkr.IndexOf(Nmax));
                        Mkr.Remove(Mmax);
                        Nkr[Nkr.IndexOf(Nmax)] -= Mmax;
                        max += Mmax;
                        resenja.Add(Mmax);
                    }
                    else if (Mmax > Nmax)
                    {
                        Console.WriteLine("Mmax: " + Mmax + " indeks mmax: " + Mkr.IndexOf(Mmax) + "Nmax:" + Nmax + " Nmax indeks: " + Nkr.IndexOf(Nmax));
                        Nkr.Remove(Nmax);
                        Mkr[Mkr.IndexOf(Mmax)] -= Nmax;
                        max += Nmax;
                        resenja.Add(Nmax);
                    }
                    else
                    {
                        Console.WriteLine("Mmax: " + Mmax + " indeks mmax: " + Mkr.IndexOf(Mmax) + "Nmax:" + Nmax + " Nmax indeks: " + Nkr.IndexOf(Nmax));
                        Mkr.Remove(Mmax);
                        Nkr.Remove(Nmax);
                        max += Nmax;
                        resenja.Add(Mmax);
                    }
                    prosliNmax.Add(Nmax);
                    prosliMmax.Add(Mmax);

                    br++;
                }
            }

            Console.WriteLine(max);
            for (int i = 0; i < resenja.Count; i += 3) 
            {
               
                Console.WriteLine(resenja[i] + " " + resenja[i + 1] + " " + resenja[i + 2]); 
            }


        }
    }
}