using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    struct Customer
    {
        internal int num, wait, arrival, stay;
    }

    class Problem
    {
        readonly int n = 100;

        Customer[] cs;
        Queue<int> waiting;
        int[] seats;
        int time, minStay;

        public Problem()
        {
            cs = new Customer[n];
            waiting = new Queue<int>();
            seats = Enumerable.Repeat(-1, 17).ToArray();
            time = 0;
            minStay = int.MaxValue;
        }

        public int SearchWaitTime(int id)
        {
            return cs[id].wait;
        }

        public void MakeMap()
        {
            for (int i = 0; i < n; i++)
            {
                Customer c;
                c.num = (i % 5 == 1) ? 5 : 2;
                c.wait = 0;
                c.arrival = time;
                c.stay = 17 * (i % 2) + 3 * (i % 3) + 19;
                cs[i] = c;

                if (waiting.Count > 0)
                {
                    waiting.Enqueue(i);
                }
                else
                {
                    int idx = CheckSeat(i);

                    if (idx != -1) SetSeat(i, idx);
                    else waiting.Enqueue(i);
                }

                for (int j = 0; j < 5; j++) CheckLeave();
            }

            while (waiting.Count > 0) CheckLeave();
        }

        private void CheckLeave()
        {
            if (time == minStay)
            {
                int tempMin = int.MaxValue;

                for (int k = 0; k < seats.Length; k++)
                {
                    if (seats[k] == -1) continue;

                    if (seats[k] == minStay) seats[k] = -1;
                    else tempMin = Math.Min(tempMin, seats[k]);
                }
                minStay = tempMin;

                CheckWait();
            }
            time++;
        }

        private void CheckWait()
        {
            while (waiting.Count > 0)
            {
                int id = waiting.Peek();
                int idx = CheckSeat(id);

                if (idx != -1)
                {
                    SetSeat(id, idx);
                    cs[id].wait = time - cs[id].arrival;
                    waiting.Dequeue();
                }
                else return;
            }
        }

        private void SetSeat(int id, int idx)
        {
            int temp = cs[id].stay + time;

            for (int i = 0; i < cs[id].num; i++)
            {
                seats[idx + i] = temp;
            }
            minStay = Math.Min(minStay, temp);
        }

        private int CheckSeat(int id)
        {
            int num = cs[id].num;
            int len = seats.Length - num;

            for (int i = 0; i <= len; i++)
            {
                bool isOpen = true;

                for (int j = 0; j < num; j++)
                {
                    if (seats[i + j] != -1) isOpen = false;
                }
                if (isOpen) return i;
            }
            return -1;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder();

            Problem p = new Problem();
            p.MakeMap();

            string input;

            while (!string.IsNullOrEmpty(input = Console.ReadLine()))
            {
                int n = int.Parse(input);
                sb.AppendLine(p.SearchWaitTime(n).ToString());
            }
            Console.Write(sb);
        }
    }
}

Measured memory_bytes: 10452