#include <bits/stdc++.h>
using namespace std;
vector<int> v[300005], s[300005];
int n, m, x, y, sta[300005], ans = 1, top, hav[300005], col[300005], now = 1;
void dfs(int o, int fa) {
  top = 0;
  for (int i = 0; i < (int)s[o].size(); ++i) {
    if (col[s[o][i]]) {
      hav[col[s[o][i]]]++;
    }
  }
  now = 1;
  for (int i = 0; i < (int)s[o].size(); ++i) {
    if (!col[s[o][i]]) {
      while (hav[now]) now++;
      col[s[o][i]] = now;
      hav[col[s[o][i]]]++;
    }
  }
  for (int i = 0; i < (int)s[o].size(); ++i) {
    hav[col[s[o][i]]]--;
  }
  for (int i = 0; i < (int)v[o].size(); ++i)
    if (v[o][i] != fa) dfs(v[o][i], o);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &x);
    for (int j = 1; j <= x; ++j) {
      scanf("%d", &y);
      s[i].push_back(y);
    }
    ans = max(ans, x);
  }
  for (int i = 2; i <= n; ++i) {
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dfs(1, 0);
  printf("%d\n", ans);
  for (int i = 1; i <= m; ++i) printf("%d ", col[i] == 0 ? 1 : col[i]);
  printf("\n");
}
