#include <bits/stdc++.h>

using namespace std;


int sum=0;
int sq=0;


vector<vector<int>> restoreGem(vector<int>& r, vector<int>& c)
{
	vector<vector<int> > dp(r.size(),vector<int>(c.size(), 0));

	for (int i = 0; i < r.size(); i++)
    {
		for (int j = 0; j < c.size(); j++)
		{
			int m = min(r[i], c[j]);
			dp[i][j] = m;
			r[i] -= m;
			c[j] -= m;
		}
	}
	return dp;
}

void printMatrix1(vector<vector<int> > ans,int N, int M)
{
	for(int i = 0; i < N; i++)
	{
		for (int j = 0; j < M; j++)
        {
            sum = sum + ans[i][j];
            if(ans[i][j] != 0)
            {
                sq++;
            }
		}
	}
}


void printMatrix2(vector<vector<int> > ans,int N, int M)
{
	for(int i = 0; i < N; i++)
	{
		for (int j = 0; j < M; j++)
        {
            if(ans[i][j] != 0)
            {
                cout<<i+1<<" "<<j+1<<" "<<ans[i][j]<<endl;
            }
		}
	}
}


int main()
{
    int n,m;
    cin>>n>>m;
	vector<int> rowSum;
	vector<int> colSum;


	int dumy;
	for(int i=0;i<n;i++)
    {
        cin>>dumy>>dumy;
        rowSum.push_back(dumy);
    }
    for(int i=0;i<m;i++)
    {
        cin>>dumy>>dumy;
        colSum.push_back(dumy);
    }

	vector<vector<int>> ans = restoreGem(rowSum, colSum);

	printMatrix1(ans, rowSum.size(),colSum.size());

	cout<<sum<<endl<<sq<<endl;

    printMatrix2(ans, rowSum.size(),colSum.size());
    return 0;
}