#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, mod = 1e9 + 7;
const long long inf = 1e18;
vector<pair<pair<int, int>, int> > ans;
set<int> s[maxn];
vector<int> st;
set<pair<int, int> > ssz;
int main() {
  int n, m, sm = 0;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    int cnt;
    scanf("%d", &cnt);
    sm += cnt;
    while (cnt--) {
      int x;
      scanf("%d", &x);
      s[i].insert(x);
    }
    ssz.insert({int((s[i]).size()), i});
  }
  int N1 = sm / n, N2 = (sm + n - 1) / n;
  while (abs((ssz.begin()->first) - (ssz.rbegin()->first)) > 1) {
    int id1 = ssz.begin()->second, id2 = ssz.rbegin()->second,
        S1 = int((s[id1]).size()), S2 = int((s[id2]).size());
    ssz.erase(ssz.begin());
    ssz.erase(--ssz.end());
    st.clear();
    for (int x : s[id1]) {
      if (s[id2].count(x)) st.push_back(x);
      s[id2].erase(x);
    }
    if (S1 == N1 || S2 == N2) {
      ans.push_back({{id2, id1}, *s[id2].begin()});
      s[id1].insert(*s[id2].begin());
      s[id2].erase(s[id2].begin());
    }
    while (S1 < N1 && S2 > N2) {
      ans.push_back({{id2, id1}, *s[id2].begin()});
      s[id1].insert(*s[id2].begin());
      s[id2].erase(s[id2].begin());
      S1++;
      S2--;
    }
    for (int x : st) s[id2].insert(x);
    ssz.insert({int((s[id1]).size()), id1});
    ssz.insert({int((s[id2]).size()), id2});
  }
  printf("%d\n", int((ans).size()));
  for (auto it : ans)
    printf("%d %d %d\n", it.first.first + 1, it.first.second + 1, it.second);
  return 0;
}
