#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-11;
template <class T>
inline void ckmin(T &a, T b) {
  if (b < a) a = b;
}
template <class T>
inline void ckmax(T &a, T b) {
  if (b > a) a = b;
}
template <class T>
inline T sqr(T x) {
  return x * x;
}
int main() {
  std::ios::sync_with_stdio(false);
  vector<pair<pair<int, int>, int>> ret;
  int n, m;
  cin >> n >> m;
  vector<unordered_set<int>> a(n);
  vector<vector<int>> g(m);
  int total = 0;
  for (int i = 0; i < (n); ++i) {
    int c;
    cin >> c;
    for (int j = 0; j < (c); ++j) {
      int p;
      cin >> p;
      --p;
      a[i].insert(p);
    }
    total += c;
  }
  int c1 = total / n;
  int c2 = (total + n - 1) / n;
  for (int step = 0; step < (3); ++step) {
    int bound, limit;
    if (step == 0)
      bound = c1 - 1, limit = c2 + 1;
    else if (step == 1)
      bound = c1 - 1, limit = c2;
    else
      bound = c1, limit = c2 + 1;
    for (int i = 0; i < (m); ++i) g[i].clear();
    for (int i = 0; i < (n); ++i)
      if (((int)a[i].size()) >= limit)
        for (int x : a[i]) g[x].push_back(i);
    set<int> skey;
    skey.clear();
    for (int i = 0; i < (m); ++i)
      if (((int)g[i].size()) > 0) skey.insert(i);
    for (int i = 0; i < (n); ++i)
      for (int key = 0; ((int)a[i].size()) <= bound; key++) {
        auto it = skey.lower_bound(key);
        if (it == skey.end()) break;
        key = *it;
        if (a[i].count(key)) continue;
        if (((int)g[key].size()) == 0) {
          skey.erase(key);
          continue;
        }
        int idx = g[key].back();
        g[key].pop_back();
        if (a[idx].count(key) && ((int)a[idx].size()) >= limit) {
          assert(a[idx].count(key));
          assert(!a[i].count(key));
          a[idx].erase(key);
          a[i].insert(key);
          ret.push_back(make_pair(make_pair(idx, i), key));
        } else
          --key;
      }
  }
  for (int i = 0; i < (n); ++i)
    assert(((int)a[i].size()) == c1 || ((int)a[i].size()) == c2);
  printf("%d\n", ((int)ret.size()));
  for (auto w : ret)
    printf("%d %d %d\n", w.first.first + 1, w.first.second + 1, w.second + 1);
  return 0;
}
