#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

ll gcd(ll a, ll b){
	if(b == 0) return a;
	return gcd(b, a % b);
}

ll H, h, m, s;

int main(){
	while(cin >> H >> h >> m >> s, H || h || m || s){
		bool flg = true;
		while(flg){
			ll begin = h * 60 * 60 + m * 60 + s;
			ll end = h * 60 * 60 + (m + 1) * 60;
			
			ll nn = H * (120 * (h * 60 * 60 + m * 60) - h * 60 * 60);
			ll dd = 119 * H - 1;
			//int nn = -120 * H * (h * 60 * 60 + m * 60) + H * (h * 60 * 60);
			//int dd = 1 - 119 * H;
			bool ff = false;
			ll aann, aadd;
			for(int i = -1; i <= 1; i++){
				ll nnn = nn - i * H * 60 * 60;
				if(( H * 60  - 1) * nnn == H * 60 * (h * 60 * 60 + m * 60) * dd) continue;
				if((H - 1) * nnn == H * (h * 60 * 60) * dd) continue;
				if(begin * dd <= nnn && nnn < end * dd){
					if(!ff || (double)aann / aadd > (double) nnn / dd){
						ff = true;
						aann = nnn;
						aadd = dd;
					}
				}
			}
			if(ff){
				ll am = aann / aadd / 60;
				ll ah = am / 60;
				aann -= am * aadd * 60;
				ll g = gcd(aann, aadd);
				cout << ah << " " << am % 60 << " " << (aann / g)  << " " << aadd / g << endl;
				break;
			}

			s = 0;
			m += 1;
			if(m == 60){
				h = (h + 1) % H;
				m = 0;
			}
		}
	}
}