#include <bits/stdc++.h>
using namespace std;
const int maxn = 500000;
vector<int> G[maxn], H[maxn];
int id[maxn], p[maxn];
void dfs(int x, int fa) {
  for (int i = 0; i < H[x].size(); ++i) {
    int X = H[x][i];
    if (id[X]) p[id[X]] = x;
  }
  int cnt = 1;
  for (int i = 0; i < H[x].size(); ++i) {
    int X = H[x][i];
    if (!id[X]) {
      while (p[cnt] == x) ++cnt;
      id[X] = cnt;
      ++cnt;
    }
  }
  for (int i = 0; i < G[x].size(); ++i) {
    int y = G[x][i];
    if (y == fa) continue;
    dfs(y, x);
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int mm = 1, mn = 1;
  for (int i = 1; i <= n; ++i) {
    int k;
    scanf("%d", &k);
    for (int j = 1; j <= k; ++j) {
      int x;
      scanf("%d", &x);
      H[i].push_back(x);
    }
    if (k > mm) {
      mm = k;
      mn = i;
    }
  }
  for (int i = 1; i < n; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs(mn, mn);
  printf("%d\n", mm);
  for (int i = 1; i <= m; ++i) {
    printf("%d", id[i] ? id[i] : 1);
    if (i == m)
      puts("");
    else
      printf(" ");
  }
  return 0;
}
