#include <bits/stdc++.h>
using namespace std;
int vi[500000], ans[500000], chk[500000];
vector<int> bekar, g[500000], v[500000];
inline void dfs(int n) {
  vi[n] = 1;
  for (auto it : v[n]) {
    if (ans[it])
      chk[ans[it]] = 1;
    else
      bekar.emplace_back(it);
  }
  int a = 1;
  for (auto it : bekar) {
    while (chk[a]) a++;
    ans[it] = a++;
  }
  for (auto it : v[n]) chk[ans[it]] = 0;
  bekar.clear();
  for (auto it : g[n]) {
    if (!vi[it]) dfs(it);
  }
}
int main() {
  int n, m;
  scanf("%d", &n);
  ;
  scanf("%d", &m);
  ;
  int x;
  for (x = 1; x <= n; x++) {
    int kahantk;
    scanf("%d", &kahantk);
    ;
    for (int y = 1; y <= kahantk; y++) {
      int p;
      scanf("%d", &p);
      ;
      v[x].emplace_back(p);
    }
  }
  for (x = 1; x < n; x++) {
    int u, v;
    scanf("%d", &u);
    ;
    scanf("%d", &v);
    ;
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  dfs(1);
  int mx = 0;
  for (x = 1; x <= m; x++) {
    mx = max(mx, ans[x]);
    ans[x] = max(1, ans[x]);
  }
  printf("%d\n", max(mx, 1));
  for (x = 1; x <= m; x++) printf("%d ", ans[x]);
}
