#include <bits/stdc++.h>
using namespace std;
int n, m, op, x, u, v, a[300010];
vector<int> ice[300010];
vector<int> G[300010];
bool vis[300010];
void dfs(int u, int fa) {
  vector<int> t;
  for (int i = 0; i < ice[u].size(); i++) {
    int v = ice[u][i];
    if (a[v]) {
      t.push_back(v);
      vis[a[v]] = 1;
    }
  }
  int res = 1;
  for (int i = 0; i < ice[u].size(); i++) {
    int v = ice[u][i];
    if (a[v]) {
      continue;
    }
    while (vis[res]) {
      res++;
    }
    a[v] = res++;
  }
  for (int i = 0; i < t.size(); i++) {
    vis[a[t[i]]] = 0;
  }
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (v != fa) {
      dfs(v, u);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &op);
    for (int j = 1; j <= op; j++) {
      scanf("%d", &x);
      ice[i].push_back(x);
    }
  }
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(1, 0);
  int res = 0;
  for (int i = 1; i <= m; i++) {
    if (!a[i]) a[i] = 1;
    res = max(res, a[i]);
  }
  cout << res;
  puts("");
  for (int i = 1; i <= m; i++) {
    cout << a[i] << " ";
  }
  puts("");
  return 0;
}
