#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;
int i, j, n, m, x, y, s[N], ans, col[N], v[N];
vector<int> g[N], ice[N], a;
void dfs(int x, int fa) {
  for (auto y : ice[x])
    if (col[y])
      v[col[y]] = 1;
    else
      a.push_back(y);
  int cnt = 1;
  for (auto y : a) {
    while (v[cnt]) cnt++;
    col[y] = cnt++;
  }
  for (auto y : ice[x]) v[col[y]] = 0;
  a.clear();
  for (auto y : g[x])
    if (y != fa) dfs(y, x);
}
int main() {
  scanf("%d%d", &n, &m);
  ans = 1;
  for (i = 1; i <= n; i++) {
    scanf("%d", &s[i]);
    if (s[i] > ans) ans = s[i];
    for (j = 1; j <= s[i]; j++) scanf("%d", &x), ice[i].push_back(x);
  }
  for (i = 1; i < n; i++) {
    scanf("%d%d", &x, &y);
    g[y].push_back(x);
    g[x].push_back(y);
  }
  dfs(1, -1);
  printf("%d\n", ans);
  for (i = 1; i <= m; i++) printf("%d ", col[i] ? col[i] : 1);
}
