#include <bits/stdc++.h>
using namespace std;
int n, m, t, x, y, f[300010], ans;
bool flag[300010];
vector<int> v[300010], mp[300010];
void Sol(int x) {
  for (int i = 0; i < v[x].size(); i++) flag[f[v[x][i]]] = 1;
  int cur = 1;
  for (int i = 0; i < v[x].size(); i++)
    if (f[v[x][i]] == 0) {
      while (flag[cur]) cur++;
      flag[cur] = 1;
      f[v[x][i]] = cur;
    }
  ans = max(ans, cur);
  for (int i = 0; i < v[x].size(); i++) flag[f[v[x][i]]] = 0;
}
void Dfs(int x, int fa) {
  Sol(x);
  for (int i = 0; i < mp[x].size(); i++)
    if (mp[x][i] != fa) Dfs(mp[x][i], x);
}
int main() {
  ans = 1;
  memset(f, 0, sizeof(f));
  memset(flag, 0, sizeof(flag));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &t);
    for (int j = 0; j < t; j++) {
      scanf("%d", &x);
      v[i].push_back(x);
    }
  }
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &x, &y);
    mp[x].push_back(y);
    mp[y].push_back(x);
  }
  Dfs(1, 1);
  printf("%d\n", ans);
  for (int i = 1; i <= m; i++) printf("%d ", max(f[i], 1));
  printf("\n");
  return 0;
}
