#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
clock_t START;
inline long double TIME() {
  return (long double)(clock() - START) / CLOCKS_PER_SEC;
}
void SHOW() {
  cout << fixed << setprecision(10);
  cout << TIME() << " SECONDS FROM START\n";
}
long double TL = 2.0;
long long calls = 0;
inline bool IS() {
  if (TL - TIME() < 0.1) return true;
  return false;
}
template <typename T1, typename T2>
inline bool amin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
inline bool amax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << p.first << ' ' << p.second;
  return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &u : v) is >> u;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto &u : v) os << u << ' ';
  return os;
}
long long hash_mod[4] = {1000000007, 998244353, 1000000009, 999999937},
          mod = hash_mod[rnd() % 4];
long long hash_pows[4] = {179, 239, 1007, 2003}, P = hash_pows[rnd() % 4];
long long binpow(long long first, long long p) {
  long long res = 1;
  while (p) {
    if (p % 2) res = (res * first) % mod;
    first = (first * first) % mod;
    p /= 2;
  }
  return res;
}
const long long N = 1e6 + 7, NS = 2e3 + 7, lg = 20, sq = 550, inf = 2e9 + 7;
const long double eps = 1e-9, pi = 3.14159265359;
vector<long long> v[N];
vector<long long> s[N];
long long c[N];
unordered_set<long long> S;
void dfs(long long first, long long p) {
  for (auto u : s[first]) {
    if (c[u] != -1) {
      S.erase(c[u]);
    }
  }
  for (auto u : s[first]) {
    if (c[u] == -1) {
      if (S.empty()) assert(0);
      c[u] = *S.begin();
      S.erase(S.begin());
    }
  }
  for (auto u : s[first]) {
    S.insert(c[u]);
  }
  for (auto u : v[first]) {
    if (u == p) continue;
    dfs(u, first);
  }
}
void solve() {
  long long n, m, ans = 1;
  cin >> n >> m;
  fill(c, c + m, -1);
  for (long long i = 0; i < n; i++) {
    long long C;
    cin >> C;
    amax(ans, C);
    while (C--) {
      long long first;
      cin >> first;
      first--;
      s[i].push_back(first);
    }
  }
  for (long long i = 0; i < n - 1; i++) {
    long long a, b;
    cin >> a >> b;
    a--, b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  for (long long i = 1; i <= ans; i++) S.insert(i);
  dfs(0, -1);
  cout << ans << endl;
  for (long long i = 0; i < m; i++) {
    if (c[i] == -1) c[i] = rnd() % ans + 1;
    cout << c[i] << ' ';
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}
