#include <bits/stdc++.h>
using namespace std;
int h[2][100005], n, m, tot = 0, ans[5 * 100005][3], fin = 0;
set<int> trans;
set<int> hv[100005];
void transR(int id, int val) {
  if (!h[0][id] && h[1][id]) trans.erase(id);
  h[1][id] += val;
  if (!h[0][id] && h[1][id]) trans.insert(id);
}
void transL(int id, int val) {
  if (!h[0][id] && h[1][id]) trans.erase(id);
  h[0][id] += val;
  if (!h[0][id] && h[1][id]) trans.insert(id);
}
void give(int from, int to, int g) {
  hv[from].erase(g);
  hv[to].insert(g);
  transR(g, -1);
  transL(g, 1);
  ans[fin][0] = from, ans[fin][1] = to, ans[fin][2] = g;
  fin++;
}
int main() {
  memset(h, 0, sizeof(h));
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    tot += a;
    for (int j = 0; j < a; j++) {
      int b;
      cin >> b;
      hv[i].insert(b);
    }
  }
  vector<int> v(n + 5);
  for (int i = 1; i <= n; i++) v[i] = i;
  sort(v.begin() + 1, v.begin() + n + 1, [&](int &a, int &b) {
    return (int)(hv[a].size()) < (int)(hv[b].size());
  });
  for (auto &i : hv[v[n]]) transR(i, 1);
  for (auto &i : hv[v[1]]) transL(i, 1);
  int tgt = tot / n, rem = tot % n;
  for (int i = 1, j = n; i < j;) {
    int tr = tgt;
    if (n - j + 1 <= rem) tr++;
    if (hv[v[j]].size() == tr) {
      for (auto &k : hv[v[j]]) transR(k, -1);
      j--;
      for (auto &k : hv[v[j]]) transR(k, 1);
      continue;
    }
    int tl = tgt;
    if (n - i + 1 <= rem) tl++;
    if (hv[v[i]].size() == tl) {
      for (auto &k : hv[v[i]]) transL(k, -1);
      i++;
      for (auto &k : hv[v[i]]) transL(k, 1);
      continue;
    }
    give(v[j], v[i], (*trans.begin()));
  }
  cout << fin << '\n';
  for (int i = 0; i < fin; i++)
    cout << ans[i][0] << ' ' << ans[i][1] << ' ' << ans[i][2] << '\n';
}
