#include <bits/stdc++.h>
using namespace std;
const int N = 3e5, M = 3e5;
int color[M + 1];
vector<int> adj[N + 1], S[N + 1];
void dfs(int u, int p) {
  set<int> U;
  U.insert(0);
  for (int i = (0), i_end = (S[u].size()); i < i_end; ++i) {
    int j = S[u][i];
    if (color[j]) U.insert(color[j]);
  }
  set<int>::iterator it = U.begin();
  for (int i = (0), i_end = (S[u].size()); i < i_end; ++i) {
    int j = S[u][i];
    if (!color[j]) {
      while (U.find(*it + 1) != U.end()) ++it;
      U.insert(color[j] = *it + 1);
      ++it;
    }
  }
  for (int i = (0), i_end = (adj[u].size()); i < i_end; ++i) {
    int v = adj[u][i];
    if (v != p) dfs(v, u);
  }
}
int main() {
  int n, m, c = 1;
  scanf("%d%d", &n, &m);
  for (int i = (1), i_end = (n + 1); i < i_end; ++i) {
    int s;
    scanf("%d", &s);
    c = max(c, s);
    for (int j = (0), j_end = (s); j < j_end; ++j) {
      int x;
      scanf("%d", &x);
      S[i].push_back(x);
    }
  }
  for (int i = (0), i_end = (n - 1); i < i_end; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1, 0);
  printf("%d\n", c);
  for (int i = (1), i_end = (m + 1); i < i_end; ++i)
    printf("%d%c", color[i] ? color[i] : 1, " \n"[i == m]);
  return 0;
}
