#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 7;
int n, m, tot, col[N];
set<int> S;
vector<int> c[N], e[N];
void dfs(int u, int p) {
  vector<int> tmp;
  for (int i = (0); i < (((int)(c[u]).size())); ++i) {
    int ice = c[u][i];
    if (~col[ice]) {
      tmp.push_back(col[ice]);
      S.erase(tmp.back());
    }
  }
  for (int i = (0); i < (((int)(c[u]).size())); ++i) {
    int ice = c[u][i];
    if (col[ice] == -1) {
      col[ice] = *S.begin();
      tmp.push_back(col[ice]);
      S.erase(tmp.back());
    }
  }
  for (int i = (0); i < (((int)(tmp).size())); ++i) S.insert(tmp[i]);
  for (int i = (0); i < (((int)(e[u]).size())); ++i) {
    int v = e[u][i];
    if (v == p) continue;
    dfs(v, u);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  tot = 1;
  for (int i = (1); i < (n + 1); ++i) {
    int si;
    scanf("%d", &si);
    tot = max(tot, si);
    for (int j = (0); j < (si); ++j) {
      int x;
      scanf("%d", &x);
      c[i].push_back(x);
    }
  }
  for (int i = (1); i < (tot + 1); ++i) S.insert(i);
  for (int i = (1); i < (n); ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    e[u].push_back(v);
    e[v].push_back(u);
  }
  memset(col, -1, sizeof(col));
  dfs(1, -1);
  printf("%d\n", tot);
  for (int i = (1); i < (m + 1); ++i)
    printf("%d%c", ~col[i] ? col[i] : 1, " \n"[i == m]);
  return 0;
}
