#include <bits/stdc++.h>

using namespace std;
struct polje
{
    int x;
    int y;
    long long vr;
};
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m;
    cin>>n>>m;
    long long k=0,v=0;
    vector<long long> a(n);
    vector<long long> b(n);
    vector<long long> c(m);
    vector<long long> d(m);
    vector<polje> rez(1000000);
    bool ab=true;
    int pb=0;
    for(int i=0;i<n;i++)
    {
        cin>>a[i]>>b[i];
        if(a[i]!=b[i]) ab=false;
        k+=b[i];
    }
    for(int i=0;i<m;i++)
    {
        cin>>c[i]>>d[i];
        v+=d[i];
    }
    if(n==m)
    {
        cout<<k<<endl;
        cout<<n<<endl;
        for(int i=0;i<n;i++) cout<<i+1<<" "<<i+1<<" "<<b[i]<<endl;
    }

    else if(ab)
    {
        cout<<k<<endl;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {

            }
        }
    }
}