#include <bits/stdc++.h>
using namespace std;
vector<int> G[300005], E[300005];
int n, m, ans, col[300005], cnt, vis[300005];
void dfs(int t, int fa) {
  int i, cnt = 1;
  for (i = 0; i < E[t].size(); i++)
    if (col[E[t][i]]) vis[col[E[t][i]]] = t;
  for (i = 0; i < E[t].size(); i++)
    if (!col[E[t][i]]) {
      while (vis[cnt] == t) cnt++;
      vis[cnt] = t, col[E[t][i]] = cnt;
    }
  for (i = 0; i < G[t].size(); i++)
    if (G[t][i] != fa) dfs(G[t][i], t);
}
int main() {
  int i, x, y;
  scanf("%d %d", &n, &m);
  for (i = 1; i <= n; i++) {
    scanf("%d", &x), ans = max(ans, x);
    while (x--) scanf("%d", &y), E[i].push_back(y);
  }
  for (i = 1; i < n; i++)
    scanf("%d%d", &x, &y), G[x].push_back(y), G[y].push_back(x);
  dfs(1, 0);
  printf("%d\n", max(ans, 1));
  for (i = 1; i <= m; i++) printf("%d ", max(col[i], 1));
  return 0;
}
