#include <bits/stdc++.h>
using namespace std;
int const maxn = 320000;
int n, m, u, v, maxx, cnt;
int head[maxn], to[maxn * 2], Next[maxn * 2];
int cl[maxn], vis[maxn];
list<int> s[maxn];
list<int>::iterator iter;
set<int> st;
void addedge(int u, int v) {
  to[cnt] = v;
  Next[cnt] = head[u];
  head[u] = cnt++;
  to[cnt] = u;
  Next[cnt] = head[v];
  head[v] = cnt++;
  return;
}
void dfs(int now) {
  st.clear();
  for (iter = s[now].begin(); iter != s[now].end(); iter++) {
    if (cl[*iter]) st.insert(cl[*iter]);
  }
  int nowcl = 1;
  for (iter = s[now].begin(); iter != s[now].end(); iter++) {
    if (!cl[*iter]) {
      while (st.find(nowcl) != st.end()) ++nowcl;
      cl[*iter] = nowcl;
      if (nowcl > maxx) maxx = nowcl;
      nowcl++;
    }
  }
  for (int i = head[now]; ~i; i = Next[i]) {
    if (!vis[to[i]]) {
      vis[to[i]] = 1;
      dfs(to[i]);
    }
  }
  return;
}
int main() {
  int sz, t;
  maxx = 1, cnt = 1;
  memset(head, -1, sizeof(head));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &sz);
    for (int j = 1; j <= sz; j++) {
      scanf("%d", &t);
      s[i].push_back(t);
    }
  }
  for (int i = 1; i <= n - 1; i++) {
    scanf("%d%d", &u, &v);
    addedge(u, v);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      vis[i] = 1;
      dfs(i);
    }
  }
  printf("%d\n", maxx);
  for (int i = 1; i <= m; i++) {
    if (!cl[i]) cl[i] = 1;
    printf("%d ", cl[i]);
  }
  printf("\n");
}
