#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 22;
int n, mx, m;
int col[N];
bool ban[N];
vector<int> s[N], G[N];
void dfs(int v, int p) {
  int ct = 1;
  for (int i = 0; i < s[v].size(); i++)
    if (col[s[v][i]]) ban[col[s[v][i]]] = true;
  for (int i = 0; i < s[v].size(); i++)
    if (!col[s[v][i]]) {
      while (ban[ct]) ct++;
      ban[ct] = true;
      col[s[v][i]] = ct;
      mx = max(mx, ct);
    }
  for (int i = 0; i < s[v].size(); i++) ban[col[s[v][i]]] = false;
  for (int i = 0; i < G[v].size(); i++)
    if (G[v][i] != p) dfs(G[v][i], v);
}
int main() {
  cin >> n >> m;
  for (int i = 1, t, x; i <= n; i++) {
    scanf("%d", &t);
    while (t--) {
      scanf("%d", &x);
      s[i].push_back(x);
    }
  }
  for (int i = 1, x, y; i < n; i++)
    scanf("%d%d", &x, &y), G[x].push_back(y), G[y].push_back(x);
  dfs(1, 0);
  if (mx == 0) mx = 1;
  cout << mx << endl;
  for (int i = 1; i <= m; i++) printf("%d ", max(1, col[i]));
  return 0;
}
