#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e14;
const long long TE3 = 1005;
const long long TE5 = 300005;
bool PRINT_TIME_OF_EXEC = false;
vector<long long> u[TE5], v[TE5];
vector<long long> c(TE5, 0);
long long ans = 0;
long long vis[TE5];
void dfs(long long x) {
  vis[x] = 1;
  set<long long> ss;
  for (long long i = (0); i < (u[x].size()); ++i) {
    if (c[u[x][i]]) ss.insert(c[u[x][i]]);
  }
  long long col = 1;
  for (long long i = (0); i < (u[x].size()); ++i) {
    if (!c[u[x][i]]) {
      while (ss.find(col) != ss.end()) col++;
      c[u[x][i]] = col;
      ans = max(col, ans);
      col++;
    }
  }
  ss.clear();
  for (long long i = (0); i < (v[x].size()); ++i) {
    long long y = v[x][i];
    if (!vis[y]) dfs(y);
  }
}
int main() {
  ios::sync_with_stdio(false);
  long long start_time = clock();
  long long n, m;
  cin >> n >> m;
  for (long long i = (0); i < (n); ++i) {
    long long x;
    cin >> x;
    for (long long j = (0); j < (x); ++j) {
      long long y;
      cin >> y;
      y--;
      u[i].push_back(y);
    }
  }
  for (long long i = (0); i < (n - 1); ++i) {
    long long a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  dfs(0);
  cout << max(ans, (long long)1) << endl;
  for (long long i = (0); i < (m); ++i) cout << max(c[i], (long long)1) << " ";
  cout << endl;
  long long end_time = clock();
  if (PRINT_TIME_OF_EXEC)
    cout << "Time of exec. " << ((double)end_time - start_time) / CLOCKS_PER_SEC
         << endl;
  return 0;
}
