#include <bits/stdc++.h>
using namespace std;
class color_ice_cream : public pair<bitset<500000>, bitset<500000> > {
  typedef int *int_ptr_t;
  int n, m, last_used_color;
  int_ptr_t color, ice_cream_set_size, *ice_cream;
  vector<int> *neighbor;
  int color_ice_cream_set(const int i) {
    int s, c = -1, k = ice_cream_set_size[i];
    for (int j = 0; j < k; j++)
      if ((s = color[ice_cream[i][j]]) != -1) second.set(s);
    for (int j = 0; j < k; j++)
      if (color[s = ice_cream[i][j]] == -1) {
        while (second[++c])
          ;
        color[s] = c;
      }
    second.reset();
    return c;
  }

 public:
  color_ice_cream() : last_used_color(-1) {
    function<int()> read_int = []() {
      int x;
      scanf("%i", &x);
      return x;
    };
    neighbor = new vector<int>[n = read_int()], ice_cream_set_size = new int[n],
    ice_cream = new int_ptr_t[n];
    color = new int[m = read_int()];
    for (int k, i = 0; i < n; i++) {
      ice_cream[i] = new int[k = ice_cream_set_size[i] = read_int()];
      for (int j = 0; j < k; j++) ice_cream[i][j] = read_int() - 1;
    }
    for (int u, v, i = 1; i < n; i++) {
      neighbor[v = read_int() - 1].push_back(u = read_int() - 1);
      neighbor[u].push_back(v);
    }
    for (int i = 0; i < m; i++) color[i] = -1;
  }
  void solve(const int i = 0) {
    first.set(i);
    int c = color_ice_cream_set(i);
    if (c > last_used_color) last_used_color = c;
    for (int j : neighbor[i])
      if (!first[j]) solve(j);
  }
  void write_solution() {
    function<int(const int)> positive_non_zero_plus_one = [](const int x) {
      return max(x, 0) + 1;
    };
    printf("%i\n", positive_non_zero_plus_one(last_used_color));
    for (int i = 0; i < m; i++)
      printf("%i ", positive_non_zero_plus_one(color[i]));
  }
};
int main() {
  color_ice_cream problem;
  problem.solve();
  problem.write_solution();
}
