#include <bits/stdc++.h>
using namespace std;
vector<int> E[310000];
vector<int> Col[310000];
vector<int> S[310000];
bool vis[310000];
int pok[310000];
void dfs(int x) {
  vis[x] = true;
  vector<int> V;
  set<int> Set;
  for (int c : S[x])
    if (pok[c] != 0) Set.insert(pok[c]);
  int ind = 1;
  for (int c : S[x])
    if (pok[c] == 0) {
      while (Set.count(ind) != 0) ++ind;
      pok[c] = ind;
      ++ind;
    }
  for (int y : E[x])
    if (vis[y] == false) dfs(y);
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    int s;
    scanf("%d", &s);
    int x;
    while (s--) {
      scanf("%d", &x);
      S[i].push_back(x);
    }
  }
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    E[u].push_back(v);
    E[v].push_back(u);
  }
  dfs(1);
  int maxc = 1;
  for (int i = 1; i <= m; ++i) {
    if (pok[i] == 0) pok[i] = 1;
    maxc = max(maxc, pok[i]);
  }
  printf("%d\n", maxc);
  for (int i = 1; i <= m; ++i) printf("%d ", pok[i]);
  puts("");
}
