#include <bits/stdc++.h>
using namespace std;
int n,m;
long long redl[50007],redd[50007],koll[50007],kold[50007];
long long  res[50007];
long long resx[80007],resy[80007],resn[80007];
int main(){
	scanf("%d %d",&n,&m);
	if(n==1){
	   long long l,d;
	   cin>>l>>d;
	   long long suma_minimuma=0;
	   for(int i=1;i<=m;i++){
				 cin>>res[i]>>kold[i];
				 kold[i]-=res[i];
				 suma_minimuma+=res[i];
	   }
	   long long ostatak=d-suma_minimuma;
	   for(int i=1;i<=m;i++){
				 if(ostatak==0){
						break;
				 }
				 if(ostatak>=kold[i]){
						res[i]+=kold[i];
						ostatak-=kold[i];
				 }
				 else{
						res[i]+=ostatak;
						ostatak=0;
				 }
	   }
	   int br=0;
	   long long suma=0;
	   for(int i=1;i<=m;i++){
				 if(res[i]>0){
						br++;
						suma+=res[i];
				 }
	   }
	   cout<<suma<<endl;
	   cout<<br<<endl;
	   for(int i=1;i<=m;i++){
				 if(res[i]>0){
						cout<<"1 "<<i<<" "<<res[i]<<endl;
				 }
	   }
	   return 0;
	}
	for(int i=1;i<=n;i++){
			int a,b;
			cin>>a>>redd[i]>>a>>kold[i];
	}
	long long suma=0;
	int br=0;
	for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
					if(kold[j]<=redd[i] and kold[j]>0){
						 suma+=kold[j];
						 br++;
						 resx[br]=i;
						 resy[br]=j;
						 resn[br]=kold[j];
						 redd[i]-=kold[j];
						 kold[j]=0;
					}
			}
	}
	cout<<suma<<endl;
	cout<<br<<endl;
	for(int i=1;i<=br;i++){
			cout<<resx[i]<<" "<<resy[i]<<" "<<resn[i]<<endl;
	}




	return 0;
}
/*/
1 3
1 8
1 2
1 3
1 4



/*/