#include<bits/stdc++.h>
#define xx first
#define yy second
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;

int n, m;
map<pii, int> mat;
ll zbirr, zbirk, kolko;
pair<pii, int> r[50010], k[50010];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> m;
    for(int i=1; i<=n; i++) {
        cin >> r[i].xx.xx >> r[i].xx.yy;
        zbirr+=ll(r[i].xx.yy);
        r[i].yy=i;
    }
    for(int i=1; i<=m; i++) {
        cin >> k[i].xx.xx >> k[i].xx.yy;
        zbirk+=ll(k[i].xx.yy);
        k[i].yy=i;
    }

    if(zbirr<=zbirk) {
        cout << zbirr << "\n";
        sort(r+1, r+1+n, [](const pair<pii, int>& A, const pair<pii, int>& B) {return A.xx.yy<B.xx.yy;});
        sort(k+1, k+1+m, [](const pair<pii, int>& A, const pair<pii, int>& B) {return A.xx.xx>B.xx.xx;});

        int gde=1, howmuch=0, dokle=-1, kkk=-1;
        for(int i=1; i<=n; i++) {
            int kk=0;
            bool napolje=0;
            while(true) {
                if(k[gde].xx.xx==0) {
                    napolje=1;
                    break;
                }
                if(r[i].xx.yy-kk<k[gde].xx.xx-howmuch) {
                    if(mat[{r[i].yy, k[gde].yy}]==0) kolko++;
                    mat[{r[i].yy, k[gde].yy}]+=r[i].xx.yy-kk;
                    howmuch+=r[i].xx.yy-kk;
                    break;
                }
                else if(r[i].xx.yy-kk==k[gde].xx.xx-howmuch) {
                    if(mat[{r[i].yy, k[gde].yy}]==0) kolko++;
                    mat[{r[i].yy, k[gde].yy}]+=r[i].xx.yy-kk;
                    gde++;
                    howmuch=0;
                    break;
                }
                else {
                    if(mat[{r[i].yy, k[gde].yy}]==0) kolko++;
                    mat[{r[i].yy, k[gde].yy}]+=k[gde].xx.xx-howmuch;
                    kk+=k[gde].xx.xx-howmuch;
                    gde++;
                    howmuch=0;
                }
            }
            dokle=i;
            kkk=kk;
            if(napolje) break;
        }
        sort(k+1, k+1+m, [](const pair<pii, int>& A, const pair<pii, int>& B) {return A.xx.yy-A.xx.xx>B.xx.yy-B.xx.xx;});
        gde=1, howmuch=k[gde].xx.xx;
        for(int i=dokle; i<=n; i++) {
            int kk=kkk;
            while(true) {
                if(r[i].xx.yy-kk<k[gde].xx.yy-howmuch) {
                    if(mat[{r[i].yy, k[gde].yy}]==0) kolko++;
                    mat[{r[i].yy, k[gde].yy}]+=r[i].xx.yy-kk;
                    howmuch+=r[i].xx.yy-kk;
                    break;
                }
                else if(r[i].xx.yy-kk==k[gde].xx.yy-howmuch) {
                    if(mat[{r[i].yy, k[gde].yy}]==0) kolko++;
                    mat[{r[i].yy, k[gde].yy}]+=r[i].xx.yy-kk;
                    gde++;
                    howmuch=k[gde].xx.xx;
                    break;
                }
                else {
                    if(mat[{r[i].yy, k[gde].yy}]==0) kolko++;
                    mat[{r[i].yy, k[gde].yy}]+=k[gde].xx.yy-howmuch;
                    kk+=k[gde].xx.yy-howmuch;
                    gde++;
                    howmuch=k[gde].xx.xx;
                }
            }
            kkk=0;
        }

        cout << kolko << "\n";
        for(auto x:mat) {
            cout << x.xx.xx << ' ' << x.xx.yy << ' ' << x.yy << "\n";
        }
    }
    else {
        cout << zbirk << "\n";
        sort(k+1, k+1+m, [](const pair<pii, int>& A, const pair<pii, int>& B) {return A.xx.yy<B.xx.yy;});
        sort(r+1, r+1+n, [](const pair<pii, int>& A, const pair<pii, int>& B) {return A.xx.xx>B.xx.xx;});

        int gde=1, howmuch=0, dokle=-1, kkk=-1;
        for(int i=1; i<=m; i++) {
            int kk=0;
            bool napolje=0;
            while(true) {
                if(r[gde].xx.xx==0) {
                    napolje=1;
                    break;
                }
                if(k[i].xx.yy-kk<r[gde].xx.xx-howmuch) {
                    if(mat[{r[gde].yy, k[i].yy}]==0) kolko++;
                    mat[{r[gde].yy, k[i].yy}]+=k[i].xx.yy-kk;
                    howmuch+=k[i].xx.yy-kk;
                    break;
                }
                else if(k[i].xx.yy-kk==r[gde].xx.xx-howmuch) {
                    if(mat[{r[gde].yy, k[i].yy}]==0) kolko++;
                    mat[{r[gde].yy, k[i].yy}]+=k[i].xx.yy-kk;
                    gde++;
                    howmuch=0;
                    break;
                }
                else {
                    if(mat[{r[gde].yy, k[i].yy}]==0) kolko++;
                    mat[{r[gde].yy, k[i].yy}]+=r[gde].xx.xx-howmuch;
                    kk+=r[gde].xx.xx-howmuch;
                    gde++;
                    howmuch=0;
                }
            }
            dokle=i;
            kkk=kk;
            if(napolje) break;
        }
        sort(r+1, r+1+n, [](const pair<pii, int>& A, const pair<pii, int>& B) {return A.xx.yy-A.xx.xx>B.xx.yy-B.xx.xx;});
        gde=1, howmuch=r[gde].xx.xx;
        for(int i=dokle; i<=m; i++) {
            int kk=kkk;
            while(true) {
                if(k[i].xx.yy-kk<r[gde].xx.yy-howmuch) {
                    if(mat[{r[gde].yy, k[i].yy}]==0) kolko++;
                    mat[{r[gde].yy, k[i].yy}]+=k[i].xx.yy-kk;
                    howmuch+=k[i].xx.yy-kk;
                    break;
                }
                else if(k[i].xx.yy-kk==r[gde].xx.yy-howmuch) {
                    if(mat[{r[gde].yy, k[i].yy}]==0) kolko++;
                    mat[{r[gde].yy, k[i].yy}]+=k[i].xx.yy-kk;
                    gde++;
                    howmuch=r[gde].xx.xx;
                    break;
                }
                else {
                    if(mat[{r[gde].yy, k[i].yy}]==0) kolko++;
                    mat[{r[gde].yy, k[i].yy}]+=r[gde].xx.yy-howmuch;
                    kk+=r[gde].xx.yy-howmuch;
                    gde++;
                    howmuch=r[gde].xx.xx;
                }
            }
            kkk=0;
        }

        cout << kolko << "\n";
        for(auto x:mat) {
            cout << x.xx.xx << ' ' << x.xx.yy << ' ' << x.yy << "\n";
        }
    }
    return 0;
}