#include <bits/stdc++.h>
using namespace std;
int pi = 1000000000ll + 7ll;
int used[300300], color[300300];
vector<int> vrr[300300], mrr[300300];
void way(int v, int p = -1) {
  for (int x : mrr[v]) {
    used[color[x]] = 1;
  }
  int col = 1;
  for (int x : mrr[v]) {
    if (color[x] == 0) {
      while (used[col] == 1) {
        col++;
      }
      color[x] = col;
      used[col] = 1;
    }
  }
  for (int x : mrr[v]) {
    used[color[x]] = 0;
  }
  for (int x : vrr[v]) {
    if (x != p) way(x, v);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int ans, n, m, k, a, b;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> k;
    mrr[i].resize(k);
    for (int j = 0; j < k; j++) {
      cin >> mrr[i][j];
    }
  }
  for (int i = 1; i < n; i++) {
    cin >> a >> b;
    vrr[a].push_back(b);
    vrr[b].push_back(a);
  }
  way(1);
  ans = 1;
  for (int i = 1; i <= m; i++) {
    if (color[i] == 0) color[i] = 1;
    if (ans < color[i]) ans = color[i];
  }
  cout << ans << '\n';
  for (int i = 1; i <= m; i++) {
    cout << color[i] << " ";
  }
  return 0;
}
