#include <bits/stdc++.h>
using namespace std;
map<int, int> color;
map<int, int> used[300010];
vector<int> v[300010];
vector<int> List[300010];
void go(int cur, int par) {
  for (auto x : List[cur])
    if (color[x]) used[cur][color[x]] = 1;
  int color_cnt = 1;
  for (auto x : List[cur])
    if (!color[x]) {
      while (used[cur][color_cnt]) color_cnt++;
      color[x] = color_cnt;
      used[cur][color_cnt] = 1;
    }
  for (auto x : v[cur])
    if (x != par) go(x, cur);
}
int main() {
  int n, m, i, j;
  scanf("%d", &n);
  scanf("%d", &m);
  for (i = 1; i <= n; i++) {
    int s;
    scanf("%d", &s);
    while (s--) {
      int x;
      scanf("%d", &x);
      List[i].push_back(x);
    }
  }
  for (i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d", &x);
    scanf("%d", &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  go(1, 1);
  int ans = 0;
  int color_cnt = 1;
  for (i = 1; i <= m; i++) {
    if (!color[i]) color[i] = 1;
    ans = max(ans, color[i]);
  }
  printf("%d\n", ans);
  for (i = 1; i <= m; i++) printf("%d ", color[i]);
  printf("\n");
}
