#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)3e5 + 10;
bool vis[maxn];
bool kk[maxn];
int cnt[maxn];
int color;
vector<int> ve[maxn];
int head[maxn];
int e;
struct node {
  int v;
  int next;
} T[maxn << 1];
void add(int u, int v) {
  T[e] = {v, head[u]};
  head[u] = e++;
}
void dfs(int pos) {
  vis[pos] = true;
  int o = 1;
  for (int i : ve[pos]) {
    if (cnt[i] != 0) {
      kk[cnt[i]] = true;
    }
  }
  for (int i : ve[pos]) {
    if (cnt[i] == 0) {
      while (kk[o]) {
        o++;
      }
      cnt[i] = o++;
    }
  }
  for (int i : ve[pos]) {
    kk[cnt[i]] = false;
  }
  color = max(color, o - 1);
  for (int i = head[pos]; ~i; i = T[i].next) {
    int v = T[i].v;
    if (vis[v]) continue;
    dfs(v);
  }
}
int main() {
  memset(head, -1, sizeof(head));
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    while (x--) {
      int y;
      scanf("%d", &y);
      ve[i].push_back(y);
    }
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  dfs(1);
  if (color == 0) {
    printf("1\n1");
  } else {
    printf("%d\n", color);
    if (cnt[1] == 0)
      printf("1");
    else
      printf("%d", cnt[1]);
  }
  for (int i = 2; i <= m; i++) {
    if (cnt[i] == 0)
      printf(" 1");
    else
      printf(" %d", cnt[i]);
  }
  printf("\n");
  return 0;
}
