#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3E5 + 5;
int n, m, sz, val, x, y, color[MAXN];
vector<int> v[MAXN], G[MAXN];
template <class T>
void read(T& x, T f = 1, char ch = getchar()) {
  x = 0;
  while (ch < '0' || ch > '9') f = (ch == '-' ? -1 : 1), ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - 48, ch = getchar();
  x *= f;
}
void dfs(int now, int f) {
  map<int, int> vis;
  for (int i = 0; i < v[now].size(); i++)
    if (color[v[now][i]]) vis[color[v[now][i]]] = 1;
  int l = 0;
  for (int i = 0; i < v[now].size(); i++) {
    if (!color[v[now][i]]) {
      while (vis[++l])
        ;
      color[v[now][i]] = l;
    }
  }
  for (int i = 0; i < G[now].size(); i++) {
    int to = G[now][i];
    if (to != f) dfs(to, now);
  }
}
signed main() {
  read(n), read(m);
  int ans = 1;
  for (int i = 1; i <= n; i++) {
    read(sz);
    for (int j = 1; j <= sz; j++) {
      read(val);
      v[i].push_back(val);
    }
    ans = max(ans, sz);
  }
  for (int i = 1; i <= n - 1; i++) {
    read(x), read(y);
    G[x].push_back(y), G[y].push_back(x);
  }
  dfs(1, 1);
  printf("%d\n", ans);
  for (int i = 1; i <= m; i++) printf("%d ", color[i] ? color[i] : 1);
  return 0;
}
