#include <bits/stdc++.h>
using namespace std;
struct el {
  int ind;
  set<int> a;
} a[100002];
struct ans {
  int from, to, kind;
};
bool operator<(const el& a, const el& b) { return a.a.size() < b.a.size(); }
vector<ans> res;
int main() {
  int n, m;
  int q = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int s;
    cin >> s;
    while (s--) {
      int x;
      cin >> x;
      a[i].a.insert(x);
    }
    q += a[i].a.size();
    a[i].ind = i;
  }
  sort(a, a + n);
  int l = 0, r = n - 1;
  int k = q / n;
  q = q % n;
  while (l < r) {
    set<int>& L = a[l].a;
    set<int>& R = a[r].a;
    int ls = k + (l + q >= n), rs = k + (r + q >= n);
    if (L.size() == ls && R.size() == rs) break;
    for (auto it = R.begin(); it != R.end();) {
      if (L.size() == ls) break;
      if (R.size() == rs) break;
      if (L.insert(*it).second) {
        res.push_back({a[r].ind + 1, a[l].ind + 1, *it});
        it = R.erase(it);
      } else
        it++;
    }
    if (L.size() == ls) l++;
    if (R.size() == rs) r--;
  }
  cout << res.size() << '\n';
  for (auto i : res) {
    cout << i.from << ' ' << i.to << ' ' << i.kind << '\n';
  }
  return 0;
}
