#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const int N = 110007;
int n, m;
struct node {
  int num;
  int id;
} a[N];
int f[N];
vector<int> pv[N], t;
int tot;
int ans;
int s[N];
int b[N];
int l, r;
bool cmp(node p, node q) { return p.num > q.num; }
void gao(node &x, node &y, int z) {
  int i, o;
  for (i = 0; i < y.num; i++) {
    b[pv[y.id][i]] = 1;
  }
  for (i = x.num - 1; z; i--) {
    int tmp = pv[x.id][i];
    if (b[tmp] == 0) {
      pv[y.id].push_back(tmp);
      y.num++;
      z--;
      printf("%d %d %d\n", x.id, y.id, tmp);
    } else {
      t.push_back(tmp);
    }
    pv[x.id][i] = 0;
    x.num--;
  }
  for (i = 0; i < y.num; i++) b[pv[y.id][i]] = 0;
  for (o = t.size(); o--;) {
    pv[x.id][x.num++] = t[o];
  }
  t.clear();
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int o;
    scanf("%d", &o);
    a[i].num = o;
    a[i].id = i;
    for (int j = 1; j <= o; j++) {
      int x;
      scanf("%d", &x);
      pv[i].push_back(x);
    }
    tot += a[i].num;
  }
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    f[i] = tot / n;
    if (i <= (tot % n)) f[i] += 1;
    if (a[i].num > f[i]) ans += a[i].num - f[i];
  }
  printf("%d\n", ans);
  l = 1;
  r = n;
  while (l < r) {
    int x = a[l].id;
    int y = a[r].id;
    if (a[l].num == f[l]) {
      l++;
      continue;
    }
    if (a[r].num == f[r]) {
      r--;
      continue;
    }
    int z = min(a[l].num - f[l], f[r] - a[r].num);
    gao(a[l], a[r], z);
  }
  return 0;
}
