#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void R(T &xx) {
  xx = 0;
  char ch = getchar();
  bool F = 0;
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') F = 1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    xx = xx + xx + (xx << 3) + ch - 48, ch = getchar();
  if (F) xx = -xx;
}
const int N = 3600 * 24;
int TT[110011], n, m, t;
pair<int, int> a[110011];
int bel[110011];
int main() {
  R(n), R(m), R(t);
  for (int i = 0; i < (int)(n); i++) {
    int x, y, z;
    R(x), R(y), R(z);
    a[i].first = x * 3600 + y * 60 + z;
    a[i].second = a[i].first + t;
    TT[a[i].first]++, TT[a[i].second]--;
  }
  bool F = 0;
  int tot = 0;
  for (int i = 0; i < (int)(N); i++) {
    tot += TT[i];
    if (tot >= m) F = 1;
  }
  if (!F) return puts("No solution"), 0;
  sort(a, a + n);
  memset(TT, 0, sizeof(TT));
  for (int i = 0; i < (int)(n); i++) TT[a[i].second]--;
  int tt = 0, nw = 0, pre = 0, idx = 0;
  for (int i = 0; i < (int)(N); i++) {
    tt += TT[i];
    for (; nw < n && a[nw].first == i; bel[pre = (nw++)] = idx) {
      if (tt == m)
        TT[a[pre].second]++;
      else
        tt++, idx++;
    }
  }
  printf("%d\n", idx);
  for (int i = 0; i < (int)(n); i++) printf("%d\n", bel[i]);
  puts("");
  return 0;
}
