#include <bits/stdc++.h>
using namespace std;
vector<int> v[300005];
int res[300005];
vector<int> col[300005];
int n, m;
int cnt;
int tot[300005];
void dfs(int node, int par = -1) {
  int i, j, x;
  vector<int> temp;
  for (i = 0; i < tot[node]; i++) {
    if (res[col[node][i]]) {
      temp.push_back(res[col[node][i]]);
    }
  }
  if (temp.size() > 0) sort(temp.begin(), temp.end());
  x = 1;
  j = 0;
  for (i = 0; i < tot[node]; i++) {
    if (res[col[node][i]]) continue;
    while (j < temp.size() && temp[j] == x) {
      j = j + 1;
      x = x + 1;
    }
    res[col[node][i]] = x;
    cnt = max(cnt, x);
    x = x + 1;
  }
  for (i = 0; i < v[node].size(); i++) {
    if (v[node][i] != par) {
      dfs(v[node][i], node);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int i, x, y, j;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    cin >> tot[i];
    x = tot[i];
    for (j = 1; j <= x; j++) {
      cin >> y;
      col[i].push_back(y);
    }
  }
  for (i = 1; i < n; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dfs(1);
  if (!cnt) cnt = 1;
  cout << cnt << "\n";
  for (i = 1; i <= m; i++) {
    if (res[i] == 0) res[i] = 1;
    cout << res[i] << " ";
  }
  cout << "\n";
  return 0;
}
