#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
vector<int> ds[N], vx[N];
int sz[N], pa[N], mau[N];
bool mark[N];
void dfs(int u) {
  for (int i = 1; i <= sz[u]; i++) mark[i] = false;
  for (auto &v : vx[u]) {
    mark[mau[v]] = true;
  }
  int i = 1;
  for (auto &v : vx[u]) {
    if (mau[v] > 0) continue;
    while (mark[i]) i++;
    mau[v] = i;
    mark[i] = true;
  }
  for (auto &v : ds[u]) {
    if (v == pa[u]) continue;
    pa[v] = u;
    dfs(v);
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int ans = 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", sz + i);
    ans = max(ans, sz[i]);
    for (int j = 0; j < sz[i]; j++) {
      int a;
      scanf("%d", &a);
      vx[i].push_back(a);
    }
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    ds[u].push_back(v);
    ds[v].push_back(u);
  }
  dfs(1);
  printf("%d\n", ans);
  for (int i = 1; i <= m; i++) printf("%d ", max(mau[i], 1));
}
