#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    vector<pair<int,int>> niz1;

    int n,m;
    cin >> n;
    cin >> m;
    int mini,maxi;
    for (int i=0;i<n;i++){
        cin >> mini;
        cin >> maxi;
        niz1.push_back({mini,maxi});
    }
    int k = 0;
    int broj = 0;

    vector<int>krompiri;
    for(int j = 0 ; j < m; j++){
        cin>>mini;
        cin>>maxi;

        for(int i = 0;i < n; i++){

                int pom;
                if(min(niz1[i].second,maxi) >= max(niz1[i].first,mini)){
                    pom = min(niz1[i].second,maxi);
                    k++;
                    niz1.push_back({i+1,j+1});
                    krompiri.push_back(pom);
                    broj+=pom;
                }



        }
    }
    cout << broj << endl << k << endl;
    for (int i = 0 ; i < k ; i ++){
        cout << niz1[n+i].first << " "<< niz1[n+i].second << " " << krompiri[i] << endl;
    }
    return 0;
}