#include <bits/stdc++.h>
using namespace std;
const int UB = 500000;
vector<tuple<int, int, int>> res;
void func(int n, int m, vector<set<int>> &kss, int lb, int ub) {
  vector<int> cnt(n);
  for (int i = 0; i < n; i++) cnt[i] = kss[i].size();
  vector<vector<int>> pats(m);
  for (int i = 0; i < n; i++)
    if (cnt[i] > ub)
      for (auto k : kss[i]) pats[k].push_back(i);
  set<int> ls;
  for (int i = 0; i < n; i++)
    if (cnt[i] < lb) ls.insert(i);
  for (int i = 0; i < m; i++) {
    int idx = 0;
    auto it = ls.begin();
    vector<int> added;
    while (idx < pats[i].size() && it != ls.end()) {
      if (cnt[pats[i][idx]] == ub) {
        idx++;
        continue;
      }
      if (kss[*it].count(i)) {
        it++;
        continue;
      }
      res.emplace_back(pats[i][idx] + 1, (*it) + 1, i + 1);
      kss[pats[i][idx]].erase(i);
      kss[*it].insert(i);
      cnt[pats[i][idx]]--;
      cnt[*it]++;
      added.push_back(*it);
      it++, idx++;
    }
    for (auto a : added)
      if (cnt[a] == lb) ls.erase(a);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<set<int>> kss(n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    int s;
    cin >> s;
    sum += s;
    while (s--) {
      int kind;
      cin >> kind;
      kind--;
      kss[i].insert(kind);
    }
  }
  int lb = sum / n;
  int ub = sum / n + (int)(sum % n != 0);
  func(n, m, kss, lb, ub);
  func(n, m, kss, ub, ub);
  cout << res.size() << endl;
  for (auto &e : res) {
    cout << get<0>(e) << " " << get<1>(e) << " " << get<2>(e) << endl;
  }
  return 0;
}
