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