#include <bits/stdc++.h>
using namespace std;
vector<int> Vs[300050];
vector<int> conn[300050];
int ans[300050];
bool chk[500050];
bool dchk[300050];
vector<int> Vu;
void DFS(int n) {
  dchk[n] = true;
  for (auto it : Vs[n]) {
    if (ans[it])
      chk[ans[it]] = true;
    else
      Vu.push_back(it);
  }
  int a = 1;
  for (auto it : Vu) {
    while (chk[a]) a++;
    ans[it] = a++;
  }
  for (auto it : Vs[n]) chk[ans[it]] = false;
  Vu.clear();
  for (auto it : conn[n]) {
    if (dchk[it]) continue;
    DFS(it);
  }
}
int main() {
  int N, M, i;
  scanf("%d %d", &N, &M);
  for (i = 1; i <= N; i++) {
    int t1, t2;
    scanf("%d", &t1);
    while (t1--) {
      scanf("%d", &t2);
      Vs[i].push_back(t2);
    }
  }
  for (i = 1; i < N; i++) {
    int t1, t2;
    scanf("%d %d", &t1, &t2);
    conn[t1].push_back(t2);
    conn[t2].push_back(t1);
  }
  DFS(1);
  int mx = 0;
  for (i = 1; i <= M; i++) {
    mx = max(mx, ans[i]);
    if (ans[i] == 0) ans[i] = 1;
  }
  mx = max(mx, 1);
  printf("%d\n", mx);
  for (i = 1; i <= M; i++) printf("%d ", ans[i]);
  return !printf("\n");
}
