#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
inline long long getint() {
  long long _x = 0, _tmp = 1;
  char _tc = getchar();
  while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
  if (_tc == '-') _tc = getchar(), _tmp = -1;
  while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
  return _x * _tmp;
}
inline long long add(long long _x, long long _y,
                     long long _mod = 1000000007LL) {
  _x += _y;
  return _x >= _mod ? _x - _mod : _x;
}
inline long long sub(long long _x, long long _y,
                     long long _mod = 1000000007LL) {
  _x -= _y;
  return _x < 0 ? _x + _mod : _x;
}
inline long long mul(long long _x, long long _y,
                     long long _mod = 1000000007LL) {
  _x *= _y;
  return _x >= _mod ? _x % _mod : _x;
}
long long mypow(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 1LL;
  long long _ret = mypow(mul(_a, _a, _mod), _x >> 1, _mod);
  if (_x & 1) _ret = mul(_ret, _a, _mod);
  return _ret;
}
long long mymul(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 0LL;
  long long _ret = mymul(add(_a, _a, _mod), _x >> 1, _mod);
  if (_x & 1) _ret = add(_ret, _a, _mod);
  return _ret;
}
inline bool equal(double _x, double _y) {
  return _x > _y - 1e-9 && _x < _y + 1e-9;
}
int __ = 1, _cs;
void build() {}
int n, mxs, m;
vector<int> v[303030], s[303030];
void init() {
  n = getint();
  m = getint();
  for (int i = 1; i <= n; i++) {
    int si = getint();
    mxs = max(mxs, si);
    while (si--) s[i].push_back(getint());
  }
  for (int i = 1; i < n; i++) {
    int ai = getint();
    int bi = getint();
    v[ai].push_back(bi);
    v[bi].push_back(ai);
  }
}
int clr[303030];
void go(int now, int prt) {
  set<int> gc;
  for (int ii : s[now])
    if (clr[ii]) gc.insert(clr[ii]);
  int nclr = 1;
  for (int ii : s[now]) {
    if (clr[ii]) continue;
    while (gc.count(nclr)) nclr++;
    clr[ii] = nclr;
    gc.insert(nclr);
  }
  for (int son : v[now]) {
    if (son == prt) continue;
    go(son, now);
  }
}
int id[303030];
bool cmp(int i, int j) { return s[i].size() > s[j].size(); }
void solve() {
  go(1, 1);
  int ans = *max_element(clr + 1, clr + m + 1);
  assert(ans == 0 or ans == mxs);
  printf("%d\n", max(1, ans));
  for (int i = 1; i <= m; i++) printf("%d%c", max(1, clr[i]), " \n"[i == m]);
}
int main() {
  build();
  while (__--) {
    init();
    solve();
  }
}
