#include <bits/stdc++.h>
using namespace std;

#define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define inf INT_MAX
#define llinf LLONG_MAX
#define mt make_tuple
#define mp make_pair
#define parll pair<ll,ll>
#define parii pair<int,int>
#define nzd __gcd
#define nzs(e1,e2) (e1/nzd(e1,e2))*e2
#define neparno(v) (v&1)
#define parno(v) (!neparno(v))
#define lowbit(v) (v&-v)
#define all(V) V.begin(),V.end()
#define fill(V,v) fill(all(V),v)
#define l_b(BRL,brl) lower_bound( BRL.begin(), BRL.end(), brl )-BRL.begin()-1
#define u_b(BRU,bru) upper_bound( BRU.begin(), BRU.end(), bru )-BRU.begin()
#define mod 1000000007
#define mxn 50002

ll q,n,m,A[mxn],B[mxn],C[mxn],D[mxn],R[mxn],suma,br;

void Ispisi2()
{
    for ( ll i = 1; i <= m; i++ )
    {
        if ( R[i] > 0 )
        {
            br++;
        }
    }
    cout << suma << "\n";
    cout << br << "\n";
    for ( ll i = 1; i <= m; i++ )
    {
        cout << "1 " << i << " " << R[i] << "\n";
    }
}

void Podzadatak2()
{
    for ( ll i = 1; i <= m; i++ )
    {
        suma += D[i];
        R[i] = D[i];
    }
    if ( suma <= B[1] )
    {
        Ispisi2();
        return;
    }
    for ( ll i = 1; i <= m; i++ )
    {
        ll x = min(D[i]-C[i],suma-B[i]);
        suma -= x;
        R[i] = D[i]-x;
        if ( suma <= B[1] )
        {
            Ispisi2();
            return;
        }
    }
    Ispisi2();
}

void Ispisi1()
{
    cout << suma << "\n";
    cout << br << "\n";
    for ( ll i = 1; i <= n; i++ )
    {
        cout << i << " " << i << " " << R[i] << "\n";
    }
}

void Podzadatak1()
{
    for ( ll i = 1; i <= n; i++ )
    {
        suma += C[i];
        R[i] = C[i];
    }
    br = n;
    Ispisi1();
}

int main()
{
    ios;

    cin >> n >> m;
    for ( ll i = 1; i <= n; i++ )
    {
        cin >> A[i] >> B[i];
    }
    bool prvi = true;
    for ( ll i = 1; i <= m; i++ )
    {
        cin >> C[i] >> D[i];
        if ( (n != m) || (!(A[i] <= C[i] && C[i] <= B[i] && B[i] <= D[i])) )
        {
            prvi = false;
        }
    }
    if ( n == 1 )
    {
        Podzadatak2();
        return 0;
    }
    if ( prvi )
    {
        Podzadatak1();
        return 0;
    }
    Podzadatak1();

    return 0;
}