#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
set<int> a[N];
int n, m;
pair<int, int> b[N], c[N];
void solve(int to, int from, int k) {
  int x = -1;
  while (k > 0) {
    auto itL = a[from].upper_bound(x);
    if (itL == a[from].end()) throw;
    int y = *itL;
    auto itS = a[to].upper_bound(x);
    while (itS != a[to].end() && *itS < y) itS++;
    if (itS == a[to].end() || *itS > y) {
      k--;
      cout << from + 1 << " " << to + 1 << " " << y << "\n";
      a[to].insert(y);
      a[from].erase(y);
    }
    x = y;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  m = 0;
  for (int i = (0); i < (n); i++) {
    int k;
    cin >> k;
    b[i] = {k, i};
    m += k;
    while (k--) {
      int x;
      cin >> x;
      a[i].insert(x);
    }
  }
  sort(b, b + n);
  reverse(b, b + n);
  for (int i = (0); i < (n); i++) {
    int z = m / n;
    z += (i < (m % n));
    c[i] = {b[i].first - z, b[i].second};
  }
  sort(c, c + n);
  int res = 0;
  for (int i = (0); i < (n); i++)
    if (c[i].first > 0) res += c[i].first;
  cout << res << "\n";
  int L = 0, R = n - 1;
  while (L < R) {
    while (L < R && c[L].first == 0) L++;
    while (L < R && c[R].first == 0) R--;
    if (L >= R) break;
    int z = min(-c[L].first, c[R].first);
    c[L].first += z;
    c[R].first -= z;
    solve(c[L].second, c[R].second, z);
  }
  return 0;
}
