#include <bits/stdc++.h>
using namespace std;
int main() {
  char str[10];
  int n, ans = 0;
  scanf("%d", &n);
  scanf("%s", str);
  int hr = (str[0] - 48) * 10 + str[1] - 48;
  int mn = (str[3] - 48) * 10 + str[4] - 48;
  if (n == 12 and (hr == 0 or hr > 12)) {
    if (str[1] == 48)
      str[0] = 49;
    else
      str[0] = 48;
  }
  if (n == 24 and (hr > 23)) {
    str[0] = 48;
  }
  if (mn > 59) str[3] = 48;
  printf("%s\n", str);
  return 0;
}
