#include <bits/stdc++.h>

#define vec vector
#define forn(i, n) for (int i = 0; i < n; i++)
#define mp make_pair
#define YY second
#define XX first
#define ll long long

using namespace std;

const bool is_testing = false;

int n, s;
multiset < pair<int, pair<int, int> > > se;

int main()
{
    freopen("castle.in", "r", stdin);
    freopen("castle.out", "w", stdout);
    if (is_testing)
    {
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    }
    cin >> n >> s;
    forn(i, n)
    {
        int z1, z2;
        //cin >> z1 >> z2;
        scanf("%d%d", &z1, &z2);
        se.insert( mp( min(z1, z2), mp(z1, z2) ) );
    }
    while(s > 0)
    {
        s--;
        if (se.size() == 0)
        {
            cout << 0, exit(0);
        }
        pair<int, pair<int, int> > t = *(--se.end());
        cout << t.XX << ' ' << t.YY.XX << ' ' << t.YY.YY << endl;
        se.erase(se.find(t));
        if (t.YY.XX - t.YY.YY > 0)
        {
            cout << "====" << endl;
            t.YY.XX -= t.YY.YY;
            t.XX = min(t.YY.XX, t.YY.YY);
            se.insert(t);
        }
    }
    ll ans = 0;
    cout << endl;
    for (multiset < pair<int, pair<int, int> > > :: iterator it = se.begin(); it != se.end(); it++)
    {
        ans += (ll)((*it).YY.XX);
        cout << (*it).XX << ' ' << (*it).YY.XX << ' ' << (*it).YY.XX << endl;
    }
    cout << ans;
}
