#include <iostream>
#include <cassert>
#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
using namespace std;
int H,h,m,s;
void judge();
void output(int n,int d);
int main() {
  while(true){
    cin >> H >> h >> m >> s;
    if(H==0&&h==0&&m==0&&s==0) return 0;
    judge();
  }
  return 0;
}

void judge(){
  int dn,dd;
  double ds,nh,nm,ns;
  while(true){
    dn = s*(119*H-1)-60*m*(H+1)-3600*h+3600*H;
    dd = 1-119*H;
    rep(i,3){
      ds = (double)dn/(double)dd;
      nh = (double)h/(double)H+(double)m/(60*(double)H)+((double)s+ds)/(3600*(double)H);
      nm = (double)m/60+((double)s+ds)/3600;
      ns = ((double)s+ds)/60;
      if(ns>=1) break;
      if(ds>=0&&ns<1&&nh!=nm&&nh!=ns&&nm!=ns){
        output(dn+dd*s,dd);
        return;
      }
      dn -= 3600*H;
    }
    s=0;
    m++;
    if(m==60){
      m=0;
      h++;
      if(h==H) h=0;
    }
  }
}

void output(int n,int d){
  if(n==0){
    cout << h << " " << m << " " << 0 << " " << 1 << endl;
    return;
  }
  int a,b;
  a=n;
  b=d;
  while(true){
    a = a%b;
    if(a==0){
      n = n/b;
      d = d/b;
      break;
    }
    b = b%a;
    if(b==0){
      n = n/a;
      d = d/a;
      break;
    }
  }
  cout << h << " " << m << " " << n << " " << d << endl;
  return;
}