#include <bits/stdc++.h>
using namespace std;
int main() {
  int f, g;
  string s;
  cin >> f >> s;
  int hours = (s[0] - '0') * 10 + (s[1] - '0'),
      minutes = (s[3] - '0') * 10 + (s[4] - '0');
  if (minutes > 59) minutes %= 10;
  if (f == 12) {
    if (hours == 0)
      hours = 1;
    else if (hours > 12) {
      if (hours % 10 != 0)
        hours %= 10;
      else
        hours = 10;
    }
  } else {
    if (hours > 23) hours %= 10;
  }
  if (hours < 10)
    cout << 0 << hours << ':';
  else
    cout << hours << ':';
  if (minutes < 10)
    cout << 0 << minutes;
  else
    cout << minutes;
  return 0;
}
