#include <bits/stdc++.h>
using namespace std;
int main() {
  int mode;
  cin >> mode;
  string time;
  cin >> time;
  if (time[3] - '0' > 5) time[3] = '5';
  int hour = (time[0] - '0') * 10 + (time[1] - '0');
  if (hour > mode || (hour == mode && mode == 24)) {
    if (mode == 12) {
      if (time[1] != '0')
        time[0] = '0';
      else
        time[0] = '1';
    } else
      time[0] = '0';
  } else if (hour == 0 && mode == 12) {
    time[0] = '1';
  }
  cout << time;
  int de;
  cin >> de;
}
