#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e6 + 10;
const long long INF = 1e9 + 10;
const int MOD = 998244353;
int n, m, sum;
vector<set<int> > a;
set<pair<int, int> > st;
vector<pair<pair<int, int>, int> > ans;
void move(int from, int to, int cnt) {
  st.erase({a[from].size(), from});
  st.erase({a[to].size(), to});
  vector<int> ers;
  for (auto cur : a[from]) {
    if (a[to].find(cur) == a[to].end()) {
      cnt--;
      ers.push_back(cur);
      if (cnt == 0) break;
    }
  }
  for (auto cur : ers) {
    ans.push_back({{from + 1, to + 1}, cur});
    a[from].erase(cur);
    a[to].insert(cur);
  }
  st.insert({a[from].size(), from});
  st.insert({a[to].size(), to});
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    int sz;
    cin >> sz;
    sum += sz;
    st.insert({sz, i});
    while (sz--) {
      int x;
      cin >> x;
      a[i].insert(x);
    }
  }
  while (true) {
    pair<int, int> fi = *st.begin(), se = *st.rbegin();
    if (se.first - fi.first < 2) break;
    if (sum % n != 0) {
      int cnt = min(sum / n - fi.first, se.first - sum / n - 1);
      cnt = max(cnt, 1);
      move(se.second, fi.second, cnt);
    } else {
      int cnt = min(sum / n - fi.first, se.first - sum / n);
      move(se.second, fi.second, cnt);
    }
  }
  cout << ans.size() << endl;
  for (auto cur : ans)
    cout << cur.first.first << ' ' << cur.first.second << ' ' << cur.second
         << '\n';
  return 0;
}
