#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n, m, vis[maxn];
struct out {
  int f, t, k;
  out() {}
  out(int f, int t, int k) : f(f), t(t), k(k) {}
};
set<pair<int, int> > S;
set<pair<int, int> >::iterator it;
set<int> G[maxn];
int nu[maxn], a[maxn], sum;
vector<out> ans;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (int)1; i <= (int)n; i++) {
    scanf("%d", &nu[i]);
    for (int j = (int)1; j <= (int)nu[i]; j++) {
      int x;
      scanf("%d", &x);
      G[i].insert(x);
    }
    S.insert({nu[i], i});
    sum += nu[i];
  }
  int cha, ma, mi;
  if (sum % n == 0) {
    ma = mi = sum / n;
    cha = 0;
  } else {
    mi = sum / n, ma = mi + 1;
    cha = 1;
  }
  if (n == 1) {
    return 0 * printf("0\n");
  }
  pair<int, int> bi, sm;
  it = S.begin();
  sm = *it;
  S.erase(it);
  it = S.end();
  it--;
  bi = *it;
  S.erase(it);
  int br = 0;
  while (1 && !br) {
    vector<int> tmp, tmp1;
    if (bi.first - sm.first <= cha) break;
    int szb = G[bi.second].size(), szs = G[sm.second].size();
    int tb = szb, ts = szs, f1 = 0, f2 = 0;
    for (set<int>::iterator is = G[sm.second].begin(); is != G[sm.second].end();
         is++) {
      int th = *is;
      a[th]--;
    }
    for (set<int>::iterator is = G[bi.second].begin(); is != G[bi.second].end();
         is++) {
      int th = *is;
      a[th]++;
      tmp1.push_back(th);
      if (a[th] == 1) {
        ans.push_back({bi.second, sm.second, th});
        tb--, ts++;
        tmp.push_back(th);
        if (tb <= mi + cha) f1 = 1;
        if (ts >= ma - cha) f2 = 1;
        if (f1 && f2 || tb == mi || ts == ma) break;
      }
    }
    for (int i = 0; i < tmp.size(); i++) {
      int th = tmp[i];
      G[sm.second].insert(th);
      G[bi.second].erase(th);
    }
    if (f1 && f2) {
      for (int i = 0; i < tmp1.size(); i++) {
        a[tmp1[i]]--;
      }
      for (set<int>::iterator is = G[sm.second].begin();
           is != G[sm.second].end(); is++) {
        int th = *is;
        a[th] = 0;
      }
      if (S.size()) {
        it = S.begin();
        sm = *it;
        S.erase(it);
      } else
        br = 1;
      if (S.size()) {
        it = S.end();
        it--;
        bi = *it;
        S.erase(it);
      } else
        br = 1;
    } else if (tb == mi) {
      for (int i = 0; i < tmp1.size(); i++) {
        a[tmp1[i]]--;
      }
      if (S.size()) {
        it = S.end();
        it--;
        bi = *it;
        S.erase(it);
      } else
        br = 1;
    } else {
      for (int i = 0; i < tmp1.size(); i++) {
        a[tmp1[i]]--;
      }
      for (set<int>::iterator is = G[sm.second].begin();
           is != G[sm.second].end(); is++) {
        int th = *is;
        a[th] = 0;
      }
      if (S.size()) {
        it = S.begin();
        sm = *it;
        S.erase(it);
      } else
        br = 1;
    }
  }
  printf("%d\n", ans.size());
  for (int i = 0; i < ans.size(); i++) {
    out x = ans[i];
    printf("%d %d %d\n", x.f, x.t, x.k);
  }
  return 0;
}
