#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
int n, m, s, p, q, l, k;
struct node {
  int x, y, z;
} a[N];
set<int> v[N];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) f ^= ch == '-', ch = getchar();
  while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
  return f ? x : -x;
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= n; i++) {
    int x = read();
    s += x;
    while (x--) v[i].insert(read());
  }
  l = s / n, p = s % n, q = n - p;
  for (int i = 1, j = 1;;) {
    while (i <= n && v[i].size() >= l + !q) {
      if (v[i].size() == l) q--;
      i++;
    }
    while (j <= n && v[j].size() <= l + (bool)p) {
      if (v[j].size() == l + 1) p--;
      j++;
    }
    if (i > n || j > n) break;
    set<int>::iterator it = v[j].begin();
    int t = min(l - v[i].size() + !q, v[j].size() - l - (bool)p);
    while (t) {
      int x = *it;
      if (*v[i].lower_bound(x) != x)
        t--, v[i].insert(x), v[j].erase(it), a[++k] = (node){j, i, x},
                                             it = v[j].lower_bound(x);
      else
        it++;
    }
  }
  printf("%d", k);
  for (int i = 1; i <= k; i++) printf("\n%d %d %d", a[i].x, a[i].y, a[i].z);
  return 0;
}
