#include <bits/stdc++.h>
using namespace std;
struct node {
  int y, nex;
} a[2 * 310000];
int n, m, fir[310000], len, ans[310000], cnt;
vector<int> v[310000];
bool b[310000];
void ins(int x, int y) {
  a[++len].y = y;
  a[len].nex = fir[x];
  fir[x] = len;
}
void dfs(int x, int fa) {
  int siz = v[x].size();
  cnt = max(cnt, siz);
  for (int i = 0; i < siz; i++)
    if (ans[v[x][i]]) b[ans[v[x][i]]] = 1;
  int j = 1;
  for (int i = 0; i < siz; i++) {
    if (ans[v[x][i]]) continue;
    while (b[j]) j++;
    ans[v[x][i]] = j;
    b[j] = 1;
    j++;
  }
  for (int i = 0; i < siz; i++) b[ans[v[x][i]]] = 0;
  for (int k = fir[x]; k; k = a[k].nex) {
    int y = a[k].y;
    if (y == fa) continue;
    dfs(y, x);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int k;
    scanf("%d", &k);
    while (k--) {
      int x;
      scanf("%d", &x);
      v[i].push_back(x);
    }
  }
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    ins(x, y);
    ins(y, x);
  }
  cnt = 1;
  dfs(1, 0);
  printf("%d\n", cnt);
  for (int i = 1; i <= m; i++) {
    if (ans[i] == 0) ans[i] = 1;
    printf("%d ", ans[i]);
  }
  return 0;
}
