#include <bits/stdc++.h>
using namespace std;
vector<int> graf[300000];
vector<int> type[300000];
int col[300000];
void bejar(int start, int apa) {
  set<int> volt;
  for (int d : type[start]) {
    if (col[d] != 0) {
      volt.insert(col[d]);
    }
  }
  int it = 1;
  for (int d : type[start]) {
    if (col[d] == 0) {
      while (volt.count(it) > 0) {
        it++;
      }
      col[d] = it;
      it++;
    }
  }
  for (int s : graf[start]) {
    if (s != apa) {
      bejar(s, start);
    }
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  int h_max = 0;
  for (int i = 0; i < n; i++) {
    int s;
    cin >> s;
    for (int j = 0; j < s; j++) {
      int z;
      cin >> z;
      z--;
      type[i].push_back(z);
    }
    if (type[h_max].size() < type[i].size()) {
      h_max = i;
    }
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    graf[u].push_back(v);
    graf[v].push_back(u);
  }
  bejar(h_max, h_max);
  if (type[h_max].size() == 0) {
    cout << 1 << endl;
  } else {
    cout << type[h_max].size() << endl;
  }
  for (int i = 0; i < m; i++) {
    cout << max(1, col[i]) << ' ';
  }
  return 0;
}
