#include <bits/stdc++.h>
using namespace std;
const int N = 500200;
const int mod = 1e9 + 7;
const long long INF = 1e18;
vector<int> col[N];
int root;
int ans;
int s[N];
vector<int> g[N];
int u, v, x;
set<int> colors;
int used[N];
inline void dfs(int v, int p = -1) {
  for (int x : col[v])
    if (used[x]) colors.erase(used[x]);
  for (int x : col[v]) {
    if (!used[x]) {
      used[x] = *colors.begin();
      colors.erase(colors.begin());
    }
  }
  for (int x : col[v]) colors.insert(used[x]);
  for (int to : g[v])
    if (to != p) dfs(to, v);
}
int main() {
  int n, m;
  ans = 1;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", s + i);
    ans = max(ans, s[i]);
    if (ans == s[i]) root = i;
    for (int j = 1; j <= s[i]; ++j) {
      scanf("%d", &x);
      col[i].push_back(x);
    }
  }
  for (int i = 1; i <= ans; ++i) colors.insert(i);
  for (int i = 1; i < n; ++i) {
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(root);
  printf("%d\n", ans);
  for (int i = 1; i <= m; ++i)
    if (!used[i])
      printf("%d ", 1);
    else
      printf("%d ", used[i]);
  return 0;
}
