#include <bits/stdc++.h>
const int maxn = 5e5 + 20;
int n, m;
int ans = 1;
int col[maxn];
bool flag[maxn];
std::vector<int> S[maxn];
std::vector<int> G[maxn];
void dfs(int x, int fa) {
  for (int i = 0; i < S[x].size(); i++) flag[i + 1] = true;
  for (int i = 0; i < S[x].size(); i++) flag[col[S[x][i]]] = false;
  int pos = 1;
  for (int i = 0; i < S[x].size(); i++) {
    int k = S[x][i];
    if (!col[k]) {
      while (!flag[pos]) pos++;
      col[k] = pos;
      flag[pos] = false;
    }
  }
  for (int i = 0; i < G[x].size(); i++) {
    if (G[x][i] != fa) dfs(G[x][i], x);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, sz, sx; i <= n; i++) {
    scanf("%d", &sz);
    ans = std::max(ans, sz);
    while (sz--) {
      scanf("%d", &sx);
      S[i].push_back(sx);
    }
  }
  for (int i = 1, x, y; i < n; i++) {
    scanf("%d%d", &x, &y);
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs(1, 0);
  printf("%d\n", ans);
  for (int i = 1; i <= m; i++) {
    if (!col[i]) col[i] = 1;
    printf("%d ", col[i]);
  }
  return 0;
}
