Problem:
Problem D: Clock Hands

We have an analog clock whose three hands (the second hand, the minute hand and the hour hand) rotate quite smoothly. You can measure two angles between the second hand and two other hands.

Write a program to find the time at which "No two hands overlap each other" and "Two angles between the second hand and two other hands are equal" for the first time on or after a given time.

Figure D.1. Angles between the second hand and two other hands

Clocks are not limited to 12-hour clocks. The hour hand of an H-hour clock goes around once in H hours. The minute hand still goes around once every hour, and the second hand goes around once every minute. At 0:0:0 (midnight), all the hands are at the upright position.

Input

The input consists of multiple datasets. Each of the dataset has four integers H, h, m and s in one line, separated by a space. H means that the clock is an H-hour clock. h, m and s mean hour, minute and second of the specified time, respectively.

You may assume 2 ≤ H ≤ 100, 0 ≤ h < H, 0 ≤ m < 60, and 0 ≤ s < 60.

The end of the input is indicated by a line containing four zeros.

Figure D.2. Examples of H-hour clock (6-hour clock and 15-hour clock)

Output

Output the time T at which "No two hands overlap each other" and "Two angles between the second hand and two other hands are equal" for the first time on and after the specified time.

For T being ho:mo:so (so seconds past mo minutes past ho o'clock), output four non-negative integers ho, mo, n, and d in one line, separated by a space, where n/d is the irreducible fraction representing so. For integer so including 0, let d be 1.

The time should be expressed in the remainder of H hours. In other words, one second after
(H − 1):59:59 is 0:0:0, not H:0:0.

Sample Input

12 0 0 0
12 11 59 59
12 1 56 0
12 1 56 3
12 1 56 34
12 3 9 43
12 3 10 14
12 7 17 58
12 7 18 28
12 7 23 0
12 7 23 31
2 0 38 29
2 0 39 0
2 0 39 30
2 1 6 20
2 1 20 1
2 1 20 31
3 2 15 0
3 2 59 30
4 0 28 48
5 1 5 40
5 1 6 10
5 1 7 41
11 0 55 0
0 0 0 0

Output for the Sample Input

0 0 43200 1427
0 0 43200 1427
1 56 4080 1427
1 56 47280 1427
1 57 4860 1427
3 10 18600 1427
3 10 61800 1427
7 18 39240 1427
7 18 82440 1427
7 23 43140 1427
7 24 720 1427
0 38 4680 79
0 39 2340 79
0 40 0 1
1 6 3960 79
1 20 2400 79
1 21 60 79
2 15 0 1
0 0 2700 89
0 28 48 1
1 6 320 33
1 6 40 1
1 8 120 11
0 55 0 1

Solution in C++11 (judge verdict: Accepted)

#include <algorithm>
#include <cstdlib>
#include <iostream>
using namespace std;

struct fraction {
	typedef long long frac_t;
	frac_t nume, deno;

	void reduce() {
		frac_t g = __gcd(nume, deno);
		nume /= g; deno /= g;
	}

	fraction(frac_t n = 1, frac_t d = 1):nume(n), deno(d) { reduce(); }
	fraction(const fraction& f):nume(f.nume), deno(f.deno) {}

	fraction operator+(const fraction& o) const {
		frac_t rdeno = deno / __gcd(deno, o.deno) * o.deno;
		frac_t rnume = rdeno / deno * nume + rdeno / o.deno * o.nume;
		return fraction(rnume, rdeno);
	}

	fraction operator-(const fraction& o) const {
		frac_t rdeno = deno / __gcd(deno, o.deno) * o.deno;
		frac_t rnume = rdeno / deno * nume - rdeno / o.deno * o.nume;
		return fraction(rnume, rdeno);
	}

	fraction operator/(const fraction& o) const {
		frac_t g1 = __gcd(nume, o.nume);
		frac_t g2 = __gcd(o.deno, deno);
		frac_t rnume = (nume / g1) * (o.deno / g2);
		frac_t rdeno = (deno / g2) * (o.nume / g1);
		return fraction(rnume, rdeno);
	}

	fraction &operator+=(const fraction& o) {
		frac_t rdeno = deno / __gcd(deno, o.deno) * o.deno;
		nume = rdeno / deno * nume + rdeno / o.deno * o.nume;
		deno = rdeno;
		reduce();
		return *this;

	}

	fraction operator+(int d) {
		return *this + fraction(d, 1);
	}

	fraction operator/(int d) {
		return fraction(nume, deno * d);
	}

	bool operator==(const fraction& o) const {
		return nume == o.nume && deno == o.deno;
	}

	bool operator<(const fraction& o) const {
		frac_t lcm_deno = deno / __gcd(deno, o.deno) * o.deno;
		return lcm_deno / deno * nume < lcm_deno / o.deno * o.nume;
	}

	double toDouble() const {
		return (double)nume / deno;
	}
};

int H;

constexpr double EPS = 1e-10;
const fraction one(1, 1);

void normalize(int &h, int &m, fraction &fs) {
	if(fs.toDouble() >= 60) {
		fs += fraction(-60, 1);
		if(++m >= 60) {
			m = 0;
			if(++h >= H) {
				h = 0;
			}
		}
	}
}

template<class T>
T get_angle(T a, T b) {
	if(b < a) swap(a, b);
	return min(b - a, a - b + 1);
}

bool equal(double a, double b) {
	return abs(a - b) < EPS;
}

bool strict_ok(int h, int m, const fraction &fs) {
	fraction theta_s = fs / 60;
	fraction theta_m = fraction(m, 60) + theta_s / 60;
	fraction theta_h = fraction(h, H) + theta_m / H;
	return !(theta_m == theta_h)
		&& get_angle(theta_s, move(theta_m)) == get_angle(move(theta_s), move(theta_h));
}

bool ok(int h, int m, const fraction &fs) {
	const double theta_s = fs.toDouble() / 60.0;
	const double theta_m = (m + theta_s) / 60.0;
	const double theta_h = (h + theta_m) / H;
	return equal(get_angle(theta_s, theta_m), get_angle(theta_s, theta_h))
		&& !equal(theta_m, theta_h) && strict_ok(h, m, fs);
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	for(int h, m, s; cin >> H >> h >> m >> s && H;) {
		const fraction d(1, 119 * H - 1);
		for(fraction fs(s, 1);; normalize(h, m, (fs += d))) {
			if(ok(h, m, fs)) {
				cout << h << " " << m << " " << fs.nume << " " << fs.deno << endl;
				break;
			}
		}
	}

	return EXIT_SUCCESS;
}