#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m;
    cin >> n >> m;
    int N[n];
    int M[m];
    vector<vector<int> > field(n,vector<int> (m,0));
    int a[n],b[n],c[m],d[m];
    for(int i=0;i<n;i++){
        N[i]=0;
        cin >> a[i] >> b[i];
    }
    for(int i=0;i<m;i++){
        M[i]=0;
        cin >> c[i] >> d[i];
    }
    long long sum=0;
    int k=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(b[i]-N[i]<d[j]-M[j]){
                field[i][j] = b[i]-N[i];
                M[j] += b[i]-N[i];
                N[i] = b[i]-N[i];
            } else{
                field[i][j] = d[j]-M[j];
                N[i] += d[j]-M[j];
                M[i] = d[j]-M[j];
            }
            if(field[i][j]>0) k++;
            sum+=field[i][j];
        }
    }
    cout << sum << endl;
    cout << k << endl;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(field[i][j]>0)
            cout << i << " " << j << endl;
        }
    }

    /*min strat*/

    //cout << "Hello world!" << endl;
    return 0;
}