#include <bits/stdc++.h>

int main()
{
    using namespace std;
 
    long long N, M;
    cin >> N >> M;
    vector<long long> Levo(N);
    vector<long long> Levo_Min(N);   
    vector<long long> Desno(M);
    vector<long long> Desno_Min(M);
    vector<long long> Solutions_Number;
    long long Solution;
    map<pair<long long, long long>,long long> Solutions;
    for(int i = 0; i < N; i++)
    { 
        cin >> Desno_Min[i] >> Desno[i];
    }
    for(int i = 0; i < M; i++)
    { 
        cin >> Levo_Min[i] >> Levo[i];
    }
    long long right = 0, left = 0;
    while(left < N )
    { 
        long long Solver = 0;
        while((Levo_Min[left] - Solver) > 0  )
        { 

            if(Levo_Min[left] > Desno[right] + Solver)
            { 
                Solutions[(make_pair(left, right))] += Desno[right];
                Solver += Desno[right];
                right++;
            }
            else
            { 
                Desno[right] -= Levo_Min[left] - Solver;
                Solution += Levo_Min[left];
                Solutions[make_pair(left, right)] += Levo_Min[left] - Solver;
                Solver = Levo_Min[left];
            }        
        }
        left++;
    }
for(int i = 0; i < N; i++)
{ 
    long long number = Levo[i] - Levo_Min[i];
    while(right < M && number > 0)
    { 
        if(Desno[right] > number)
        { 
            Desno[right] -= number;
            Solution += number;
            Solutions_Number.push_back(number);
            Solutions[make_pair(i, right)]+= number;
            number-= Desno[right];
        }
        else
        {   
            Solutions[make_pair(i, right)] += Desno[right];
            number -= Desno[right];
            Solution+= Desno[right];
            right++;
        } 
    }
}
while (right < M)
{
    while(Desno_Min[right] > 0)
    { 
        for(  auto &p : Solutions)
        { 
        
            if(p.second > Desno_Min[p.first.second])
            { 
                int number = p.second - Desno_Min[p.first.second];
                if(Desno_Min[right] > number)
                { 
                    p.second -= number;
                    Desno_Min[right] -= number;
                    Solutions[make_pair(p.first.first, right)] += number;
                }   
                else
                { 
                    p.second -= Desno_Min[right];
                    Solutions[make_pair(p.first.first, right)] += Desno_Min[right];
                    Desno_Min[right] = 0;
                }
            }
        }
    }
    right++;

}


cout << Solution << endl;
long long br = 0;
for(const  auto &p : Solutions)
{ 
    if(p.second != 0)
    { 
        br++;    
    }
        
}
cout << br << endl;
for(const  auto &p : Solutions)
{ 
    if(p.second != 0)
    { 
        cout << p.first.first + 1<<" " <<p.first.second + 1<<" "<< p.second << endl;   
    }
        
    }

    return 0;
}