#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
vector<int> G[N], col[N];
int paint[N], tick[N], xxx;
void dfs(int x, int p = 0) {
  xxx++;
  vector<int> decide;
  for (int c : col[x]) {
    if (paint[c]) {
      assert(tick[paint[c]] < xxx);
      tick[paint[c]] = xxx;
    } else {
      decide.push_back(c);
    }
  }
  int l = 1;
  for (int c : decide) {
    while (tick[l] == xxx) l++;
    paint[c] = l;
    tick[l] = xxx;
  }
  for (int u : G[x]) {
    if (u == p) continue;
    dfs(u, x);
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    int s;
    scanf("%d", &s);
    for (int j = 0; j < s; ++j) {
      int x;
      scanf("%d", &x);
      col[i].push_back(x);
    }
  }
  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(1);
  for (int i = 1; i <= m; ++i)
    if (paint[i] == 0) paint[i] = 1;
  int ans = *max_element(paint + 1, paint + 1 + m);
  printf("%d\n", ans);
  for (int i = 1; i <= m; ++i) printf("%d%c", paint[i], " \n"[i == m]);
}
