#include <bits/stdc++.h>
using namespace std;
int n, M;
int cont = 0;
int a[5111111];
char s1[5111111];
int mp[11] = {11, 24 * 60 * 60, 60 * 60, 60};
int moth[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main() {
  int y, mo, d, h, m, s;
  scanf("%d%d", &n, &M);
  while (scanf("%d-%d-%d %d:%d:%d", &y, &mo, &d, &h, &m, &s) != EOF) {
    gets(s1);
    ++cont;
    for (int i = 1; i < mo; i++) {
      a[cont] += moth[i] * 24 * 60 * 60;
    }
    a[cont] += d * 24 * 60 * 60 + h * 60 * 60 + m * 60 + s;
  }
  int i;
  sort(a + 1, a + 1 + cont);
  int f = 1;
  for (i = 1; i <= cont; i++) {
    int t = a[i] - n + 1;
    int ans = lower_bound(a + 1, a + 1 + cont, t) - (a + 1);
    if (i - ans >= M) {
      f = 0;
      for (int j = 1; j <= 12; j++) {
        if (a[i] >= moth[j] * 24 * 60 * 60)
          a[i] -= (moth[j] * 24 * 60 * 60);
        else {
          mo = j;
          break;
        }
      }
      d = a[i] / mp[1];
      h = a[i] % mp[1] / mp[2];
      m = a[i] % mp[2] / mp[3];
      s = a[i] % mp[3];
      printf("2012");
      if (mo <= 9)
        printf("-0%d", mo);
      else
        printf("-%d", mo);
      if (d <= 9)
        printf("-0%d", d);
      else
        printf("-%d", d);
      if (h <= 9)
        printf(" 0%d", h);
      else
        printf(" %d", h);
      if (m <= 9)
        printf(":0%d", m);
      else
        printf(":%d", m);
      if (s <= 9)
        printf(":0%d", s);
      else
        printf(":%d", s);
      printf("\n");
      break;
    }
  }
  if (f) printf("-1\n");
}
