#include <bits/stdc++.h>
using namespace std;
const int maxn = 300010;
int n, m, s;
vector<int> g[maxn];
vector<int> ve[maxn];
int color[maxn];
bool vis[maxn];
int sol[maxn];
void dfs(const int u) {
  vis[u] = 0;
  for (int i = 0; i < ((int)(ve[u]).size()); ++i) {
    int c = ve[u][i];
    if (color[c] != -1) sol[color[c]] = u;
  }
  int k = 1;
  for (int i = 0; i < ((int)(ve[u]).size()); ++i) {
    int c = ve[u][i];
    if (color[c] == -1) {
      while (sol[k] == u) ++k;
      sol[k] = u;
      color[c] = k;
    }
  }
  for (int i = 0; i < ((int)(g[u]).size()); ++i) {
    int v = g[u][i];
    if (vis[v]) dfs(v);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  int a, b;
  for (int k = (1); k <= (n); ++k) {
    scanf("%d", &s);
    for (int i = 0; i < (s); ++i) {
      scanf("%d", &a);
      ve[k].push_back(a);
    }
  }
  for (int k = (2); k <= (n); ++k) {
    scanf("%d%d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  memset(vis, 1, sizeof(vis));
  memset(color, -1, sizeof(color));
  for (int i = (1); i <= (n); ++i)
    if (vis[i]) dfs(i);
  int ans = 0;
  for (int i = (1); i <= (m); ++i) ans = max(ans, color[i]);
  if (ans == 0) ans = 1;
  for (int i = (1); i <= (m); ++i)
    if (color[i] == -1) color[i] = 1;
  printf("%d\n", ans);
  for (int i = (1); i <= (m); ++i) {
    if (i > 1) printf(" ");
    printf("%d", color[i]);
  }
  printf("\n");
  return 0;
}
