#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int N = 300005;
vector<int> g[N];
vector<int> b[N];
int col[N];
bool vis[N];
void go(int x) {
  vis[x] = 1;
  set<int> c;
  for (auto &&y : b[x]) {
    c.insert(col[y]);
  }
  int res = 1;
  for (auto &&y : b[x]) {
    if (col[y]) {
      continue;
    }
    while (c.find(res) != c.end()) {
      res++;
    }
    col[y] = res;
    c.insert(res);
  }
  for (auto &&y : g[x]) {
    if (!vis[y]) {
      go(y);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    while (x-- > 0) {
      int y;
      cin >> y;
      b[i].emplace_back(y);
    }
    sort(b[i].begin(), b[i].end());
  }
  for (int i = 0; i <= n - 2; ++i) {
    int u, v;
    cin >> u >> v;
    if (int(b[u].size()) > int(b[v].size())) {
      swap(u, v);
    }
    for (auto &&x : b[u]) {
      auto it = lower_bound(b[v].begin(), b[v].end(), x);
      if (it != b[v].end() && (*it) == x) {
        g[u].emplace_back(v);
        g[v].emplace_back(u);
        break;
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      go(i);
    }
  }
  int ans = 0;
  for (int i = 1; i <= m; ++i) {
    ans = max(ans, col[i]);
  }
  ans = max(ans, 1);
  cout << ans << '\n';
  for (int i = 1; i <= m; ++i) {
    if (col[i] == 0) {
      col[i] = 1;
    }
    cout << col[i] << ' ';
  }
  cout << '\n';
  return 0;
}
