#include <bits/stdc++.h>
using namespace std;
const int NMAX = 300000;
vector<int> G[NMAX + 5];
vector<int> C[NMAX + 5];
int N, M, ans[NMAX + 5], max_col = 1;
bool viz[NMAX + 2];
void DFS(int nod) {
  viz[nod] = 1;
  vector<pair<int, int> > vv;
  vector<int> nep, v;
  map<int, int> MP;
  for (auto col : C[nod]) {
    if (ans[col]) {
      v.push_back(ans[col]);
      MP[ans[col]] = 1;
    } else
      nep.push_back(col);
  }
  sort(v.begin(), v.end());
  int ind = 0, fre = 1;
  for (auto x : nep) {
    while (MP[fre]) {
      ++fre;
    }
    ans[x] = fre;
    ++fre;
  }
  for (auto x : G[nod]) {
    if (!viz[x]) {
      DFS(x);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> M;
  for (int i = 1; i <= N; ++i) {
    int nr, x;
    cin >> nr;
    for (int j = 1; j <= nr; ++j) {
      cin >> x;
      C[i].push_back(x);
    }
  }
  for (int i = 1; i < N; ++i) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  DFS(1);
  for (int i = 1; i <= M; ++i) {
    if (ans[i] < 1) ans[i] = 1;
    max_col = max(max_col, ans[i]);
  }
  cout << max_col << '\n';
  for (int i = 1; i <= M; ++i) cout << ans[i] << ' ';
  return 0;
}
