#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char ch;
  int x = 0, f = 1;
  while (ch = getchar(), ch < '!')
    ;
  if (ch == '-') f = -1, ch = getchar();
  while (x = x * 10 + ch - '0', ch = getchar(), isdigit(ch))
    ;
  return f * x;
}
const int N = 300005;
vector<int> c[N], G[N];
int n, m, col[N];
bool vis[N];
void dfs(int u, int fa) {
  int cur = 1;
  for (int i = 0; i < (int)(c[u]).size(); i++) vis[col[c[u][i]]] = 1;
  for (int i = 0; i < (int)(c[u]).size(); i++) {
    int x = c[u][i];
    if (!col[x]) {
      while (vis[cur]) cur++;
      col[x] = cur;
      vis[col[x]] = 1;
    }
  }
  for (int i = 0; i < (int)(c[u]).size(); i++) vis[col[c[u][i]]] = 0;
  for (int i = 0; i < (int)(G[u]).size(); i++)
    if (G[u][i] != fa) dfs(G[u][i], u);
}
int main() {
  n = read(), m = read();
  for (int i = 1, x, y; i <= n; i++) {
    x = read();
    while (x--) y = read(), c[i].push_back(y);
  }
  for (int i = 1, u, v; i < n; i++) {
    u = read(), v = read();
    G[u].push_back(v), G[v].push_back(u);
  }
  dfs(1, 0);
  for (int i = 1; i <= m; i++)
    if (!col[i]) col[i] = 1;
  printf("%d\n", *max_element(col + 1, col + m + 1));
  for (int i = 1; i <= m; i++) printf("%d ", col[i]);
  return 0;
}
