#include <bits/stdc++.h>
using namespace std;
long long n, m;
unordered_set<long long> v[300010];
vector<long long> g[300010];
long long col[300010];
void dfs(long long x, long long p) {
  unordered_set<long long> used;
  for (auto t : v[x]) {
    if (~col[t]) used.insert(col[t]);
  }
  long long r = 1;
  for (auto t : v[x]) {
    if (~col[t]) continue;
    for (; used.find(r) != used.end(); r++) {
    }
    col[t] = r;
    used.insert(r);
  }
  for (auto y : g[x])
    if (y != p) dfs(y, x);
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    long long k;
    scanf("%lld", &k);
    for (int j = 0; j < k; j++) {
      long long r;
      scanf("%lld", &r);
      v[i].insert(r);
    }
  }
  for (int i = 0; i < n - 1; i++) {
    long long a, b;
    scanf("%lld%lld", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (int i = 1; i <= m; i++) col[i] = -1;
  dfs(1, 0);
  long long ma = 1;
  for (int i = 1; i <= m; i++) ma = max(ma, col[i]);
  cout << ma << endl;
  for (int i = 1; i <= m; i++) {
    if (i > 1) printf(" ");
    if (col[i] == -1) col[i] = 1;
    printf("%lld", col[i]);
  }
  cout << endl;
  return 0;
}
