#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> s[300010];
vector<int> g[300010];
char used[300010];
int color[300010];
char usedcolor[300010];
void dfs(int v) {
  used[v] = true;
  for (auto u : s[v]) {
    usedcolor[color[u]] = 1;
  }
  int lo = 1;
  for (auto u : s[v]) {
    while (usedcolor[lo]) lo++;
    if (color[u] == 0) {
      color[u] = lo;
      usedcolor[lo] = 1;
    }
  }
  for (auto u : s[v]) usedcolor[color[u]] = 0;
  for (auto to : g[v]) {
    if (!used[to]) dfs(to);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    int si;
    scanf("%d", &si);
    for (int j = 0; j < si; j++) {
      int t;
      scanf("%d", &t);
      s[i].push_back(t - 1);
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int from, to;
    scanf("%d%d", &from, &to);
    g[from - 1].push_back(to - 1);
    g[to - 1].push_back(from - 1);
  }
  int start = 0, t = 0;
  for (int i = 0; i < n; i++) {
    if (s[i].size() > t) {
      t = s[i].size();
      start = i;
    }
  }
  int cur = 1;
  for (auto u : s[start]) {
    color[u] = cur++;
  }
  dfs(start);
  int mx = 0;
  for (int i = 0; i < m; i++) {
    if (color[i] == 0) color[i] = 1;
    mx = max(mx, color[i]);
  }
  printf("%d\n", mx);
  for (int i = 0; i < m; i++) printf("%d ", color[i]);
  return 0;
}
