#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const long long linf = (long long)1e18;
const double eps = (double)1e-8;
const int mod = (int)1e9 + 7;
const int maxn = (int)3e5 + 5;
int t[maxn], t2[maxn], n, m, second[maxn];
vector<int> a[maxn], vc[maxn];
void dfs(int v, int pr) {
  for (int i = (0); i < (int)(vc[v].size()); ++i) {
    int col = vc[v][i];
    if (t[col]) {
      t2[t[col]] = v;
    }
  }
  int u = 1;
  for (int i = (0); i < (int)(vc[v].size()); ++i) {
    int col = vc[v][i];
    if (!t[col]) {
      while (t2[u] == v) {
        ++u;
      }
      t2[u] = v;
      t[col] = u;
    }
  }
  for (int i = (0); i < (int)(a[v].size()); ++i) {
    if (a[v][i] == pr) continue;
    dfs(a[v][i], v);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long ans = 1;
  cin >> n >> m;
  for (int i = (1); i <= (int)(n); ++i) {
    cin >> second[i];
    ans = max((long long)second[i], ans);
    for (int j = (1); j <= (int)(second[i]); ++j) {
      int x;
      cin >> x;
      vc[i].push_back(x);
    }
  }
  for (int i = (1); i <= (int)(n - 1); ++i) {
    int u, v;
    cin >> u >> v;
    a[v].push_back(u);
    a[u].push_back(v);
  }
  memset((t), 0, sizeof(t));
  memset((t2), 0, sizeof(t2));
  dfs(1, 0);
  cout << ans << "\n";
  for (int i = (1); i <= (int)(m); ++i) {
    cout << max(t[i], 1) << " ";
  }
  cout << "\n";
  return 0;
}
