public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String line = "";
		StringBuilder out = new StringBuilder();
		while ((line = in.readLine()) != null && line.length() != 0) {
			int[] v = readInts(line);
			if (v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0)
				break;
			GregorianCalendar c = new GregorianCalendar(v[3], v[2] - 1, v[1]);
			c.add(Calendar.DAY_OF_MONTH, v[0]);
			String ans = c.get(Calendar.DAY_OF_MONTH) + " ";
			ans += (c.get(Calendar.MONTH) + 1) + " ";
			ans += c.get(Calendar.YEAR);
			out.append(ans + "\n");
		}
		System.out.print(out);
	}