#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 222;
set<int> ori[maxn];
vector<int> ids[maxn];
int top, bot;
int dest[maxn];
int have[maxn];
int occ[maxn];
int ans[maxn][3];
int arr = 0;
int flag[maxn];
vector<pair<int, int> > pp;
void solve() {
  int n, m;
  scanf("%d %d", &n, &m);
  int tot = 0;
  for (int i = 1; i <= n; i++) {
    int s;
    scanf("%d", &s);
    tot += s;
    int a;
    for (int j = 1; j <= s; j++) {
      scanf("%d", &a);
      ori[i].insert(a);
    }
    pp.push_back(make_pair(s, i));
  }
  sort(pp.begin(), pp.end());
  reverse(pp.begin(), pp.end());
  bot = tot / n;
  top = bot;
  if (tot % n) top++;
  int topcnt = tot % n;
  for (int i = 0; i < pp.size(); i++) {
    if (i < topcnt)
      dest[pp[i].second] = top;
    else
      dest[pp[i].second] = bot;
  }
  for (int i = 1; i <= n; i++) {
    if (ori[i].size() < dest[i]) flag[i] = 1;
  }
  set<int> se;
  for (int i = 1; i <= n; i++) {
    if (ori[i].size() > dest[i]) {
      for (int a : ori[i]) {
        occ[a]++;
        se.insert(a);
        ids[a].push_back(i);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (flag[i]) {
      int needcnt = dest[i] - ori[i].size();
      for (int a : ori[i]) have[a] = 1;
      vector<int> needdel;
      for (int id : se) {
        if (!have[id] && occ[id] > 0) {
          while (ori[ids[id].back()].size() == dest[ids[id].back()]) {
            ids[id].pop_back();
          }
          int fid = ids[id].back();
          ids[id].pop_back();
          arr++;
          ans[arr][0] = fid;
          ans[arr][1] = i;
          ans[arr][2] = id;
          ori[fid].erase(id);
          occ[id]--;
          if (occ[id] == 0) needdel.push_back(id);
          if (ori[fid].size() == dest[fid]) {
            for (int a : ori[fid]) {
              occ[a]--;
              if (occ[a] == 0) needdel.push_back(a);
            }
          }
          needcnt--;
        }
        if (needcnt == 0) break;
      }
      for (int a : ori[i]) have[a] = 0;
      for (int a : needdel) se.erase(a);
    }
  }
  cout << arr << endl;
  for (int i = 1; i <= arr; i++) {
    printf("%d %d %d\n", ans[i][0], ans[i][1], ans[i][2]);
  }
}
signed main() { solve(); }
