#include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
const int N = 3e5 + 5;
int n, m;
int col[N], s[N], colors;
bool vis[N];
vector<vector<int> > g(N), c(N);
void dfs(int x) {
  if (vis[x]) return;
  vis[x] = 1;
  set<int> s;
  int sz = c[x].size();
  for (int i = 0; i < sz; i++) {
    if (col[c[x][i]] != 0) s.insert(col[c[x][i]]);
  }
  int y = 1;
  for (int i = 0; i < sz; i++) {
    if (col[c[x][i]] == 0) {
      while (s.count(y)) y++;
      col[c[x][i]] = y++;
    }
  }
  sz = g[x].size();
  for (int i = 0; i < sz; i++) {
    if (!vis[g[x][i]]) dfs(g[x][i]);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  ;
  cin >> n >> m;
  int maxid = 0;
  colors = 1;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    if (s[i] > colors) {
      colors = s[i];
      maxid = i + 1;
    }
    for (int j = 0; j < s[i]; j++) {
      int x;
      cin >> x;
      c[i + 1].push_back(x);
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  cout << colors << endl;
  if (maxid) dfs(maxid);
  for (int i = 0; i < m; i++) {
    if (col[i + 1] == 0) col[i + 1] = 1;
  }
  for (int i = 0; i < m; i++) {
    cout << col[i + 1] << " ";
  }
  cout << endl;
}
