#include <bits/stdc++.h>
using namespace std;
struct node {
  int l, r, x;
  node() {}
  node(int l, int r, int x) : l(l), r(r), x(x) {}
};
vector<int> vec[100010];
int n, m;
int vis[100010];
vector<node> res;
int a[100010 * 100], now[100010], nx[100010 * 100], last;
int sze[100010];
queue<int> q;
void add(int x, int id) {
  a[++last] = x;
  nx[last] = now[id];
  now[id] = last;
}
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    int tot = 0;
    res.clear();
    memset(now, 0, sizeof now);
    for (int i = 1, x, y; i <= n; ++i) {
      scanf("%d", &x);
      tot += x;
      vec[i].clear();
      for (int j = 1; j <= x; ++j) {
        scanf("%d", &y);
        vec[i].push_back(y);
      }
      sze[i] = vec[i].size();
    }
    if (tot % n == 0) {
      int x = tot / n;
      for (int i = 1; i <= n; ++i)
        if (sze[i] > x)
          for (auto it : vec[i]) add(i, it);
      for (int i = 1; i <= m; ++i)
        if (now[i]) q.push(i);
      for (int i = 1, front; i <= n; ++i)
        if (sze[i] < x) {
          for (auto it : vec[i]) vis[it] = 1;
          while (sze[i] < x) {
            front = q.front();
            q.pop();
            if (vis[front]) {
              q.push(front);
              continue;
            }
            int id = now[front];
            for (; id; id = nx[id]) {
              if (sze[a[id]] <= x) continue;
              res.push_back(node(a[id], i, front));
              vec[i].push_back(front);
              vis[front] = 1;
              --sze[a[id]];
              ++sze[i];
              id = nx[id];
              break;
            }
            if (id) q.push(front);
            now[front] = id;
          }
          for (auto it : vec[i]) vis[it] = 0;
        }
    } else {
      int x = tot / n + 1;
      int need = n - tot % n;
      for (int i = 1; i <= n; ++i)
        if (sze[i] > x)
          for (auto it : vec[i]) add(i, it);
      for (int i = 1; i <= m; ++i)
        if (now[i]) q.push(i);
      for (int i = 1, front; i <= n; ++i)
        if (sze[i] < x) {
          if (sze[i] == x - 1 && need) {
            --need;
            continue;
          }
          for (auto it : vec[i]) vis[it] = 1;
          while (sze[i] < x) {
            if (sze[i] == x - 1 && need) {
              --need;
              break;
            }
            front = q.front();
            q.pop();
            if (vis[front]) {
              q.push(front);
              continue;
            }
            int id = now[front];
            for (; id; id = nx[id]) {
              if (sze[a[id]] <= x) continue;
              res.push_back(node(a[id], i, front));
              vec[i].push_back(front);
              vis[front] = 1;
              --sze[a[id]];
              ++sze[i];
              id = nx[id];
              break;
            }
            if (id) q.push(front);
            now[front] = id;
          }
          for (auto it : vec[i]) vis[it] = 0;
        }
    }
    int len = res.size();
    printf("%d\n", len);
    for (int i = 0; i < len; ++i)
      printf("%d %d %d\n", res[i].l, res[i].r, res[i].x);
  }
  return 0;
}
