#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7fffffff;
const int maxn = 1e5 + 5;
int n, m, h, hh, mm;
int main() {
  scanf("%d", &n);
  scanf("%d:%d", &h, &m);
  hh = h / 10;
  h = h % 10;
  mm = m / 10;
  m = m % 10;
  switch (n) {
    case 12:
      if (hh > 1) {
        if (h == 0)
          hh = 1;
        else
          hh = 0;
      } else {
        if (hh == 1) {
          if (h > 2) h = 0;
        } else if (h == 0)
          h = 1;
      }
    case 24:
      if (hh * 10 + h > 23) hh = 0;
      break;
  }
  if (mm > 5) mm = 0;
  printf("%d%d:%d%d\n", hh, h, mm, m);
  return 0;
}
