#include <bits/stdc++.h>
#define ll long long
#define MAXN 50010
using namespace std;
ll a[MAXN],b[MAXN],c[MAXN],d[MAXN];
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll n,m;
    cin >> n >> m;
    for (ll i=1;i<=n;i++)
        cin >> a[i] >> b[i];
    for (ll i=1;i<=m;i++)
        cin >> c[i] >> d[i];
    if (n==m)
    {
        bool da=true;
        ll sum=0;
        for (ll i=1;i<=n;i++)
        {
            if (a[i]<=c[i] && c[i]<=b[i] && b[i]<=d[i])
            {
                sum+=b[i];
                continue;
            }
            da=false;
        }
        if (da)
        {
            cout << sum << "\n" << n << "\n";
            for (ll i=1;i<=n;i++)
                cout << i << " " << i << " " << b[i] << "\n";
            return 0;
        }
    }
    return 0;
}