#include <bits/stdc++.h>
using namespace std;
int sz[313131];
pair<int, int> all[313131];
vector<int> A[313131];
int ck[515151], ans[313131];
vector<int> edge[313131];
void fill(int i) {
  for (int v : A[i]) {
    ck[ans[v]] = 1;
  }
  int now = 1;
  for (int v : A[i]) {
    if (ans[v] == 0) {
      while (ck[now]) now++;
      ans[v] = now++;
    }
  }
  for (int v : A[i]) ck[ans[v]] = 0;
}
void dfs(int w, int bef) {
  fill(w);
  for (int nxt : edge[w])
    if (nxt != bef) dfs(nxt, w);
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int mx = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &sz[i]);
    all[i] = {-sz[i], i};
    for (int j = 0; j < sz[i]; j++) {
      int v;
      scanf("%d", &v);
      A[i].push_back(v);
    }
    mx = max(sz[i], mx);
  }
  for (int i = 1; i < n; i++) {
    int s, e;
    scanf("%d%d", &s, &e);
    s--;
    e--;
    edge[s].push_back(e);
    edge[e].push_back(s);
  }
  int st = 0;
  for (int i = 0; i < n; i++) {
    if (edge[i].size() == 1) st = i;
  }
  dfs(st, -1);
  if (mx == 0) mx = 1;
  for (int i = 1; i <= m; i++) {
    if (ans[i] == 0) ans[i] = 1;
  }
  printf("%d\n", mx);
  for (int i = 1; i <= m; i++) {
    printf("%d ", ans[i]);
  }
  return 0;
}
