#include <bits/stdc++.h>
using namespace std;
#pragma warning(disable : 4996)
int N, M, A[1 << 19], B[1 << 19], col[1 << 19];
bool used[1 << 19];
vector<int> X[300009], S[300009];
void dfs(int pos) {
  used[pos] = true;
  vector<int> vec;
  for (int i = 0; i < S[pos].size(); i++) {
    if (col[S[pos][i]] >= 1) vec.push_back(col[S[pos][i]]);
  }
  sort(vec.begin(), vec.end());
  int cnt = 1;
  for (int i = 0; i < S[pos].size(); i++) {
    if (col[S[pos][i]] >= 1) continue;
    while (true) {
      int pos1 = lower_bound(vec.begin(), vec.end(), cnt) - vec.begin();
      if (pos1 < vec.size() && vec[pos1] == cnt)
        cnt++;
      else
        break;
    }
    col[S[pos][i]] = cnt;
    cnt++;
  }
  for (int i = 0; i < X[pos].size(); i++) {
    if (used[X[pos][i]] == true) continue;
    dfs(X[pos][i]);
  }
}
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= N; i++) {
    int c;
    scanf("%d", &c);
    for (int j = 0; j < c; j++) {
      int p;
      scanf("%d", &p);
      S[i].push_back(p);
    }
  }
  for (int i = 1; i <= N - 1; i++) {
    scanf("%d%d", &A[i], &B[i]);
    X[A[i]].push_back(B[i]);
    X[B[i]].push_back(A[i]);
  }
  dfs(1);
  for (int i = 1; i <= M; i++) {
    if (col[i] == 0) col[i] = 1;
  }
  int maxn = 0;
  for (int i = 1; i <= M; i++) maxn = max(maxn, col[i]);
  printf("%d\n", maxn);
  for (int i = 1; i <= M; i++) {
    if (i >= 2) printf(" ");
    printf("%d", col[i]);
  }
  printf("\n");
  return 0;
}
