#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
struct edge {
  int u, v;
  int next;
} e[maxn << 1];
int p[maxn], eid;
inline void add(int u, int v) {
  e[++eid].u = u, e[eid].v = v;
  e[eid].next = p[u];
  p[u] = eid;
}
int n, m, tot;
vector<int> s[maxn];
map<int, int> ans;
bool vis[maxn];
inline void dfs(int u, int fa) {
  for (register int i = 0; i < s[u].size(); ++i) vis[ans[s[u][i]]] = 1;
  int cnt = 1;
  for (register int i = 0; i < s[u].size(); ++i) {
    if (ans[s[u][i]] != 0) continue;
    while (vis[cnt]) cnt++;
    vis[cnt] = 1;
    ans[s[u][i]] = cnt;
  }
  tot = max(tot, cnt);
  for (register int i = 0; i < s[u].size(); ++i) vis[ans[s[u][i]]] = 0;
  for (register int i = p[u]; i; i = e[i].next) {
    if (e[i].v == fa) continue;
    dfs(e[i].v, u);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  int x, y;
  for (register int i = 1; i <= n; ++i) {
    scanf("%d", &x);
    while (x--) {
      scanf("%d", &y);
      s[i].push_back(y);
    }
  }
  for (register int i = 1; i < n; ++i) {
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  tot = 0;
  dfs(1, 0);
  printf("%d\n", tot);
  for (register int i = 1; i <= m; ++i) {
    if (ans[i] == 0) ans[i] = 1;
    printf("%d ", ans[i]);
  }
  return 0;
}
