#include <iostream>
#include <vector>


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

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

    int a;

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

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

    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], kolone[i]));
        }
    }

    /*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";
        }
    }
    
}