#include <bits/stdc++.h>
using namespace std;
vector<int> adj[300300];
vector<int> col[300300];
int color[300300], vst[300300], runs;
void dfs(int u, int fa = 0) {
  runs++;
  for (int i = col[u].size(); i--;) {
    int c = col[u][i];
    if (color[c]) {
      vst[color[c]] = runs;
      continue;
    }
  }
  int tmp = 1;
  for (int i = col[u].size(); i--;) {
    int c = col[u][i];
    if (color[c]) continue;
    while (vst[tmp] == runs) tmp++;
    color[c] = tmp;
    vst[tmp] = runs;
  }
  for (int i = adj[u].size(); i--;) {
    int v = adj[u][i];
    if (v == fa) continue;
    dfs(v, u);
  }
}
int main() {
  int n, m, u, v;
  cin >> n >> m;
  int ans = 1;
  for (int i = 1; i <= n; i++) {
    int k, c;
    scanf("%d", &k);
    ans = max(ans, k);
    while (k--) {
      scanf("%d", &c);
      col[i].push_back(c);
    }
  }
  for (int i = 1; i < n; i++) {
    scanf("%d %d", &u, &v);
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1);
  printf("%d\n", ans);
  for (int i = 1; i <= m; i++) printf("%d ", max(1, color[i]));
  return 0;
}
