#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:200000000")
using namespace std;
struct _ {
  _() {
    cout.precision(30);
    ios_base::sync_with_stdio(0);
  }
} __;
const int MOD = 1e9 + 7;
const int N = 3e5 + 10;
vector<int> colors[N];
vector<int> g[N];
int result[N], n, m;
void dfs(int v, int p) {
  sort(colors[v].begin(), colors[v].end(),
       [&](int a, int b) { return result[a] < result[b]; });
  int ix = colors[v].size();
  for (int i = 0; i < colors[v].size(); i++) {
    if (result[colors[v][i]] != 0) {
      ix = i;
      break;
    }
  }
  int start = 0;
  for (int cn = 1; start < colors[v].size() && result[colors[v][start]] == 0;
       cn++) {
    if (ix < colors[v].size() && result[colors[v][ix]] == cn) {
      ix++;
    } else {
      result[colors[v][start]] = cn;
      start++;
    }
  }
  for (int to : g[v]) {
    if (to == p) continue;
    dfs(to, v);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int cnt;
    cin >> cnt;
    colors[i].resize(cnt);
    for (int j = 0; j < cnt; j++) {
      cin >> colors[i][j];
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(0, -1);
  int max_used = 0;
  for (int i = 1; i <= m; i++) {
    if (result[i] == 0) result[i] = 1;
    max_used = max(max_used, result[i]);
  }
  cout << max_used << endl;
  for (int i = 1; i <= m; i++) {
    cout << result[i] << " ";
  }
  return 0;
}
