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

namespace prvi1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int m = int.Parse(Console.ReadLine());
            string [] s;
            int[] a = new int[n];
            int[] b = new int[n];
            int[] c = new int[m];
            int[] d = new int[m];
            for (int i = 0; i < n; i++)
            {
                s = Console.ReadLine().Split();
                a[i] = int.Parse(s[0]);
                b[i] = int.Parse(s[1]);
            }
            for (int i = 0; i < m; i++)
            {
                s = Console.ReadLine().Split();
                c[i] = int.Parse(s[0]);
                d[i] = int.Parse(s[1]);
            }
            int zbirGranicaKolona = 0;
            foreach(int e in b)
            {
                zbirGranicaKolona += e;
            }
            if(n == 1)
            {
                if (d[0] == 0)
                {
                    Console.WriteLine(0);
                    Console.WriteLine(0);
                }
                else
                {
                    if (zbirGranicaKolona < d[0])
                    {
                        Console.WriteLine(zbirGranicaKolona);
                        for (int i = 0; i < b.Length; i++)
                        {
                            if (b[i] == 0)
                            {
                                zbirGranicaKolona--;
                            }
                        }
                        Console.WriteLine(zbirGranicaKolona);
                        for (int i = 0; i < n; i++)
                        {
                            for (int j = 0; j < m; j++)
                            {
                                if (b[i] != 0)
                                    Console.WriteLine($"{i} {j}");
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine(d[0]);
                    }
                }

            }
        }
    }
}