#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
long long int n, m, ans, col;
vector<long long int> v[N], par(N), cola(N);
set<long long int> se[N];
void dfs(long long int s, long long int p) {
  par[s] = p;
  col = 1;
  set<long long int> no, used;
  for (auto ic : se[s]) {
    if (se[p].find(ic) == se[p].end()) {
      no.insert(ic);
    } else {
      used.insert(cola[ic]);
    }
  }
  for (auto x : no) {
    while (used.find(col) != used.end()) col++;
    cola[x] = col;
    used.insert(col);
    ans = max(ans, col);
  }
  for (auto u : v[s])
    if (u != p) {
      dfs(u, s);
    }
}
int main() {
  cin >> n >> m;
  for (long long int i = (long long int)1; i <= (long long int)n; i++) {
    long long int sz;
    cin >> sz;
    for (long long int j = (long long int)0; j <= (long long int)sz - 1; j++) {
      long long int x;
      cin >> x;
      se[i].insert(x);
    }
  }
  for (long long int i = (long long int)1; i <= (long long int)n - 1; i++) {
    long long int x, y, z;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dfs(1, 0);
  ans = max(ans, 1LL);
  cout << ans << "\n";
  for (long long int i = (long long int)1; i <= (long long int)m; i++)
    cout << max(1LL, cola[i]) << " ";
}
