#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 2;
int n, m, mxcol = 1, col[N];
vector<int> g[N], c[N];
void Dfs(int u, int p) {
  set<int> cand;
  for (int i = 1; i <= (int)c[u].size(); ++i) cand.insert(i);
  mxcol = max(mxcol, (int)c[u].size());
  vector<int> tobe;
  for (int x : c[u])
    if (!col[x])
      tobe.push_back(x);
    else {
      auto it = cand.lower_bound(col[x]);
      if (it != cand.end() && *it == col[x]) cand.erase(it);
    }
  for (int x : tobe) col[x] = *cand.begin(), cand.erase(cand.begin());
  cand.clear();
  for (int v : g[u])
    if (v != p) Dfs(v, u);
}
void Solve() {
  Dfs(1, 0);
  printf("%d\n", mxcol);
  for (int i = 1; i <= m; ++i)
    if (!col[i]) col[i] = 1;
  for (int i = 1; i <= m; ++i) printf("%d ", col[i]);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, sze; i <= n; ++i) {
    scanf("%d", &sze);
    c[i].resize(sze);
    for (int j = 0; j < sze; ++j) scanf("%d", &c[i][j]);
  }
  for (int i = 0, u, v; i < n - 1; ++i) {
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  Solve();
  return 0;
}
