#include <bits/stdc++.h>
template <typename T>
class IntegerIterator
    : public std::iterator<std::input_iterator_tag, T, std::ptrdiff_t, T*, T> {
 public:
  explicit IntegerIterator(T value) : value(value) {}
  IntegerIterator& operator++() {
    ++value;
    return *this;
  }
  IntegerIterator operator++(int) {
    IntegerIterator copy = *this;
    ++value;
    return copy;
  }
  IntegerIterator& operator--() {
    --value;
    return *this;
  }
  IntegerIterator operator--(int) {
    IntegerIterator copy = *this;
    --value;
    return copy;
  }
  T operator*() const { return value; }
  bool operator==(IntegerIterator rhs) const { return value == rhs.value; }
  bool operator!=(IntegerIterator rhs) const { return !(*this == rhs); }

 private:
  T value;
};
template <typename T>
class IntegerRange {
 public:
  IntegerRange(T begin, T end) : begin_(begin), end_(end) { ; }
  IntegerIterator<T> begin() const { return IntegerIterator<T>(begin_); }
  IntegerIterator<T> end() const { return IntegerIterator<T>(end_); }

 private:
  T begin_;
  T end_;
};
template <typename T>
class ReversedIntegerRange {
 public:
  ReversedIntegerRange(T begin, T end) : begin_(begin), end_(end) { ; }
  std::reverse_iterator<IntegerIterator<T>> begin() const {
    return std::reverse_iterator<IntegerIterator<T>>(
        IntegerIterator<T>(begin_));
  }
  std::reverse_iterator<IntegerIterator<T>> end() const {
    return std::reverse_iterator<IntegerIterator<T>>(IntegerIterator<T>(end_));
  }

 private:
  T begin_;
  T end_;
};
template <typename T>
IntegerRange<T> range(T to) {
  return IntegerRange<T>(0, to);
}
template <typename T>
IntegerRange<T> range(T from, T to) {
  return IntegerRange<T>(from, to);
}
template <typename T>
IntegerRange<T> inclusiveRange(T to) {
  return IntegerRange<T>(0, to + 1);
}
template <typename T>
IntegerRange<T> inclusiveRange(T from, T to) {
  return IntegerRange<T>(from, to + 1);
}
template <typename T>
ReversedIntegerRange<T> downrange(T from) {
  return ReversedIntegerRange<T>(from, 0);
}
template <typename T>
ReversedIntegerRange<T> downrange(T from, T to) {
  return ReversedIntegerRange<T>(from, to);
}
template <typename T>
ReversedIntegerRange<T> inclusiveDownrange(T from) {
  return ReversedIntegerRange<T>(from + 1, 0);
}
template <typename T>
ReversedIntegerRange<T> inclusiveDownrange(T from, T to) {
  return ReversedIntegerRange<T>(from + 1, to);
}
template <typename T>
void updateMin(T& oldValue, const T& newValue) {
  if (newValue < oldValue) {
    oldValue = newValue;
  }
}
template <typename T>
void updateMax(T& oldValue, const T& newValue) {
  if (oldValue < newValue) {
    oldValue = newValue;
  }
}
using namespace std;
class TaskC {
 public:
  vector<vector<int>> g;
  vector<vector<int>> colors;
  vector<int> anses;
  set<int> can_use;
  void dfs(int v, int p) {
    for (int c : colors[v]) {
      if (anses[c] != -1) {
        can_use.erase(anses[c]);
      }
    }
    for (int c : colors[v]) {
      if (anses[c] == -1) {
        anses[c] = *can_use.begin();
        can_use.erase(can_use.begin());
      }
    }
    for (int c : colors[v]) {
      can_use.insert(anses[c]);
    }
    for (int to : g[v]) {
      if (p == to) {
        continue;
      }
      dfs(to, v);
    }
  }
  void solve(std::istream& in, std::ostream& out) {
    int n, m;
    in >> n >> m;
    int ans = 1;
    g.resize(n);
    colors.resize(n);
    for (int i : range(n)) {
      int c;
      in >> c;
      updateMax(ans, c);
      colors[i].resize(c);
      for (int j : range(c)) {
        in >> colors[i][j];
        --colors[i][j];
      }
    }
    for (int i : range(n - 1)) {
      int a, b;
      in >> a >> b;
      --a, --b;
      g[a].push_back(b);
      g[b].push_back(a);
    }
    out << ans << "\n";
    anses.resize(m, -1);
    for (int i : range(ans)) {
      can_use.insert(i);
    }
    dfs(0, 0);
    for (int i : range(m)) {
      out << max(0, anses[i]) + 1 << " ";
    }
  }
};
int main() {
  std::ios_base::sync_with_stdio(false);
  TaskC solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  in.tie(0);
  out << std::fixed;
  out.precision(20);
  solver.solve(in, out);
  return 0;
}
