#include <bits/stdc++.h>
using namespace std;
const int max_n = 3e5 + 5;
int n, m;
vector<int> g[max_n];
vector<int> types[max_n];
int kolor[max_n];
void dfs(int v, int par) {
  vector<int> uzyte;
  for (__typeof((types[v]).begin()) i = ((types[v]).begin());
       i != (types[v]).end(); ++i) {
    int lod = *i;
    if (kolor[lod] != -1) uzyte.push_back(kolor[lod]);
  }
  sort((uzyte).begin(), (uzyte).end());
  int pos = 0;
  int myc = 0;
  for (__typeof((types[v]).begin()) i = ((types[v]).begin());
       i != (types[v]).end(); ++i) {
    int lod = *i;
    if (kolor[lod] == -1) {
      while (pos < uzyte.size() && uzyte[pos] == myc) {
        myc++;
        pos++;
      }
      if (pos == uzyte.size()) {
        kolor[lod] = myc;
        myc++;
      } else {
        kolor[lod] = myc;
        myc++;
      }
    }
  }
  for (__typeof((g[v]).begin()) i = ((g[v]).begin()); i != (g[v]).end(); ++i) {
    int w = *i;
    if (w == par) continue;
    dfs(w, v);
  }
}
int main() {
  scanf("%d%d", &(n), &(m));
  ;
  int ans = 0;
  for (int i = (0); i < (n); ++i) {
    int(x);
    scanf("%d", &(x));
    ;
    ans = max(ans, x);
    for (int j = (0); j < (x); ++j) {
      int(b);
      scanf("%d", &(b));
      ;
      b--;
      types[i].push_back(b);
    }
  }
  for (int i = (0); i < (n - 1); ++i) {
    int(u), (v);
    scanf("%d%d", &(u), &(v));
    ;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = (0); i < (m); ++i) kolor[i] = -1;
  dfs(0, 0);
  ans = max(ans, 1);
  printf("%d\n", ans);
  for (int i = (0); i < (m); ++i) {
    if (kolor[i] == -1)
      printf("1 ");
    else
      printf("%d ", kolor[i] + 1);
  }
  printf("\n");
}
