#include <bits/stdc++.h>
#define MAXN 50010

using namespace std;

typedef long long ll;

pair<ll, ll> x[MAXN];
pair<ll, ll> y[MAXN];
vector<ll> rx;
vector<ll> ry;
vector<ll> rn;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll n, m;
    cin >> n >> m;
    ll sb = 0, sd = 0, sa = 0, sc = 0;
    for(int i = 1; i <= n; i++)
    {
        cin >> x[i].first >> x[i].second;
        sb += x[i].second;
        sa += x[i].first;
    }
    for(int i = 1; i <= m; i++)
    {
        cin >> y[i].first >> y[i].second;
        sd += y[i].second;
        sc += y[i].first;
    }
    ll j = 1;
    ll k;
    if(sb <= sd)
    {
        cout << sb << "\n";
        for(int i = 1; i <= n; i++)
        {
            k = x[i].second;
            if(sc == 0)
            {
                while(k > 0)
                {
                    if(k >= y[j].second && y[j].second != 0)
                    {
                        rx.push_back(i);
                        ry.push_back(j);
                        rn.push_back(y[j].second);
                        k -= y[j].second;
                        j++;
                    }
                    else
                    {
                        rx.push_back(i);
                        ry.push_back(j);
                        rn.push_back(k);
                        y[j].second -= k;
                        k = 0;
                    }
                }
            }
            else
            {
                if(sc >= k)
                {
                    sc -= k;
                    while(k > 0)
                    {
                        if(k >= y[j].first && y[j].first != 0)
                        {
                            rx.push_back(i);
                            ry.push_back(j);
                            rn.push_back(y[j].first);
                            k -= y[j].first;
                            y[j].second -= y[j].first;
                            j++;
                        }
                        else
                        {
                            rx.push_back(i);
                            ry.push_back(j);
                            rn.push_back(k);
                            y[j].first -= k;
                            y[j].second -= k;
                            k = 0;
                        }
                    }
                }
                else
                {
                    vector<pair<ll, ll>> pomYN;
                    sc = 0;
                    while(j <= m)
                    {
                        if(y[j].first != 0)
                        {
                            pomYN.push_back({j, y[j].first});
                            k -= y[j].first;
                            y[j].second -= y[j].first;
                        }
                        j++;
                    }
                    j = 1;
                    while(k > 0)
                    {
                        if(k >= y[j].second && y[j].second != 0)
                        {
                            pomYN.push_back({j, y[j].second});
                            k -= y[j].second;
                            j++;
                        }
                        else
                        {
                            pomYN.push_back({j, k});
                            y[j].second -= k;
                            k = 0;
                        }
                    }
                    sort(pomYN.begin(), pomYN.end());
                    int L = pomYN.size();
                    for(int g = 0; g < L - 1; g++)
                    {
                        if(pomYN[g].first == pomYN[g + 1].first)
                        {
                            rx.push_back(i);
                            ry.push_back(pomYN[g].first);
                            rn.push_back(pomYN[g].second + pomYN[g + 1].second);
                            g++;
                        }
                        else
                        {
                            rx.push_back(i);
                            ry.push_back(pomYN[g].first);
                            rn.push_back(pomYN[g].second);
                        }
                    }
                    if(pomYN[L - 2].first != pomYN[L - 1].first)
                    {
                        rx.push_back(i);
                        ry.push_back(pomYN[L - 1].first);
                        rn.push_back(pomYN[L - 1].second);
                    }
                }
            }
        }
        ll N = rx.size();
        cout << N << "\n";
        for(int i = 0; i < N; i++)
        {
            cout << rx[i] << " " << ry[i] << " " << rn[i] << "\n";
        }
    }
    else
    {
        cout << sd << "\n";
        for(int i = 1; i <= m; i++)
        {
            k = y[i].second;
            if(sa == 0)
            {
                while(k > 0)
                {
                    if(k >= x[j].second && x[j].second != 0)
                    {
                        rx.push_back(j);
                        ry.push_back(i);
                        rn.push_back(x[j].second);
                        k -= x[j].second;
                        j++;
                    }
                    else
                    {
                        rx.push_back(j);
                        ry.push_back(i);
                        rn.push_back(k);
                        x[j].second -= k;
                        k = 0;
                    }
                }
            }
            else
            {
                if(sa >= k)
                {
                    sa -= k;
                    while(k > 0)
                    {
                        if(k >= x[j].first && x[j].first != 0)
                        {
                            rx.push_back(j);
                            ry.push_back(i);
                            rn.push_back(x[j].first);
                            k -= x[j].first;
                            x[j].second -= x[j].first;
                            j++;
                        }
                        else
                        {
                            rx.push_back(j);
                            ry.push_back(i);
                            rn.push_back(k);
                            x[j].first -= k;
                            x[j].second -= k;
                            k = 0;
                        }
                    }
                }
                else
                {
                    vector<pair<ll, ll>> pomYN;
                    sa = 0;
                    while(j <= n)
                    {
                        if(x[j].first != 0)
                        {
                            pomYN.push_back({j, x[j].first});
                            k -= x[j].first;
                            x[j].second -= x[j].first;
                        }
                        j++;
                    }
                    j = 1;
                    while(k > 0)
                    {
                        if(k >= x[j].second && x[j].second != 0)
                        {
                            pomYN.push_back({j, x[j].second});
                            k -= x[j].second;
                            j++;
                        }
                        else
                        {
                            pomYN.push_back({j, k});
                            x[j].second -= k;
                            k = 0;
                        }
                    }
                    sort(pomYN.begin(), pomYN.end());
                    int L = pomYN.size();
                    for(int g = 0; g < L - 1; g++)
                    {
                        if(pomYN[g].first == pomYN[g + 1].first)
                        {
                            ry.push_back(i);
                            rx.push_back(pomYN[g].first);
                            rn.push_back(pomYN[g].second + pomYN[g + 1].second);
                            g++;
                        }
                        else
                        {
                            ry.push_back(i);
                            rx.push_back(pomYN[g].first);
                            rn.push_back(pomYN[g].second);
                        }
                    }
                    if(pomYN[L - 2].first != pomYN[L - 1].first)
                    {
                        ry.push_back(i);
                        rx.push_back(pomYN[L - 1].first);
                        rn.push_back(pomYN[L - 1].second);
                    }
                }
            }
        }
        ll N = rx.size();
        cout << N << "\n";
        for(int i = 0; i < N; i++)
        {
            cout << rx[i] << " " << ry[i] << " " << rn[i] << "\n";
        }
    }
    return 0;
}