#include <bits/stdc++.h>
using namespace std;
long long getint() {
  long long _x = 0, _tmp = 1;
  char _tc = getchar();
  while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
  if (_tc == '-') _tc = getchar(), _tmp = -1;
  while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
  return _x * _tmp;
}
long long mypow(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 1ll;
  long long _tmp = mypow(_a, _x / 2, _mod);
  _tmp = (_tmp * _tmp) % _mod;
  if (_x & 1) _tmp = (_tmp * _a) % _mod;
  return _tmp;
}
bool equal(double _x, double _y) { return _x > _y - 1e-7 && _x < _y + 1e-7; }
int __ = 1, cs;
int n, m, t;
vector<int> v;
deque<pair<int, int> > d;
int idx, ans[20010];
bool got;
void build() {}
void init() {
  n = getint();
  m = getint();
  t = getint();
  for (int i = 0; i < n; i++) {
    int th, tm, ts;
    th = getint();
    tm = getint();
    ts = getint();
    v.push_back(th * 3600 + tm * 60 + ts);
  }
}
void solve() {
  for (int i = 0; i < n; i++) {
    while (d.size() && d.front().first <= v[i] - t) d.pop_front();
    if ((int)d.size() == m) {
      ans[i] = d.back().second;
      d.pop_back();
    } else
      ans[i] = ++idx;
    d.push_back(make_pair(v[i], ans[i]));
    if ((int)d.size() == m) got = true;
  }
  if (!got) {
    puts("No solution");
  } else {
    printf("%d\n", idx);
    for (int i = 0; i < n; i++) printf("%d\n", ans[i]);
  }
}
int main() {
  build();
  while (__--) {
    init();
    solve();
  }
}
