#include <bits/stdc++.h>
using namespace std;
set<int> se[100500];
vector<int> a[100500], nxt[100500], lst[100500];
vector<bool> tp[100500];
int mns[100500], ps[100500];
bool cmp(int x, int y) { return int(se[x].size()) > int(se[y].size()); }
int main() {
  ios_base::sync_with_stdio(0);
  iostream::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  int sum = 0;
  int f[n];
  vector<int> vr(n);
  for (int i = 0; i < n; i++) {
    vr[i] = i;
    int len;
    cin >> len;
    sum += len;
    nxt[i].resize(len);
    lst[i].resize(len);
    tp[i].resize(len, 0);
    for (int j = 0; j < len; j++) {
      nxt[i][j] = j + 1;
      lst[i][j] = j - 1;
      int x;
      cin >> x;
      se[i].insert(x);
      a[i].push_back(x);
    }
  }
  for (int i = 0; i < n; i++) {
    f[i] = sum / n;
    if (sum % n != 0) {
      f[i]++;
      sum--;
    }
  }
  sort(vr.begin(), vr.end(), cmp);
  int l = 0, r = n - 1;
  vector<array<int, 3> > ans;
  ans.clear();
  while (l < r) {
    int nm = vr[l], id = vr[r];
    if (f[l] == int(se[nm].size()) - mns[nm]) {
      l++;
      continue;
    }
    if (f[r] == int(se[id].size())) {
      r--;
      continue;
    }
    int i = ps[nm];
    while (i < int(a[nm].size()) && f[l] != int(se[nm].size()) - mns[nm] &&
           f[r] != int(se[id].size())) {
      int val = a[nm][i];
      if (!tp[nm][i] && se[id].find(val) == se[id].end()) {
        ans.push_back({nm + 1, id + 1, val});
        se[id].insert(val);
        mns[nm]++;
        int j = lst[nm][i];
        int nx = nxt[nm][i];
        if (nx != n) lst[nm][nx] = j;
        if (j != -1) nxt[nm][j] = nx;
        tp[nm][i] = 1;
        while (ps[nm] < int(a[nm].size()) && tp[nm][ps[nm]]) ps[nm]++;
      }
      i = nxt[nm][i];
    }
  }
  cout << int(ans.size()) << '\n';
  ;
  for (auto it : ans) cout << it[0] << " " << it[1] << " " << it[2] << '\n';
  ;
}
