#include <bits/stdc++.h>
using namespace std;
long long inf = 2e18;
long long mod = 1e9 + 7;
const long long M = 5e5;
vector<long long> g[M];
vector<long long> s[M];
long long c[M];
long long cn = 1;
bool mark[M];
void dfs(long long x) {
  mark[x] = 1;
  set<long long> ciu;
  for (long long y : s[x]) {
    if (c[y] != 0) ciu.insert(c[y]);
  }
  long long unc = 0;
  auto piciu = ciu.begin();
  for (long long y : s[x]) {
    if (c[y] != 0) continue;
    unc++;
    while ((piciu != ciu.end()) && unc == *piciu) {
      unc++;
      piciu++;
    }
    c[y] = unc;
  }
  cn = max(cn, unc);
  for (long long y : g[x]) {
    if (mark[y]) continue;
    dfs(y);
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < n; i++) {
    long long siz = 0;
    cin >> siz;
    for (long long j = 0; j < siz; j++) {
      long long val;
      cin >> val;
      s[i].push_back(val);
    }
  }
  for (long long i = 1; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(0);
  cout << cn << '\n';
  for (long long i = 1; i <= m; i++) cout << c[i] + (c[i] == 0) << ' ';
}
