#include <bits/stdc++.h>

using namespace std;

vector <pair <int, int>> redovi;
vector <pair <int, int>> kolone;
vector <pair <pair <int, int>, int>> resenja;

void ogranicenja_redova(int n)
{
    pair <int, int> pom;
    for(int i = 0; i < n; i++)
    {
        scanf("%d %d", &pom.first, &pom.second);
        redovi.push_back(pom);
    }
}
void ogranicenja_kolona(int m)
{
    pair <int, int> pom;
    for(int i = 0; i < m; i++)
    {
        scanf("%d %d", &pom.first, &pom.second);
        kolone.push_back(pom);
    }
}
void ispisi_resenja(int br)
{
    for(int i = 0; i < br; i++)
    {
        printf("%d %d %d\n", resenja[i].first.first, resenja[i].first.second, resenja[i].second);
    }
}
int main()
{
    int N, M;
    scanf("%d %d", &N, &M);
    ogranicenja_redova(N);
    ogranicenja_kolona(M);

    int K = 0, br = 0;
    int p;
    pair <pair <int, int>, int> pom;

    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < M; j++)
        {
            if(max(redovi[i].first, kolone[j].first) <= min(redovi[i].second, kolone[j].second))
            {
                p = min(redovi[i].second, kolone[j].second);
                K += p;
                br++;
                redovi[i].second -= p;
                kolone[j].second -= p;
                pom.first.first = i+1;
                pom.first.second = j+1;
                pom.second = p;
                resenja.push_back(pom);
            }
            if(redovi[i].second == 0)
                    break;
        }
    }

    printf("%d\n%d\n", K, br);
    ispisi_resenja(br);

    return 0;
}