#include <bits/stdc++.h>
using namespace std;
const int M = 3e5 + 5;
int n, m, sum[M], p[M], ans, col[M], tot, fir[M];
set<int> s;
vector<int> g[M];
struct edge {
  int v, nex;
} e[M << 1];
void add(int u, int v) {
  e[++tot] = (edge){v, fir[u]};
  fir[u] = tot;
}
bool cmp(int a, int b) { return sum[a] > sum[b]; }
void fil(int x) {
  s.clear();
  for (int j = 0; j < sum[x]; j++) {
    if (col[g[x][j]]) s.insert(col[g[x][j]]);
  }
  int c = 1;
  for (int j = 0; j < sum[x]; j++) {
    if (col[g[x][j]]) continue;
    while (s.count(c)) c++;
    col[g[x][j]] = c++;
  }
}
void dfs(int u, int fa) {
  fil(u);
  for (int i = fir[u]; i; i = e[i].nex)
    if (e[i].v != fa) dfs(e[i].v, u);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> sum[i];
    p[i] = i;
    for (int j = 1; j <= sum[i]; j++) {
      int x;
      cin >> x;
      g[i].push_back(x);
    }
    ans = max(ans, sum[i]);
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }
  dfs(1, 0);
  cout << max(ans, 1) << endl;
  for (int i = 1; i <= m; i++) cout << max(col[i], 1) << ' ';
  cout << endl;
  return 0;
}
