#include <bits/stdc++.h>
const int MAX_N = 300000;
const int MAX_M = 300000;
std::vector<int> types[1 + MAX_N];
std::vector<int> neighbours[1 + MAX_N];
int c;
int colours[1 + MAX_M];
bool used[1 + MAX_M];
void dfs(int u = 1, int father = 0) {
  for (int type : types[u])
    if (colours[type] != 0) used[colours[type]] = true;
  int i = 0, j = 1;
  while (i < (int)types[u].size()) {
    if (used[j]) {
      j++;
    } else {
      if (colours[types[u][i]] == 0) {
        colours[types[u][i]] = j;
        j++;
      }
      i++;
    }
  }
  for (int type : types[u]) used[colours[type]] = false;
  for (int v : neighbours[u])
    if (v != father) {
      dfs(v, u);
    }
}
int main(void) {
  int n, m;
  scanf("%d%d", &n, &m);
  int c = 1;
  for (int i = 1; i <= n; i++) {
    int s;
    scanf("%d", &s);
    c = std::max(c, s);
    for (int j = 0; j < s; j++) {
      int type;
      scanf("%d", &type);
      types[i].push_back(type);
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    neighbours[u].push_back(v);
    neighbours[v].push_back(u);
  }
  dfs();
  for (int i = 1; i <= m; i++)
    if (colours[i] == 0) colours[i] = 1;
  printf("%d\n", c);
  for (int i = 1; i <= m; i++) printf("%d ", colours[i]);
  printf("\n");
  return 0;
}
