using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;


public class MissedCall
{
	public int waitingTime(int N, int songDuration, int delay)
	{
		int t = songDuration * N + 5 * (N - 1);
		int r = 0;
		for (int i = 0; r <= t + delay; i++)
		{
			r = i * delay;
			if (r >= t)
				return r;

			for (int j = 1; j < N; j++)
			{
				int d = j * songDuration + 5 * (j-1);
				if (!(r + 1 <= d || r >= d + 5))
					return Math.Max(r, d);
			}
		}

		return -1;
	}

	// BEGIN KAWIGIEDIT TESTING
	// Generated by KawigiEdit 2.1.4 (beta) modified by pivanof
	#region Testing code generated by KawigiEdit
	[STAThread]
	private static Boolean KawigiEdit_RunTest(int testNum, int p0, int p1, int p2, Boolean hasAnswer, int p3) {
		Console.Write("Test " + testNum + ": [" + p0 + "," + p1 + "," + p2);
		Console.WriteLine("]");
		MissedCall obj;
		int answer;
		obj = new MissedCall();
		DateTime startTime = DateTime.Now;
		answer = obj.waitingTime(p0, p1, p2);
		DateTime endTime = DateTime.Now;
		Boolean res;
		res = true;
		Console.WriteLine("Time: " + (endTime - startTime).TotalSeconds + " seconds");
		if (hasAnswer) {
			Console.WriteLine("Desired answer:");
			Console.WriteLine("\t" + p3);
		}
		Console.WriteLine("Your answer:");
		Console.WriteLine("\t" + answer);
		if (hasAnswer) {
			res = answer == p3;
		}
		if (!res) {
			Console.WriteLine("DOESN'T MATCH!!!!");
		} else if ((endTime - startTime).TotalSeconds >= 2) {
			Console.WriteLine("FAIL the timeout");
			res = false;
		} else if (hasAnswer) {
			Console.WriteLine("Match :-)");
		} else {
			Console.WriteLine("OK, but is it right?");
		}
		Console.WriteLine("");
		return res;
	}
	public static void m1(string[] args) {
		Boolean all_right;
		all_right = true;
		
		int p0;
		int p1;
		int p2;
		int p3;
		
		// ----- test 0 -----
		p0 = 2;
		p1 = 5;
		p2 = 7;
		p3 = 7;
		all_right = KawigiEdit_RunTest(0, p0, p1, p2, true, p3) && all_right;
		// ------------------
		
		// ----- test 1 -----
		p0 = 4;
		p1 = 5;
		p2 = 20;
		p3 = 40;
		all_right = KawigiEdit_RunTest(1, p0, p1, p2, true, p3) && all_right;
		// ------------------
		
		// ----- test 2 -----
		p0 = 6;
		p1 = 9;
		p2 = 20;
		p3 = 40;
		all_right = KawigiEdit_RunTest(2, p0, p1, p2, true, p3) && all_right;
		// ------------------
		
		// ----- test 3 -----
		p0 = 1;
		p1 = 10;
		p2 = 1;
		all_right = KawigiEdit_RunTest(3, p0, p1, p2, false, p3) && all_right;
		// ------------------
		
		if (all_right) {
			Console.WriteLine("You're a stud (at least on the example cases)!");
		} else {
			Console.WriteLine("Some of the test cases had errors.");
		}
	}
	#endregion
	// END KAWIGIEDIT TESTING
}
//Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
