#include <bits/stdc++.h>
using namespace std;
const int MX = (1 << 17);
set<int> S[MX];
int n;
int bitgiver[MX];
set<int> av;
int cnt[MX];
set<int> who[MX];
int bittaker[MX];
void addtogiver(int x, int V) {
  while (x < MX) {
    bitgiver[x] += V;
    x += x & (-x);
  }
}
int tar[MX];
void init() {
  for (int j = 1; j <= n; j++) {
    if (S[j].size() <= tar[j]) continue;
    for (auto pp : S[j]) {
      if (!av.count(pp)) {
        av.insert(pp);
        addtogiver(pp, 1);
      }
      who[pp].insert(j);
      ++cnt[pp];
    }
  }
}
void addtotaker(int x, int V) {
  while (x < MX) {
    bittaker[x] += V;
    x += x & (-x);
  }
}
int find_mismatch() {
  int ret = 0, a = 0, b = 0;
  for (int j = 16; j >= 0; j--) {
    if (a + bitgiver[ret + (1 << j)] == b + bittaker[ret + (1 << j)])
      ret += (1 << j);
  }
  return ++ret;
}
void inittaker(int idx) {
  for (auto pp : S[idx]) {
    if (av.count(pp)) addtotaker(pp, 1);
  }
}
void clrtaker(int idx) {
  for (auto pp : S[idx]) {
    if (av.count(pp)) addtotaker(pp, -1);
  }
}
vector<pair<pair<int, int>, int> > sol;
void transfer(int which, int to) {
  S[to].insert(which);
  int from = *who[which].begin();
  sol.push_back({{from, to}, which});
  who[which].erase(from);
  addtotaker(which, 1);
  --cnt[which];
  if (cnt[which] == 0) {
    av.erase(which);
    addtogiver(which, -1);
    addtotaker(which, -1);
  }
  S[from].erase(which);
  if (S[from].size() == tar[from]) {
    for (auto pp : S[from]) {
      --cnt[pp];
      who[pp].erase(from);
      if (cnt[pp] == 0) {
        addtogiver(pp, -1);
        av.erase(pp);
        if (S[to].count(pp)) addtotaker(pp, -1);
      }
    }
  }
}
vector<pair<int, int> > sorted;
int main() {
  int K;
  scanf("%d %d", &n, &K);
  int sum = 0;
  for (int j = 1; j <= n; j++) {
    int K;
    scanf("%d", &K);
    sum += K;
    while (K--) {
      int x;
      scanf("%d", &x);
      S[j].insert(x);
    }
  }
  for (int j = 1; j <= n; j++) {
    sorted.push_back({S[j].size(), j});
  }
  sort(sorted.rbegin(), sorted.rend());
  for (int j = 1; j <= n; j++) {
    int who = sorted[j - 1].second;
    tar[who] = sum / n;
    if (j <= sum % n) ++tar[who];
  }
  init();
  for (int j = 1; j <= n; j++) {
    if (S[j].size() >= tar[j]) continue;
    inittaker(j);
    int taker = j;
    while (S[taker].size() < tar[taker]) {
      int which = find_mismatch();
      transfer(which, taker);
    }
    clrtaker(taker);
  }
  cout << sol.size() << endl;
  for (auto pp : sol)
    cout << pp.first.first << ' ' << pp.first.second << ' ' << pp.second
         << endl;
}
