#include <bits/stdc++.h>
using namespace std;
int const N = 3e5 + 41;
int n, m;
vector<int> e[N];
vector<int> ice[N];
int ans, col[N], ansi;
vector<int> ver[N];
int t[N * 4];
void upd(int v, int tl, int tr, int p, int add) {
  if (tl == tr) {
    t[v] += add;
  } else {
    int tm = (tl + tr) / 2;
    if (p <= tm)
      upd(v * 2 + 1, tl, tm, p, add);
    else
      upd(v * 2 + 2, tm + 1, tr, p, add);
    t[v] = min(t[v * 2 + 1], t[v * 2 + 2]);
  }
}
int getMin(int v, int tl, int tr) {
  if (tl == tr) {
    return tl;
  } else {
    int tm = (tl + tr) / 2;
    if (t[v * 2 + 1] == 0)
      return getMin(v * 2 + 1, tl, tm);
    else
      return getMin(v * 2 + 2, tm + 1, tr);
  }
}
void dfs(int u, int p) {
  for (int i = 0; i < (int)(ice[u].size()); ++i) {
    int ty = ice[u][i];
    if (col[ty] != -1) {
      upd(0, 0, ans - 1, col[ty], 1);
    }
  }
  for (int i = 0; i < (int)(ice[u].size()); ++i) {
    int ty = ice[u][i];
    if (col[ty] == -1) {
      col[ty] = getMin(0, 0, ans - 1);
      upd(0, 0, ans - 1, col[ty], 1);
    }
  }
  for (int i = 0; i < (int)(ice[u].size()); ++i) {
    int ty = ice[u][i];
    upd(0, 0, ans - 1, col[ty], -1);
  }
  for (int i = 0; i < (int)(e[u].size()); ++i) {
    int to = e[u][i];
    if (to == p) continue;
    dfs(to, u);
  }
}
void solve() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    int k;
    scanf("%d", &k);
    for (int j = 0; j < k; ++j) {
      int ti;
      scanf("%d", &ti);
      ice[i].push_back(ti);
      ver[ti].push_back(i);
    }
    ans = max(ans, (int)(ice[i].size()));
  }
  for (int i = 0; i < N; ++i) col[i] = -1;
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    --u;
    --v;
    e[u].push_back(v);
    e[v].push_back(u);
  }
  dfs(0, 0);
  ans = max(ans, 1);
  printf("%d\n", ans);
  for (int i = 1; i < m + 1; ++i) {
    col[i] = max(col[i], 0);
  }
  for (int i = 1; i < m + 1; ++i) {
    printf("%d ", col[i] + 1);
  }
}
int main() {
  solve();
  return 0;
}
