#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
using namespace std;
long long n, m;
set<long long> v[((long long)(1e6 + 600))];
int main() {
  ios::sync_with_stdio(0);
  long long t, k;
  cin >> n >> m;
  long long s = 0;
  for (int i = 0; i < n; i++) {
    cin >> t;
    while (t--) {
      cin >> k;
      v[i].insert(k);
    }
    s += v[i].size();
  }
  long long l = s / n, r = s / n + ((s % n) > 0);
  stack<long long> g;
  for (int i = 0; i < n; i++)
    if (v[i].size() < l) g.push(i);
  vector<pair<pair<long long, long long>, long long> > pas;
  for (int i = 0; i < n; i++) {
    while (v[i].size() > r && g.size()) {
      auto w = g.top();
      for (auto it = v[i].begin(); it != v[i].end();) {
        if (v[w].find(*it) != v[w].end()) {
          it++;
          continue;
        }
        pas.push_back({{i, w}, *it});
        v[w].insert((*it));
        auto k = it;
        k++;
        v[i].erase(it);
        it = k;
        if (v[i].size() <= r) break;
        if (v[w].size() >= l) break;
      }
      if (v[w].size() >= l) g.pop();
    }
  }
  if (g.size()) {
    for (int i = 0; i < n; i++) {
      while (v[i].size() > l && g.size()) {
        auto w = g.top();
        for (auto it = v[i].begin(); it != v[i].end();) {
          if (v[w].find(*it) != v[w].end()) {
            it++;
            continue;
          }
          pas.push_back({{i, w}, *it});
          v[w].insert((*it));
          auto k = it;
          k++;
          v[i].erase(it);
          it = k;
          if (v[i].size() == l) break;
          if (v[w].size() >= l) break;
        }
        if (v[w].size() >= l) g.pop();
      }
    }
  } else {
    for (int i = 0; i < n; i++)
      if (v[i].size() < r) g.push(i);
    for (int i = 0; i < n; i++) {
      while (v[i].size() > r && g.size()) {
        auto w = g.top();
        for (auto it = v[i].begin(); it != v[i].end();) {
          if (v[w].find(*it) != v[w].end()) {
            it++;
            continue;
          }
          pas.push_back({{i, w}, *it});
          v[w].insert((*it));
          auto k = it;
          k++;
          v[i].erase(it);
          it = k;
          if (v[i].size() <= r) break;
          if (v[w].size() == r) break;
        }
        if (v[w].size() == r) g.pop();
      }
    }
  }
  cout << pas.size() << endl;
  for (auto x : pas) {
    cout << x.first.first + 1 << " " << x.first.second + 1 << " " << x.second
         << endl;
  }
}
