#include <bits/stdc++.h>

using namespace std;

int main()
{
	int N,M;
	cin>>N>>M;
	if(N==1)
	{
		int a;
		int b;
		cin>>a>>b;
		int R[M];
		int u=0;
		int K=0;
		for(int i=0;i<M;i++)
		{
			int c;
			int d;
			cin>>c>>d;
			int minimum =max(a,c);
			int maksimum =min(d,b);
			if(minimum<maksimum)
			{
				R[i]=maksimum;
				u+=maksimum;
				K+=1;
			}
			else
			{
				R[i]=0;
			}
			
		}
		cout<<u<<endl<<K<<endl;
		for(int i=0;i<M;i++)
		{
			if(R[i]!=0)
			{
				cout<<1<<" "<<i+1<<" "<<R[i];
			}
		}
		return 0;
	}
	
	bool abjednaki=true;
	bool acnule=true;
	
	int a[N];
	int b[N];
	for(int i=0;i<N;i++)
	{
		cin>>a[i]>>b[i];
		if(a[i]!=b[i])
		{
			abjednaki=false;
		}
		if(a!=0)
		{
			acnule=false;
		}
	}
	bool cdjednaki=true;
	
	int c[M];
	int d[M];
	for(int i=0;i<M;i++)
	{
		cin>>c[i]>>d[i];
		if(c[i]!=d[i])
		{
			cdjednaki=false;
		}
		if(c!=0)
		{
			acnule=false;
		}
	}
	
	if((cdjednaki==true)&&(abjednaki==true))
	{	
	int u=0;
		int K=0;
		vector<int> x;
		vector<int> y;
		vector<int> n;
		for(int i=0;i<N;i++)
		{
			for(int j=0;j<M;j++)
			{
				if(a[i]==c[j])
				{
					K+=1;
					u+=a[i];
					n.push_back(a[i]);
					x.push_back(i+1);
					y.push_back(j+1);
				}
			}
		}
		cout<<u<<endl<<K<<endl;
		for(int i=0;i<K;i++)
		{
			cout<<x[i]<<" "<<y[i]<<" "<<n[i]<<endl;
		}
		return 0;
	}
	
	if(abjednaki==true)
	{
			int u=0;
		int K=0;
		vector<int> x;
		vector<int> y;
		vector<int> n;
		for(int i=0;i<N;i++)
		{
			for(int j=0;j<M;j++)
			{
				if((a[i]<=d[j])&&(a[i]>=c[j]))
				{
					K+=1;
					u+=a[i];
					n.push_back(a[i]);
					x.push_back(i+1);
					y.push_back(j+1);
				}
			}
		}
		cout<<u<<endl<<K<<endl;
		for(int i=0;i<K;i++)
		{
			cout<<x[i]<<" "<<y[i]<<" "<<n[i]<<endl;
		}
		return 0;
	}
	
	if(acnule==true)
	{
			int u=0;
		int K=0;
		vector<int> x;
		vector<int> y;
		vector<int> n;
		for(int i=0;i<N;i++)
		{
			for(int j=0;j<M;j++)
			{
				if(min(b[i],d[j])!=0)
				{
					K+=1;
					u+=min(b[i],d[j]);
					n.push_back(min(b[i],d[j]));
					x.push_back(i+1);
					y.push_back(j+1);
				}
			}
		}
		cout<<u<<endl<<K<<endl;
		for(int i=0;i<K;i++)
		{
			cout<<x[i]<<" "<<y[i]<<" "<<n[i]<<endl;
		}
		return 0;
	}
	else
	{
		
		int u=0;
		int K=0;
		vector<int> x;
		vector<int> y;
		vector<int> n;
		for(int i=0;i<N;i++)
		{
			for(int j=0;j<M;j++)
			{
				if(b[i]!=0)
				{
					K+=1;
					u+=b[i];
					n.push_back(b[i]);
					x.push_back(i+1);
					y.push_back(j+1);
				}
			}
		}
		cout<<u<<endl<<K<<endl;
		for(int i=0;i<K;i++)
		{
			cout<<x[i]<<" "<<y[i]<<" "<<n[i]<<endl;
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}