#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const double PI = acos(-1.0);
const int INF = 1000000000;
const int MOD = 1000000007;
const int M = INF;
int mo[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
unsigned long long convert(string& s) {
  for (int i = 0; i < s.size(); i++)
    if (s[i] == '-' || s[i] == ':') s[i] = ' ';
  stringstream ss(s);
  unsigned long long year, m, d, hh, mm, dd;
  ss >> year >> m >> d >> hh >> mm >> dd;
  unsigned long long ans = 0;
  for (int i = 0; i + 1 < m; i++) ans += mo[i];
  ans = (((ans + d) * 24 + hh) * 60 + mm) * 60 + dd;
  return ans;
}
int main() {
  double TIME_START = clock();
  vector<unsigned long long> times;
  vector<string> back;
  int n, m;
  cin >> n >> m;
  scanf("\n ");
  int i = 0;
  string s;
  while (getline(cin, s)) {
    back.push_back(s.substr(0, 19));
    unsigned long long time = convert(s);
    times.push_back(time);
    while (*times.rbegin() - times[i] >= n) i++;
    if (times.size() - i >= m) {
      cout << *back.rbegin();
      return 0;
    }
  }
  cout << -1;
  fprintf(stderr, "\n\n%.15lf\n\n",
          (double)(clock() - TIME_START) / CLOCKS_PER_SEC);
  return 0;
}
