#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long _INF = 0xc0c0c0c0c0c0c0c0;
const long long mod = (int)1e9 + 7;
const int N = 3e5 + 100;
vector<int> vc[N], e[N];
int col[N];
int vis[N];
int mx = 0;
void dfs(int o, int u) {
  for (int v : vc[u]) {
    vis[col[v]]++;
  }
  int b = 1;
  for (int v : vc[u]) {
    if (col[v]) continue;
    while (vis[b]) ++b;
    vis[b] = 1;
    col[v] = b;
  }
  mx = max(mx, b);
  for (int v : vc[u]) {
    vis[col[v]] = 0;
  }
  for (int v : e[u]) {
    if (v == o) continue;
    dfs(u, v);
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    int si, tv;
    scanf("%d", &si);
    for (int j = 1; j <= si; ++j) {
      scanf("%d", &tv);
      vc[i].push_back(tv);
    }
  }
  int u, v;
  for (int i = 1; i < n; ++i) {
    scanf("%d%d", &u, &v);
    e[u].push_back(v);
    e[v].push_back(u);
  }
  dfs(0, 1);
  printf("%d\n", mx);
  for (int i = 1; i <= m; ++i) {
    if (!col[i]) col[i] = 1;
    printf("%d%c", col[i], " \n"[i == m]);
  }
  return 0;
}
