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

#define ll long long


int main()
{

    int n, m;
    cin >> n >> m;
    vector<int> a(n),b(n),c(m),d(m);
    map<int, int> d1;
    for(int i = 0; i < n; i++)
    {
        cin >> a[i] >> b[i];
    }

    for(int j = 0; j < m; j++)
    {
        cin >> c[j] >> d[j];
        d1[d[j]] = j;
    }
    sort(d.begin(), d.end());

    int stampaj[n][3];
    int k = 0;

    for(int i = 0; i < n; i++)
    {
        auto it = lower_bound(d.begin(), d.end(), b[i]);
        stampaj[i][0] = i + 1;
        if(it == d.end())
        {
            k += *(it - 1);
            stampaj[i][1] = d1[*(it - 1)];
            stampaj[i][2] = *(it - 1);
        }

        else if(*it == b[i])
        {
            stampaj[i][1] = d1[*it] + 1;
            stampaj[i][2] = b[i];
        }
        else
        {
            stampaj[i][1] = d1[*it] + 1;
            stampaj[i][2] = b[i];
        }
        /*
        if(*it != b[i])
        {
            stampaj[i][1] = d1[*(it - 1)] + 1;
        }
        else
        {
            stampaj[i][1] = d1[*it] + 1;
            stampaj[i][2] = *it;
            k += *it;
        }

        else
        {
            stampaj[i][2] = *it;
            k += *it;
        }*/
        k += stampaj[i][2];
    }

    cout << k << endl;
    cout << n << endl;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < 3; j++)
            cout << stampaj[i][j] << " ";
        cout << endl;
    }

    return 0;
}