#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int> > g, s;
int c = 1;
vector<int> col;
void do_it(int u) {
  int cnt = s[u].size();
  c = max(c, cnt);
  vector<bool> used(c + 1, false);
  for (int i = 0; i < cnt; ++i) {
    if (col[s[u][i]] > 0) {
      used[col[s[u][i]]] = true;
    }
  }
  int k = 1;
  for (int i = 0; i < cnt; ++i) {
    if (col[s[u][i]] <= 0) {
      while (used[k]) k++;
      col[s[u][i]] = k;
      used[k] = true;
    }
  }
  return;
}
int main() {
  scanf("%d %d", &n, &m);
  g.resize(n);
  col.assign(m, -1);
  s.resize(n);
  for (int i = 0; i < n; ++i) {
    int k;
    scanf("%d", &k);
    s[i].resize(k);
    for (int j = 0; j < k; ++j) {
      scanf("%d", &s[i][j]);
      s[i][j]--;
    }
  }
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  stack<int> q;
  vector<bool> used(n, false);
  used[0] = true;
  q.push(0);
  while (!q.empty()) {
    int u = q.top();
    q.pop();
    do_it(u);
    for (int i = 0; i < (int)g[u].size(); ++i) {
      int v = g[u][i];
      if (!used[v]) {
        used[v] = true;
        q.push(v);
      }
    }
  }
  for (int i = 0; i < m; ++i) {
    if (col[i] < 0) col[i] = 1;
  }
  cout << c << endl;
  cout << col[0];
  for (int i = 1; i < m; ++i) cout << ' ' << col[i];
}
