#include <bits/stdc++.h>
using namespace std;
struct __isoff {
  __isoff() {
    if (0) freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
    srand('C' + 'T' + 'A' + 'C' + 'Y' + 'M' + 'B' + 'A');
  }
  ~__isoff() {}
} __osafwf;
const unsigned long long p1 = 131;
const unsigned long long p2 = 129;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int infi = 1e9 + 7;
const long long inf = 1e18 + 7;
const long long dd = 5e5 + 7;
int n, m;
vector<int> P[dd];
vector<int> S[dd];
vector<int> color;
struct super {
  set<int> G;
  vector<int> T;
  void build(int n) {
    for (int i = 0; i < n; i++) {
      G.insert(i);
    }
  }
  int get() { return *G.begin(); }
  void del(int x) {
    G.erase(x);
    T.push_back(x);
  }
  void reload() {
    for (auto x : T) {
      G.insert(x);
    }
    T.clear();
  }
} G;
void Paint(int v) {
  G.reload();
  for (auto x : S[v]) {
    if (color[x] >= 0) {
      G.del(color[x]);
    }
  }
  for (auto x : S[v]) {
    if (color[x] < 0) {
      color[x] = G.get();
      G.del(color[x]);
    }
  }
}
void dfs(int v, int p) {
  G.reload();
  for (auto x : S[v]) {
    if (color[x] >= 0) {
      G.del(color[x]);
    }
  }
  for (auto x : S[v]) {
    if (color[x] < 0) {
      color[x] = G.get();
      G.del(color[x]);
    }
  }
  for (auto u : P[v]) {
    if (u != p) {
      dfs(u, v);
    }
  }
}
int main() {
  cin >> n >> m;
  G.build(n + m + 100);
  color.resize(m, -1);
  for (int i = 0; i < n; i++) {
    int s;
    scanf("%d", &s);
    for (int j = 0; j < s; j++) {
      int t;
      scanf("%d", &t);
      S[i].push_back(t - 1);
    }
  }
  vector<pair<int, int> > F;
  for (int i = 0; i < n; i++) {
    F.push_back({(int)S[i].size(), i});
  }
  sort(F.begin(), F.end());
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    P[a].push_back(b);
    P[b].push_back(a);
  }
  dfs(F.back().second, -1);
  cout << max(1, *max_element(color.begin(), color.end()) + 1) << '\n';
  for (int i = 0; i < m; i++) {
    printf("%d ", max(color[i], 0) + 1);
  }
}
