#include <bits/stdc++.h>
using namespace std;
unordered_set<int> S[300001];
unordered_set<int> COL[300001];
vector<int> G[300001];
int C[300001];
void dfs(int v, int p) {
  for (int u : G[v]) {
    if (u == p) continue;
    for (int c : S[u])
      if (S[v].count(c)) COL[u].insert(C[c]);
    int i = 1;
    for (int c : S[u]) {
      if (S[v].count(c)) continue;
      while (COL[u].count(i)) i++;
      C[c] = i;
      COL[u].insert(i);
      i++;
    }
    dfs(u, v);
  }
}
void solve() {
  int n, m, s, x, u, v;
  int ans = 1;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> s;
    ans = max(ans, s);
    while (s--) {
      cin >> x;
      S[i].insert(x);
    }
  }
  for (int i = 1; i < n; ++i) {
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  {
    int i = 1;
    for (int c : S[1]) {
      C[c] = i;
      COL[1].insert(i++);
    }
  }
  dfs(1, 0);
  cout << ans << '\n';
  for (int i = 1; i <= m; ++i) cout << max(1, C[i]) << ' ';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
