#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> c[300010];
set<int> s[300010];
vector<int> e[300010];
int col[300010];
void dfs(int p, int pre) {
  if (pre == -1) {
    int t = 1;
    for (int i = 0; i < c[p].size(); i++) {
      col[c[p][i]] = t++;
    }
  } else {
    vector<int> v;
    for (int i = 0; i < c[p].size(); i++) {
      if (col[c[p][i]] != 0) {
        v.push_back(col[c[p][i]]);
      }
    }
    sort(v.begin(), v.end());
    int t = 1;
    int j = 0;
    while (j < v.size() && t == v[j]) {
      j++;
      t++;
    }
    for (int i = 0; i < c[p].size(); i++) {
      if (col[c[p][i]] == 0) {
        col[c[p][i]] = t++;
      }
      while (j < v.size() && t == v[j]) {
        j++;
        t++;
      }
    }
  }
  for (int i = 0; i < e[p].size(); i++)
    if (e[p][i] != pre) {
      dfs(e[p][i], p);
    }
}
int main() {
  cin >> n >> m;
  int res = 1;
  for (int i = 0; i < n; i++) {
    int k;
    scanf("%d", &k);
    res = max(res, k);
    for (int j = 0; j < k; j++) {
      int tmp;
      scanf("%d", &tmp);
      c[i].push_back(tmp - 1);
      s[i].insert(tmp - 1);
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    e[u].push_back(v);
    e[v].push_back(u);
  }
  cout << res << endl;
  dfs(0, -1);
  for (int i = 0; i < m; i++)
    if (col[i] == 0) col[i] = 1;
  for (int i = 0; i < m; i++) {
    cout << col[i] << " ";
  }
  return 0;
}
