#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;
using ll = long long;

int main()
{
    fast;
    ll n, m;
    cin >> n >> m;
    bool podzad1 = true;
    ll br_podzad1 = 0;
    if(n != m) podzad1 = false;

    vector<pair<ll, ll> > ogr_red; // ogranicenja za red
    vector<pair<ll, ll> > ogr_kol; // ogranicenja za kolone
//    vector<ll> zbir_red(n, 0);
//    vector<ll> zbir_kol(m, 0);
    for(ll i = 0; i < n; i++)
    {
        ll a, b;
        cin >> a >> b;
        ogr_red.push_back(make_pair(a, b));
        br_podzad1 += b;
    }
    for(ll i = 0; i < m; i++)
    {
        ll c, d;
        cin >> c >> d;
        ogr_kol.push_back(make_pair(c, d));
        if(ogr_red[i].first > ogr_kol[i].first || ogr_kol[i].first > ogr_red[i].second || ogr_red[i].second > ogr_kol[i].second) podzad1 = false;
    }

    if(podzad1)
    {
        cout << br_podzad1 << "\n";
        cout << n << "\n";
        for(int i = 0; i < n; i++)
        {
            cout << i + 1 << " " << i + 1 << " " << ogr_red[i].second << "\n";
        }
    }


    return 0;
}