#include <bits/stdc++.h>
using namespace std;
mt19937 ran(time(0));
int mod = 1e9 + 7;
int gcd(int a, int b) {
  if (!b) return a;
  return gcd(b, a % b);
}
long long Bpow(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) {
      ans *= a;
      ans %= mod;
    }
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> k(n);
  set<pair<int, int>> st;
  vector<vector<int>> nums(m + 1);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> k[i];
    sum += k[i];
    st.insert({k[i], i});
    for (int j = 0; j < k[i]; j++) {
      int a;
      cin >> a;
      nums[a].push_back(i);
    }
  }
  int mi = sum / n;
  int cnt = sum - mi * n;
  int now = 0;
  for (int i = 0; i < n; i++) {
    if (k[i] > mi) now++;
  }
  vector<vector<int>> ans;
  for (int i = 1; i < m + 1; i++) {
    for (auto& nm : nums[i]) {
      st.erase({k[nm], nm});
    }
    vector<pair<int, int>> in;
    for (auto& nm : nums[i]) {
      if (!st.empty() &&
          (now > cnt && k[nm] >= mi + 1 && st.begin()->first < mi ||
           now < cnt && k[nm] > mi + 1 && st.begin()->first <= mi ||
           now == cnt && k[nm] > mi + 1 && st.begin()->first < mi)) {
        k[nm]--;
        if (mi == k[nm]) --now;
        pair<int, int> v = *st.begin();
        v.first++;
        if (v.first == mi + 1) now++;
        st.erase({v.first - 1, v.second});
        in.push_back(v);
        k[v.second]++;
        ans.push_back({nm + 1, v.second + 1, i});
      }
    }
    for (auto& t : in) st.insert(t);
    for (auto& nm : nums[i]) {
      st.insert({k[nm], nm});
    }
  }
  cout << ans.size() << "\n";
  for (auto& v : ans) {
    for (auto& viv : v) cout << viv << " ";
    cout << "\n";
  }
}
