#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  char h[5];
  for (int i = 0; i < 5; i++) cin >> h[i];
  if (n == 24) {
    bool bad = false;
    if (h[0] > '2') h[0] = '0';
    if (h[0] == '2') bad = true;
    if (h[1] > '3' && bad) h[1] = '0';
  } else {
    bool bad = false;
    if (h[0] > '1') {
      if (h[1] == '0')
        h[0] = '1';
      else
        h[0] = '0';
    }
    if (h[0] == '1') bad = true;
    if (h[1] > '2' && bad) h[1] = '1';
    if (h[0] == '0' && h[1] == '0') h[0] = '1';
  }
  if (h[3] > '5') h[3] = '0';
  for (int i = 0; i < 5; i++) cout << h[i];
  return 0;
}
