#include <bits/stdc++.h>
using namespace std;
int n, m, col[300003], tag[300003];
vector<int> v[300003], to[300003];
void dfs(int x, int fa) {
  for (int i = 0; i < v[x].size(); ++i)
    if (col[v[x][i]]) tag[col[v[x][i]]] = 1;
  int k = 1;
  for (int i = 0; i < v[x].size(); ++i)
    if (!col[v[x][i]]) {
      while (tag[k]) ++k;
      tag[col[v[x][i]] = k] = 1;
    }
  for (int i = 0; i < v[x].size(); ++i) tag[col[v[x][i]]] = 0;
  for (int i = 0; i < to[x].size(); ++i)
    if (to[x][i] != fa) dfs(to[x][i], x);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, k; i <= n; ++i) {
    scanf("%d", &k);
    for (int j = 1, x; j <= k; ++j) {
      scanf("%d", &x);
      v[i].push_back(x);
    }
  }
  for (int i = 1, u, v; i < n; ++i) {
    scanf("%d%d", &u, &v);
    to[u].push_back(v);
    to[v].push_back(u);
  }
  dfs(1, 0);
  int k = 1;
  for (int i = 1; i <= m; ++i) k = max(k, col[i]);
  printf("%d\n", k);
  for (int i = 1; i <= m; ++i) printf("%d ", col[i] ? col[i] : 1);
}
