#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

ll gcd(ll a, ll b)
{
	assert(a > 0 && b > 0);
	if (a < b) swap(a, b);
	return (a%b ? gcd(a%b, b) : b);
}

ll lcm(ll a, ll b)
{
	return a / gcd(a, b)*b;
}

struct Fraction
{
	ll s, d; // s/d
	Fraction() :s(0), d(1) {};
	Fraction(ll n) :s(n), d(1) {};
	Fraction(ll s_, ll d_) :s(s_), d(d_) { reduct(); };
	void reduct()
	{
		if (s == 0)
		{
			d = 1;
			return;
		}
		int sig = 1;
		if (s < 0)
		{
			sig *= -1;
		}
		if (d < 0)
		{
			sig *= -1;
			d *= -1;
			s *= -1;
		}
		ll g;
		if (sig == -1) g = gcd(-s, d);
		else g = gcd(s, d);
		s /= g; d /= g;
	}
	Fraction operator+(const Fraction& rhs)
	{
		ld nd = lcm(d, rhs.d);
		ld ns = nd / d * s + nd / rhs.d * rhs.s;
		Fraction res(ns, nd);
		res.reduct();
		return res;
	}
	Fraction operator-(const Fraction& rhs)
	{
		ld nd = lcm(d, rhs.d);
		ld ns = nd / d * s - nd / rhs.d * rhs.s;
		Fraction res(ns, nd);
		res.reduct();
		return res;
	}
	Fraction operator*(const Fraction& rhs)
	{
		ld nd = d * rhs.d;
		ld ns = s * rhs.s;
		Fraction res(ns, nd);
		res.reduct();
		return res;
	}
	Fraction operator/(const Fraction& rhs)
	{
		ld nd = d * rhs.s;
		ld ns = s * rhs.d;
		Fraction res(ns, nd);
		res.reduct();
		return res;
	}
	bool operator==(const Fraction& rhs)
	{
		return (d == rhs.d) && (s == rhs.s);
	}
	bool operator<(const Fraction& rhs)
	{
		return s * rhs.d < rhs.s * d;
	}
};

int H;

typedef tuple<int, int, Fraction> Time;
Fraction vs, vm, vh;

tuple<Fraction, Fraction, Fraction> angle(Time time)
{
	int h, m;
	Fraction s;
	tie(h, m, s) = time;
	Fraction ang_s(s * vs);
	Fraction ang_m((Fraction(m*60) + s) * vm);
	Fraction ang_h((Fraction(h * 3600 + m*60) + s) * vh);
	return make_tuple(ang_s, ang_m, ang_h);
}

Time opt(Time now, Fraction add)
{
	int h, m;
	Fraction s;
	tie(h, m, s) = now;
	s = s + add;
	if (s.s >= 60 * s.d)
	{
		s = s - Fraction(60);
		m++;
	}
	if (m == 60)
	{
		m = 0;
		h++;
	}
	if (h == H)
	{
		h = 0;
	}
	return Time(h, m, s);
}


int main()
{
	cin.sync_with_stdio(false); cout << fixed << setprecision(10);
	while (cin >> H, H)
	{
		int h, m, s_;
		cin >> h >> m >> s_;
		Fraction s(s_, 1);
		vs = Fraction(360, 60);
		vm = Fraction(360 , 3600);
		vh = Fraction(360, 3600 * H);
		Fraction ang_s, ang_m, ang_h;
		Time start(h, m, s);
		tie(ang_s, ang_m, ang_h) = angle(start);
		Fraction theta[2];
		theta[0] = (ang_h + ang_m) / Fraction(2);
		theta[1] = theta[0] < Fraction(180)  ? theta[0] + Fraction(180) : theta[0] - Fraction(180);
		vector<Fraction> add;
		REP(i, 2)
		{
			if (theta[i] == ang_s)
			{
				add.push_back(Fraction(0));
				continue;
			}
			if (theta[i] < ang_s) theta[i] = theta[i] + Fraction(360);
			Fraction t = (Fraction(2) * (theta[i] - ang_s)) / (Fraction(2) * vs - vm - vh);
			add.push_back(t);
		}
		sort(ALL(add));
		for (auto t : add)
		{
			Time time = opt(start, t);
			Fraction as, am, ah;
			tie(as, am, ah) = angle(time);
			if (as == am || am == ah || ah == as) continue;
			cout << get<0>(time) << " " << get<1>(time) << " " << get<2>(time).s << " " << get<2>(time).d << endl;
			break;
		}
	}
	return 0;
}

Measured memory_bytes: 3144