#include <bits/stdc++.h>
using namespace std;
pair<int, pair<int, int> > getStep(int nh, int nm, int h, int m) {
  pair<int, pair<int, int> > ret;
  ret.second = make_pair(nh, nm);
  ret.first += nh / 10 != h / 10;
  ret.first += nh % 10 != h % 10;
  ret.first += nm % 10 != m % 10;
  ret.first += nm / 10 != m / 10;
  return ret;
}
int main() {
  int T, h, m;
  scanf("%d%d:%d", &T, &h, &m);
  pair<int, pair<int, int> > ans(1000, make_pair(0, 0));
  if (T == 12) {
    for (int nh = 1, ed_nh = 12; nh <= ed_nh; nh++) {
      for (int nm = 0, ed_nm = 60; nm < ed_nm; nm++) {
        ans = min(ans, getStep(nh, nm, h, m));
      }
    }
  } else {
    for (int nh = 0, ed_nh = 24; nh < ed_nh; nh++) {
      for (int nm = 0, ed_nm = 60; nm < ed_nm; nm++) {
        ans = min(ans, getStep(nh, nm, h, m));
      }
    }
  }
  printf("%02d:%02d\n", ans.second.first, ans.second.second);
  return 0;
}
