#include <bits/stdc++.h>
int a[600010], last[600010], first[600010], next[600010], k;
int a2[600010], last2[600010], first2[600010], next2[600010], k2;
int x, y, n, ans, res[300010], m, bo[300010];
void add(int x, int y) {
  k++;
  a[k] = y;
  if (first[x] == 0)
    first[x] = k;
  else
    next[last[x]] = k;
  last[x] = k;
}
void add2(int x, int y) {
  k2++;
  a2[k2] = y;
  if (first2[x] == 0)
    first2[x] = k2;
  else
    next2[last2[x]] = k2;
  last2[x] = k2;
}
void dfs(int x, int fa) {
  int l = 1;
  for (int i = first[x]; i; i = next[i])
    if (res[a[i]] != 0) {
      bo[res[a[i]]] = x;
      while (bo[l] == x && l <= ans) l++;
    }
  for (int i = first[x]; i; i = next[i])
    if (res[a[i]] == 0) {
      res[a[i]] = l;
      bo[l] = x;
      while (bo[l] == x) l++;
    }
  if ((l - 1) > ans) ans = l - 1;
  for (int i = first2[x]; i; i = next2[i])
    if (a2[i] != fa) {
      dfs(a2[i], x);
    }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    for (int j = 1; j <= x; j++) {
      scanf("%d", &y);
      add(i, y);
    }
  }
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &x, &y);
    add2(x, y);
    add2(y, x);
  }
  ans = 1;
  dfs(1, 0);
  printf("%d\n", ans);
  for (int i = 1; i <= m; i++)
    if (res[i] == 0)
      printf("1 ");
    else
      printf("%d ", res[i]);
  return 0;
}
