#include <iostream>
#include <vector>

struct parovi {
    int a, b;
};

int main()
{
    int N, M;
    std::cin >> N >> M;

    std::vector<parovi> redovi(N);
    std::vector<parovi> kolone(M);

    for(int i = 0; i < N; i++)
        std::cin >> redovi[i].a >> redovi[i].b;

    for(int i = 0; i < M; i++)
        std::cin >> kolone[i].a >> kolone[i].b;

    std::vector<std::vector<int>> vec(N, std::vector<int> (M));

    int brojKomada = 0;

    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < M; j++)
        {
            brojKomada += (vec[i][j] = std::min(redovi[i].b, kolone[i].b));
        }
    }

    /*for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < M; j++)
        {
            std::cout << vec[i][j] << " ";
        }
        std::cout << "\n";
    }*/

    int brojPolja = N * M;

    std::cout << brojKomada << "\n" << brojPolja << "\n";

    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < M; j++)
        {
            std::cout << i << " " << j << " " << vec[i][j] << "\n";
        }
    }
    
}