#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
using namespace std;

int n,m;
vector<pii> red,kol;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;
    red.resize(n);
    kol.resize(m);
    long long s1=0,s2=0;
    for(int i = 0; i < n; i++) {cin >> red[i].fi >> red[i].se; s1 += red[i].se;}
    for(int j = 0; j < m; j++) {cin >> kol[j].fi >> kol[j].se; s2 += kol[j].se;}
    cout << min(s1,s2) << "\n";
    cout << (long long)m*n << "\n";
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
    {
        cout << i << " " << j << " " << 1 << "\n";
    }

}