#include <bits/stdc++.h>
using namespace std;
vector<int> flavors[300001];
vector<int> adj[300001];
int color[300001];
void dfs(int v, int p) {
  set<int> colors;
  for (auto f : flavors[v])
    if (color[f] != -1) colors.insert(color[f]);
  int first = 1;
  for (auto f : flavors[v])
    if (color[f] == -1) {
      while (colors.find(first) != colors.end()) first++;
      color[f] = first++;
    }
  for (auto w : adj[v])
    if (w != p) dfs(w, v);
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  int ans = 0;
  int start = 0;
  for (int i = 0; i < n; i++) {
    int s;
    scanf("%d", &s);
    if (ans < s) {
      ans = s;
      start = i;
    }
    for (int j = 0; j < s; j++) {
      int tmp;
      scanf("%d", &tmp);
      flavors[i].push_back(tmp - 1);
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    adj[u - 1].push_back(v - 1);
    adj[v - 1].push_back(u - 1);
  }
  memset(color, -1, sizeof color);
  dfs(0, 0);
  printf("%d\n", max(1, ans));
  for (int i = 0; i < m; i++) printf("%d ", color[i] != -1 ? color[i] : 1);
  printf("\n");
}
