#include <bits/stdc++.h>
using namespace std;
vector<int> tree[300001];
vector<int> color[300001];
vector<int> present[300001];
vector<int> val(300001, -1);
vector<bool> vis(300001, 0);
long long mod = 1000000007;
void dfs(int x) {
  int mx = 1;
  int lim = present[x].size();
  for (int i = 0; i < color[x].size(); i++) {
    int j = color[x][i];
    if (val[j] == -1) continue;
    if (val[j] >= lim) continue;
    present[x][val[j]] = 1;
  }
  for (int i = 1; i < lim; i++) {
    if (!present[x][i]) {
      mx = i;
      break;
    }
  }
  for (int i = 0; i < color[x].size(); i++) {
    int j = color[x][i];
    if (val[j] == -1) {
      val[j] = mx;
      present[x][mx] = 1;
      while (mx < lim && present[x][mx]) mx++;
    }
  }
  for (int i = 0; i < tree[x].size(); i++) {
    int j = tree[x][i];
    if (vis[j]) continue;
    vis[j] = 1;
    dfs(j);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int s;
    cin >> s;
    for (int j = 0; j < s; j++) {
      int c;
      cin >> c;
      color[i].push_back(c);
      present[i].push_back(0);
    }
    present[i].push_back(0);
  }
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    tree[x].push_back(y);
    tree[y].push_back(x);
  }
  vis[1] = 1;
  dfs(1);
  int ans = 0;
  for (int i = 1; i <= m; i++) {
    if (val[i] == -1) val[i] = 1;
    ans = max(ans, val[i]);
  }
  cout << ans << endl;
  for (int i = 1; i <= m; i++) {
    cout << val[i] << " ";
  }
  cout << endl;
}
