#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
vector<int> c[N];
vector<int> g[N];
int col[N];
bool have[N];
void dfs(int u, int p) {
  int C = 1;
  for (int x : c[u])
    if (col[x]) have[col[x]] = 1;
  for (int x : c[u])
    if (!col[x]) {
      while (have[C]) ++C;
      have[col[x] = C] = 1;
    }
  for (int x : c[u]) have[col[x]] = 0;
  for (int v : g[u])
    if (v != p) dfs(v, u);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int m;
  cin >> m;
  int ans = 1;
  for (int i = 1; i <= n; ++i) {
    int s;
    cin >> s;
    c[i].resize(s);
    for (int& x : c[i]) cin >> x;
    if (ans < s) ans = s;
  }
  for (int i = 2; i <= n; ++i) {
    int u;
    cin >> u;
    int v;
    cin >> v;
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  dfs(1, 0);
  cout << ans << "\n";
  for (int i = 1; i <= m; ++i) cout << max(col[i], 1) << " ";
}
