#include <bits/stdc++.h>
using namespace std;
const long long INF = 2e9 + 10;
const long long MOD = 1e9 + 7;
const long long P = 29;
const long double EPS = 1e-9;
const int N = 3e5 + 10;
const int M = 1e3 + 10;
vector<int> col[N], g[N];
int color[N];
bool used[N];
void dfs(int v) {
  used[v] = true;
  set<int> usd;
  for (int i = 0; i < col[v].size(); i++) usd.insert(color[col[v][i]]);
  for (auto t : g[v]) {
    if (used[t]) continue;
    int cur = 1;
    set<int> us1;
    for (int i = 0; i < col[t].size(); i++)
      if (usd.count(color[col[t][i]])) us1.insert(color[col[t][i]]);
    for (int i = 0; i < col[t].size(); i++) {
      if (color[col[t][i]]) continue;
      while (us1.count(cur)) cur++;
      color[col[t][i]] = cur++;
    }
    dfs(t);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int cnt;
    cin >> cnt;
    for (int j = 0; j < cnt; j++) {
      int x;
      cin >> x;
      col[i].push_back(x);
    }
    sort(col[i].begin(), col[i].end());
  }
  for (int i = 0; i < n - 1; i++) {
    int v, u;
    cin >> v >> u;
    g[--v].push_back(--u);
    g[u].push_back(v);
  }
  for (int i = 0; i < col[0].size(); i++) color[col[0][i]] = i + 1;
  dfs(0);
  cout << max(1, *max_element(color, color + m + 1)) << endl;
  for (int i = 1; i < m + 1; i++) cout << max(1, color[i]) << " ";
}
