#include <bits/stdc++.h>
int colors[300001] = {};
int used[300001] = {};
int visited[300001] = {};
std::vector<int> vertices[300001];
std::vector<int> edges[300001];
void dfs(int vert, int last) {
  if (visited[vert]) return;
  visited[vert] = 1;
  for (int i = 0; i < vertices[vert].size(); i++) {
    if (colors[vertices[vert][i]]) {
      used[colors[vertices[vert][i]]] = 1;
    }
  }
  int curr = 1;
  for (int i = 0; i < vertices[vert].size(); i++) {
    if (colors[vertices[vert][i]]) continue;
    while (used[curr]) curr++;
    colors[vertices[vert][i]] = curr;
    curr++;
  }
  for (int i = 0; i < vertices[vert].size(); i++) {
    used[colors[vertices[vert][i]]] = 0;
  }
  for (int i = 0; i < edges[vert].size(); i++) {
    if (edges[vert][i] == last) continue;
    dfs(edges[vert][i], vert);
  }
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int s;
    scanf("%d", &s);
    std::vector<int> indices;
    for (int j = 1; j <= s; j++) {
      int index;
      scanf("%d", &index);
      vertices[i].push_back(index);
    }
  }
  for (int i = 1; i < n - 1; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    edges[u].push_back(v);
    edges[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    dfs(i, -1);
  }
  int highest = -1;
  for (int i = 1; i <= m; i++) {
    if (!colors[i]) colors[i] = 1;
    highest = std::max(highest, colors[i]);
  }
  printf("%d\n", highest);
  for (int i = 1; i <= m; i++) {
    printf("%d ", colors[i]);
  }
  printf("\n");
  return 0;
}
