#include <bits/stdc++.h>
using namespace std;
const int MAX = 300005;
int N, M, C;
vector<int> nodes[MAX];
bool used[MAX];
int colors[MAX];
vector<int> tree[MAX];
void dfs(int n, int p) {
  int ct = 1;
  for (int ic : nodes[n]) {
    if (colors[ic] != -1) {
      used[colors[ic]] = true;
    }
  }
  for (int ic : nodes[n]) {
    if (colors[ic] != -1) {
      continue;
    }
    while (used[ct]) {
      ct++;
    }
    colors[ic] = ct;
    used[ct] = true;
  }
  for (int ic : nodes[n]) {
    used[colors[ic]] = false;
  }
  for (int c : tree[n]) {
    if (c == p) {
      continue;
    }
    dfs(c, n);
  }
}
int main() {
  scanf("%d%d", &N, &M);
  C = 1;
  for (int i = 0; i < N; i++) {
    int t;
    scanf("%d", &t);
    C = max(C, t);
    for (int j = 0; j < t; j++) {
      int a;
      scanf("%d", &a);
      nodes[i + 1].push_back(a);
    }
  }
  for (int i = 0; i < M + 1; i++) {
    colors[i] = -1;
  }
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    tree[u].push_back(v);
    tree[v].push_back(u);
  }
  dfs(1, -1);
  for (int i = 0; i < M; i++) {
    if (colors[i + 1] == -1) {
      colors[i + 1] = 1;
    }
  }
  printf("%d\n", C);
  for (int i = 0; i < M; i++) {
    printf("%d ", colors[i + 1]);
  }
  return 0;
}
