#include <bits/stdc++.h>
using namespace std;
int n, m;
int num[300005] = {};
vector<int> s[300005];
vector<int> d[300005];
int ans = 0, color[300005] = {};
int head[300005] = {}, tot = 0;
int sta1[300005], top1 = 0, sta2[300005], top2 = 0;
bool check[300005] = {};
struct edge {
  int to, next;
} e[300005 << 1];
void dfs(int v, int fa) {
  for (int i = 0; i < num[v]; ++i) {
    if (color[s[v][i]]) {
      check[color[sta2[++top2] = s[v][i]]] = true;
      continue;
    }
    sta1[++top1] = s[v][i];
  }
  int c = 1;
  while (top1) {
    while (check[c]) ++c;
    color[sta1[top1--]] = c++;
  }
  while (top2) check[color[sta2[top2--]]] = false;
  for (int i = head[v]; i != 0; i = e[i].next) {
    if (e[i].to == fa) continue;
    dfs(e[i].to, v);
  }
}
inline void AC() {
  cin >> n >> m;
  int x, y;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", num + i);
    if (num[ans] < num[i]) ans = i;
    for (int j = 1; j <= num[i]; ++j) {
      scanf("%d", &x);
      s[i].push_back(x);
    }
  }
  for (int i = 1; i < n; ++i) {
    scanf("%d%d", &x, &y);
    e[++tot] = edge{y, head[x]}, head[x] = tot;
    e[++tot] = edge{x, head[y]}, head[y] = tot;
  }
  if (num[ans])
    cout << num[ans] << '\n';
  else
    cout << 1 << '\n';
  dfs(1, 1);
  for (int i = 1; i <= m; ++i)
    if (color[i])
      printf("%d ", color[i]);
    else
      printf("1 ");
}
int main() {
  AC();
  return 0;
}
