#include <bits/stdc++.h>
using namespace std;
int main() {
  int format;
  cin >> format;
  int hour, minute;
  char isto;
  cin >> hour >> isto >> minute;
  if (format == 12) {
    if (hour > 12 && hour % 10 != 0)
      hour = hour % 10;
    else if (hour > 12 && hour % 10 == 0)
      hour = 10;
    else if (hour == 0)
      hour = 1;
  } else {
    if (hour > 23) hour = hour % 10;
  }
  if (minute > 59) minute = minute % 60;
  if (hour < 10) {
    cout << "0" << hour << isto;
  } else {
    cout << hour << isto;
  }
  if (minute < 10) {
    cout << "0" << minute;
  } else
    cout << minute;
  return 0;
}
