#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
template <typename T1, typename T2>
inline void chkmin(T1 &x, T2 y) {
  if (y < x) x = y;
};
template <typename T1, typename T2>
inline void chkmax(T1 &x, T2 y) {
  if (y > x) x = y;
};
int gcd(int x, int y) { return (y == 0 ? x : gcd(y, x % y)); }
const int MAXN = 5e5 + 100;
vector<int> g[MAXN];
set<int> second[MAXN];
int res, c[MAXN], n, m;
void dfs(int ss, int p) {
  vector<int> v;
  if (p == -1) {
    for (auto it : second[ss]) c[it] = ++res;
  } else {
    for (auto it : second[ss])
      if (c[it] != 0) v.push_back(c[it]);
    sort(v.begin(), v.end());
    int now = 1, itr = 0;
    for (auto it : second[ss]) {
      if (c[it] == 0) {
        while (itr < (int)v.size()) {
          if (now == v[itr]) {
            itr++;
            now++;
          } else
            break;
        }
        c[it] = now;
        now++;
      }
    }
    res = max(res, now - 1);
  }
  for (auto it : g[ss]) {
    if (it == p) continue;
    dfs(it, ss);
  }
}
void solve() {
  cin >> n >> m;
  for (int i = 1; i < n + 1; i++) {
    int x;
    cin >> x;
    for (int j = 0; j < x; j++) {
      int c;
      cin >> c;
      second[i].insert(c);
    }
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1, -1);
  if (res == 0) res = 1;
  cout << res << endl;
  for (int i = 1; i < m + 1; i++) {
    if (c[i] == 0) c[i] = 1;
    cout << c[i] << " ";
  }
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
