#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main(int argc, char** argv) {
	ios_base::sync_with_stdio(false);
	
	int n,m;
	cin >>n>>m;
	if(n==1)
	{
        for(;;)
        {
            
        }
		int a,mx;
		cin >>a>>mx;
		int z=0;
		int br=0;
		vector<pair<int,int> >v(m);
		for(int i=0;i<m;i++)
		{
			cin >>v[i].first>>v[i].second;
			z+=v[i].first;
		}
		for(int i=0;i<m;i++)
		{
		//	cout <<v[i].first<<" "<<v[i].second<<"\n";
		}
		vector<int > v2;
		int i=0;
		while(i<m && z!=mx)
		{
			//cout <<i<<" "<<v[i].first<<" "<<v[i].second<<"\n";
			if(v[i].second!=0)
			{
				//cout <<"ulazi\n";
				if(z+v[i].second-v[i].first<=mx)
				{
					v2.push_back(v[i].second-v[i].first);
					z+=v[i].second-v[i].first;
				}
				else
				{
					v2.push_back(mx-z);
					z=mx;
				}
			}
			else
			{
				v2.push_back(0);	
			}
			i++;
		}
		while(v2.size()<v.size())
		{
			v2.push_back(0);
		}
		cout <<z<<"\n";
		vector<pair<pair<int,int>, int> >v3;
		for(int i=0;i<v2.size();i++)
		{
			//cout <<v2[i]<<" "<<v[i].first<<"\n";
			if(v2[i]+v[i].first>0)
			{
				v3.push_back(make_pair(make_pair(1,i+1),v2[i]+v[i].first));
			}
		}
		cout <<v3.size()<<"\n";
		for(int i=0;i<v3.size();i++)
		{
			cout <<v3[i].first.first<<" "<<v3[i].first.second<<" "<<v3[i].second<<"\n";
		}
	}
	else
	{
		//cout <<"asdf\n";
		int z=0;
		int a=0,b=0;
		int sti[n];
		for(int i=0;i<n;i++)
		{
			int x;
			cin >>sti[i]>>x;
			a+=sti[i];
			//z+=sti[i];
		}
		//cout <<"fff\n";
		int stj[m];
		for(int j=0;j<m;j++)
		{
			int x;
			cin >>stj[j]>>x;
			b+=stj[j];
			//z+=stj[j];
		}
		//cout <<"rererer\n";
		//cout <<z<<"\n";
		//int i=0,j=0;
		
		vector<pair<pair<int,int>,int > >v3;
		int i=0,j=0;
		while(i<n && j<m)
		{
			
			while(sti[i]==0 && i<n)
				{
					i++;
				}
				while(stj[j]==0 && j<m)
				{
					j++;
				}
			if(i>=n || j>=m)
			{
				break;
			}
			
			int x=min(sti[i],stj[j]);
			sti[i]-=x;
			stj[j]-=x;
			z+=x;
			v3.push_back(make_pair(make_pair(i+1,j+1),x));
		}
		cout <<z<<"\n";
	cout <<v3.size()<<"\n";
	for(int i=0;i<v3.size();i++)
	{
		cout <<v3[i].first.first<<" "<<v3[i].first.second<<" "<<v3[i].second<<"\n";
	}
	}
	
	
	return 0;
}