#include <bits/stdc++.h>
using namespace std;
set<int> use[300005];
vector<int> a, b;
vector<int> Next[300005];
vector<int> Edge[300005];
int all[300005] = {0};
int ans[300005] = {0};
int con[300005] = {0};
int now = 1;
void F(int here, int fa) {
  set<int> have;
  int i;
  for (i = 1; i <= all[here]; i++) have.insert(i);
  for (auto i : use[here])
    if (use[fa].find(i) != use[fa].end()) have.erase(ans[i]);
  for (auto i : use[here]) {
    if (use[fa].find(i) == use[fa].end()) {
      ans[i] = *have.begin();
      have.erase(ans[i]);
    }
  }
  for (auto i : Next[here])
    if (i != fa) F(i, here);
}
int main() {
  int N, M, i, j, K, K2, x, y, t;
  scanf("%d %d", &N, &M);
  for (i = 1; i <= N; i++) {
    scanf("%d", &K);
    all[i] = K;
    now = max(now, K);
    while (K--) {
      scanf("%d", &t);
      use[i].insert(t);
      con[t]++;
    }
  }
  for (i = 1; i < N; i++) {
    scanf("%d %d", &x, &y);
    Next[x].push_back(y);
    Next[y].push_back(x);
  }
  F(1, 0);
  for (i = 1; i <= M; i++)
    if (con[i] == 0) ans[i] = 1;
  printf("%d\n", now);
  for (i = 1; i <= M; i++) printf("%d ", ans[i]);
  return 0;
}
