#include <bits/stdc++.h>
using namespace std;
const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, -1, 1};
const double eps = 1e-9;
const int INF = 0x7FFFFFFF;
const long long INFLL = 0x7FFFFFFFFFFFFFFFLL;
const double pi = acos(-1);
template <class T>
T take(queue<T> &O) {
  T tmp = O.front();
  O.pop();
  return tmp;
}
template <class T>
T take(stack<T> &O) {
  T tmp = O.top();
  O.pop();
  return tmp;
}
template <class T>
T take(priority_queue<T> &O) {
  T tmp = O.top();
  O.pop();
  return tmp;
}
template <class T>
inline void getint(T &num) {
  bool neg = 0;
  num = 0;
  char c;
  while ((c = getchar_unlocked()) && (!isdigit(c) && c != '-'))
    ;
  if (c == '-') {
    neg = 1;
    c = getchar_unlocked();
  }
  do num = num * 10 + c - '0';
  while ((c = getchar_unlocked()) && isdigit(c));
  num *= (neg) ? -1 : 1;
}
template <class T>
inline bool inRange(T z, T a, T b) {
  return a <= z && z <= b;
}
void OPEN(string in = "input.txt", string out = "output.txt") {
  freopen(in.c_str(), "r", stdin);
  freopen(out.c_str(), "w", stdout);
  return;
}
int n, m, T;
vector<pair<pair<int, int>, int> > seg;
int idseg[20005];
bitset<20005> ada;
set<pair<int, int> > SET;
set<pair<int, int> > SETQ;
int main(int argc, const char *argv[]) {
  getint(n);
  getint(m);
  getint(T);
  for (int(i) = (0), _t = (n); i < (_t); ++(i)) {
    int h, m, s;
    getint(h);
    getint(m);
    getint(s);
    s = h * 3600 + m * 60 + s;
    seg.push_back(make_pair(make_pair(s, s + T - 1), i));
  }
  sort(seg.begin(), seg.end());
  ada.reset();
  int cnt = 0, bef = -1;
  int idcnt = 0;
  bool ok = 0;
  for (int(i) = (0), _t = ((int)seg.size()); i < (_t); ++(i)) {
    pair<pair<int, int>, int> &e = seg[i];
    while (!SETQ.empty() && (*SETQ.begin()).first <= e.first.first) {
      pair<int, int> now = *SETQ.begin();
      SETQ.erase(SETQ.begin());
      ada.set(now.second, 0);
      --cnt;
      SET.erase(make_pair(seg[now.second].first.first, now.second));
    }
    ada.set(e.second, 1);
    ++cnt;
    if (cnt > m) {
      idseg[e.second] = idseg[bef];
      SETQ.erase(make_pair(seg[bef].first.second + 1, seg[bef].second));
      seg[bef].first.second = e.first.second;
      SETQ.insert(make_pair(e.first.second + 1, seg[bef].second));
      ada.set(e.second, 0);
      --cnt;
    } else {
      idseg[e.second] = idcnt++;
      SETQ.insert(make_pair(e.first.second + 1, e.second));
      bef = i;
    }
    if (cnt == m) ok = 1;
  }
  if (!ok) {
    cout << "No solution\n";
    exit(0);
  }
  cout << idcnt << "\n";
  for (int(i) = (0), _t = (n); i < (_t); ++(i)) cout << idseg[i] + 1 << "\n";
  return 0;
}
