#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c *x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  ~debug() { cerr << endl; }
  template <class c>
  typename enable_if<sizeof dud<c>(0) != 1, debug &>::type operator<<(c i) {
    cerr << boolalpha << i;
    return *this;
  }
  template <class c>
  typename enable_if<sizeof dud<c>(0) == 1, debug &>::type operator<<(c i) {
    return *this << range(begin(i), end(i));
  }
  template <class c, class b>
  debug &operator<<(pair<b, c> d) {
    return *this << "(" << d.first << ", " << d.second << ")";
  }
  template <class c>
  debug &operator<<(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it;
    return *this << "]";
  }
};
const long double eps = 1e-9;
const long long inf = 1e18;
auto rnd = bind(uniform_int_distribution<int>(0, 63), mt19937(time(0)));
auto rndd = bind(uniform_int_distribution<int>(55, 55), mt19937(time(0)));
const int N = 1e5 + 1;
set<long long> have_lower[N];
set<long long> have_greater[N];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  ;
  long long n, m;
  cin >> n >> m;
  long long l, r;
  vector<long long> second(n);
  vector<vector<long long>> a(n, vector<long long>());
  long long sm = 0;
  for (long long i = 0; i < n; i++) {
    cin >> second[i];
    a[i].resize(second[i]);
    for (long long j = 0; j < second[i]; j++) {
      cin >> a[i][j];
      --a[i][j];
    }
    sm += second[i];
  }
  l = sm / n, r = ceil((long double)sm / n);
  long long cll = n - (sm % n);
  long long clr = sm % n;
  set<long long> blya_upd;
  for (long long i = 0; i < n; i++) {
    if (second[i] == l) {
      if (cll != 0) {
        cll--;
        continue;
      }
    }
    if (second[i] == r) {
      if (clr != 0) {
        clr--;
        continue;
      }
    }
    if (second[i] >= r) {
      for (auto &z : a[i]) have_greater[z].insert(i);
    } else {
      for (auto &z : a[i]) {
        have_lower[z].insert(i);
      }
      blya_upd.insert(i);
    }
  }
  vector<pair<long long, pair<long long, long long>>> ans;
  set<long long> pob;
  for (long long i = 0; i < m; i++) {
    pob.clear();
    for (auto &z : have_lower[i]) {
      blya_upd.erase(z);
    }
    while ((long long)blya_upd.size() && (long long)have_greater[i].size()) {
      long long from = *have_greater[i].rbegin();
      long long to = *blya_upd.begin();
      second[from]--;
      second[to]++;
      if (second[to] == l && cll) {
        for (auto &z : a[to]) {
          have_lower[z].erase(to);
        }
        cll--;
      } else if (second[to] == r) {
        assert(clr);
        clr--;
        for (auto &z : a[to]) {
          have_lower[z].erase(to);
        }
      } else {
        pob.insert(to);
      }
      if (second[from] == r && clr) {
        for (auto &z : a[from]) {
          have_greater[z].erase(from);
        }
        clr--;
      } else if (second[from] == l) {
        assert(cll);
        cll--;
        for (auto &z : a[from]) {
          have_greater[z].erase(from);
        }
      }
      ans.push_back({i, {from, to}});
      have_greater[i].erase(from);
      blya_upd.erase(to);
    }
    for (auto &z : pob) blya_upd.insert(z);
    for (auto &z : have_lower[i]) {
      blya_upd.insert(z);
    }
  }
  cout << (long long)ans.size() << '\n';
  for (auto &z : ans) {
    cout << z.second.first + 1 << ' ' << z.second.second + 1 << ' '
         << z.first + 1 << '\n';
  }
  return 0;
}
