#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 200;
int n, m;
vector<int> ice[MAXN];
vector<int> g[MAXN];
int col[MAXN];
bool used[MAXN];
void dfs(int u, int p) {
  int rem = 1;
  for (int v : ice[u]) used[col[v]] = true;
  for (int v : ice[u]) {
    if (col[v]) continue;
    while (used[rem]) ++rem;
    col[v] = rem++;
  }
  for (int v : ice[u]) used[col[v]] = false;
  for (int v : g[u])
    if (v != p) dfs(v, u);
}
int main() {
  scanf("%d", &n), scanf("%d", &m);
  int cap;
  int u, v;
  int capmax = 1;
  for (int i = 0; i < (int)n; ++i) {
    scanf("%d", &cap);
    capmax = max(capmax, cap);
    for (int j = 0; j < (int)cap; ++j) {
      scanf("%d", &u);
      ice[i].push_back(u);
    }
  }
  for (int i = 0; i < (int)n - 1; ++i) {
    scanf("%d", &u), scanf("%d", &v);
    g[--u].push_back(--v), g[v].push_back(u);
  }
  dfs(0, -1);
  printf("%d\n", capmax);
  for (int i = 1; i <= (int)m; ++i) {
    printf("%d ", max(1, col[i]));
  }
  printf("\n");
}
