#include <bits/stdc++.h>
using namespace std;
int col[300005], lst[300005], to[600005], pre[600005], tot;
bool vis[300005];
vector<int> vec[300005];
inline void add_edge(int u, int v) {
  to[tot] = v;
  pre[tot] = lst[u];
  lst[u] = tot++;
}
void dfs(int u, int fa = -1) {
  for (int i = 0; i < vec[u].size(); i++) {
    if (col[vec[u][i]]) vis[col[vec[u][i]]] = true;
  }
  int idx = 1;
  for (int i = 0; i < vec[u].size(); i++) {
    if (col[vec[u][i]]) continue;
    while (vis[idx]) idx++;
    col[vec[u][i]] = idx;
    vis[idx] = true;
  }
  for (int i = 0; i < vec[u].size(); i++) vis[col[vec[u][i]]] = false;
  for (int i = lst[u]; ~i; i = pre[i]) {
    if (to[i] == fa) continue;
    dfs(to[i], u);
  }
}
int main() {
  memset(lst, -1, sizeof(lst));
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    int len;
    scanf("%d", &len);
    for (int j = 0; j < len; j++) {
      int x;
      scanf("%d", &x);
      vec[i].push_back(--x);
    }
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    add_edge(--u, --v);
    add_edge(v, u);
  }
  dfs(0);
  int ans = 0;
  for (int i = 0; i < m; i++) {
    if (!col[i]) col[i] = 1;
    ans = max(ans, col[i]);
  }
  printf("%d\n", ans);
  for (int i = 0; i < m; i++) printf("%d%c", col[i], " \n"[i + 1 == m]);
  return 0;
}
