#include <bits/stdc++.h>
using namespace std;
set<int> in[100005];
pair<int, int> id[100005];
int bit[100005];
int lastNum[100005];
int color[100005];
void solve() {
  int n, m;
  scanf("%d %d ", &n, &m);
  long long sum = 0;
  for (int i = 1; i <= n; ++i) {
    int num;
    scanf("%d ", &num);
    sum += num;
    while (num--) {
      int x;
      scanf("%d ", &x);
      in[i].insert(x);
    }
    id[i] = {in[i].size(), i};
  }
  int x = sum / n;
  int lim = (n - sum % n);
  int t = 0;
  for (int i = 1; i <= n; ++i) {
    if (i <= lim) {
      lastNum[i] = x;
    } else {
      lastNum[i] = x + 1;
    }
    t += lastNum[i];
  }
  assert(t == sum);
  sort(id + 1, id + n + 1);
  int l = 1, r = n;
  vector<pair<pair<int, int>, int> > out;
  bool lin = false, rin = false;
  set<int> swapNums;
  while (l < r) {
    int lid = id[l].second;
    int rid = id[r].second;
    if (in[lid].size() == lastNum[l]) {
      if (lin) {
        for (int x : in[lid]) {
          assert(color[x] & 1);
          color[x] ^= 1;
          if (color[x] == 2) {
            assert(!swapNums.count(x));
            swapNums.insert(x);
          }
        }
      }
      ++l;
      lin = false;
      continue;
    }
    if (in[rid].size() == lastNum[r]) {
      if (rin) {
        for (int x : in[rid]) {
          assert(color[x] & 2);
          if (color[x] == 2) {
            assert(swapNums.count(x));
            swapNums.erase(x);
          }
          color[x] ^= 2;
        }
      }
      --r;
      rin = false;
      continue;
    }
    if (!lin) {
      for (int x : in[lid]) {
        color[x] |= 1;
        if (color[x] == 3) {
          assert(swapNums.count(x));
          swapNums.erase(x);
        }
      }
      lin = true;
    }
    if (!rin) {
      for (int x : in[rid]) {
        color[x] |= 2;
        if (color[x] == 2) {
          assert(!swapNums.count(x));
          swapNums.insert(x);
        }
      }
      rin = true;
    }
    assert(in[lid].size() < lastNum[l]);
    assert(in[rid].size() > lastNum[r]);
    while (in[lid].size() != lastNum[l] && in[rid].size() != lastNum[r]) {
      assert(swapNums.size() > 0);
      int g = *swapNums.begin();
      swapNums.erase(g);
      in[lid].insert(g);
      in[rid].erase(g);
      out.push_back({{rid, lid}, g});
      color[g] = 1;
    }
  }
  printf("%d\n", out.size());
  for (pair<pair<int, int>, int> tmp : out) {
    printf("%d %d %d\n", tmp.first.first, tmp.first.second, tmp.second);
  }
}
int main() {
  solve();
  return 0;
}
