#include <bits/stdc++.h>
using namespace std;
int n, m, s[300001], c[300001], res;
vector<int> t[300001];
vector<int> adj[300001];
void f(int p, int u) {
  vector<int> used;
  for (int i = 0; i < s[u]; i++)
    if (c[t[u][i]]) used.push_back(c[t[u][i]]);
  sort(used.begin(), used.end());
  int color = 1;
  for (int i = 0, j = 0; i < s[u]; i++) {
    if (c[t[u][i]]) continue;
    while (j < used.size() && used[j] == color) color++, j++;
    c[t[u][i]] = color++;
    res = max(res, c[t[u][i]]);
  }
  for (auto to : adj[u])
    if (to != p) f(u, to);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", s + i);
    for (int j = 0, x; j < s[i]; j++) {
      scanf("%d", &x);
      t[i].push_back(x);
    }
  }
  for (int i = 0, u, v; i < n - 1; i++) {
    scanf("%d%d", &u, &v);
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  f(0, 1);
  printf("%d\n", max(res, 1));
  for (int i = 1; i <= m; i++) printf("%d ", max(c[i], 1));
  return 0;
}
