#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int FFTMOD = 119 << 23 | 1;
const int INF = (int)1e9 + 23111992;
const long long LINF = (long long)1e18 + 23111992;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
  return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
  return a < val ? a = val, 1 : 0;
}
inline unsigned long long isqrt(unsigned long long k) {
  unsigned long long r = sqrt(k) + 1;
  while (r * r > k) r--;
  return r;
}
inline long long icbrt(long long k) {
  long long r = cbrt(k) + 1;
  while (r * r * r > k) r--;
  return r;
}
inline void addmod(int& a, int val, int p = MOD) {
  if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
  if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int myrand() { return abs((int)mt()); }
struct node_t {
  node_t *l, *r;
  int cnt;
  node_t(node_t* l = 0, node_t* r = 0, int cnt = 0) : l(l), r(r), cnt(cnt) {}
};
int cnt(node_t* p) { return p ? p->cnt : 0; }
node_t* upd(node_t* p, int i, int L, int R, int val) {
  if (i < L || R < i) return p;
  if (L == R) {
    return new node_t(0, 0, cnt(p) + val);
  }
  node_t* res = new node_t(p ? p->l : 0, p ? p->r : 0);
  res->l = upd(res->l, i, L, L + R >> 1, val);
  res->r = upd(res->r, i, (L + R >> 1) + 1, R, val);
  res->cnt = cnt(res->l) + cnt(res->r);
  return res;
}
int query(node_t* p, node_t* q, int L, int R) {
  if (L == R) {
    return L;
  }
  if (cnt(p ? p->l : 0) > cnt(q ? q->l : 0)) {
    return query(p->l, q ? q->l : 0, L, L + R >> 1);
  } else {
    return query(p->r, q ? q->r : 0, (L + R >> 1) + 1, R);
  }
}
const int maxn = 1e5 + 5;
int n, m;
node_t* f[maxn];
void chemthan() {
  cin >> n >> m;
  vector<vector<int> > a(n);
  for (int i = (0); i < (n); ++i) {
    int k;
    cin >> k;
    a[i].resize(k);
    for (int& x : a[i]) {
      cin >> x, x--;
      f[i] = upd(f[i], x, 0, m - 1, 1);
    }
  }
  set<pair<int, int> > heap;
  for (int i = (0); i < (n); ++i) {
    heap.insert(make_pair(cnt(f[i]), i));
  }
  vector<pair<pair<int, int>, int> > res;
  while (1) {
    int u = heap.begin()->second;
    int v = heap.rbegin()->second;
    if (cnt(f[u]) < cnt(f[v]) - 1) {
      heap.erase(make_pair(cnt(f[u]), u));
      heap.erase(make_pair(cnt(f[v]), v));
      int x = query(f[v], f[u], 0, m - 1);
      f[v] = upd(f[v], x, 0, m - 1, -1);
      f[u] = upd(f[u], x, 0, m - 1, +1);
      heap.insert(make_pair(cnt(f[u]), u));
      heap.insert(make_pair(cnt(f[v]), v));
      res.push_back(make_pair(make_pair(v, u), x));
    } else {
      break;
    }
  }
  cout << int((res).size()) << "\n";
  for (auto e : res) {
    cout << e.first.first + 1 << " " << e.first.second + 1 << " "
         << e.second + 1 << "\n";
  }
}
int main(int argc, char* argv[]) {
  ios_base::sync_with_stdio(0), cin.tie(0);
  if (argc > 1) {
    assert(freopen(argv[1], "r", stdin));
  }
  if (argc > 2) {
    assert(freopen(argv[2], "wb", stdout));
  }
  chemthan();
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}
