Problem:
Score : 100 points

Problem Statement

In this problem, we use the 24-hour clock.

Takahashi gets up exactly at the time H_1 : M_1 and goes to bed exactly at the time H_2 : M_2. (See Sample Inputs below for clarity.)
He has decided to study for K consecutive minutes while he is up.
What is the length of the period in which he can start studying?

Constraints

0 \le H_1, H_2 \le 23

0 \le M_1, M_2 \le 59

The time H_1 : M_1 comes before the time H_2 : M_2.

K \ge 1

Takahashi is up for at least K minutes.

All values in input are integers (without leading zeros).

Input

Input is given from Standard Input in the following format:

H_1 M_1 H_2 M_2 K

Output

Print the length of the period in which he can start studying, as an integer.

Sample Input 1

10 0 15 0 30

Sample Output 1

270

Takahashi gets up at exactly ten in the morning and goes to bed at exactly three in the afternoon.
It takes 30 minutes to do the study, so he can start it in the period between ten o'clock and half-past two. The length of this period is 270 minutes, so we should print 270.

Sample Input 2

10 0 12 0 120

Sample Output 2

0

Takahashi gets up at exactly ten in the morning and goes to bed at exactly noon. It takes 120 minutes to do the study, so he has to start it at exactly ten o'clock. Thus, we should print 0.

Solution in C# (.NET Core 3.1.201) (judge verdict: Runtime Error)

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

namespace atcoder
{
    class Solver
    {
        //Solve
        public void Solve()
        {
            cin = new Scanner();
            int n = cin.nextint;
            int[] a = cin.scanint;

            ArrayList nodes = new ArrayList();

            nodes.Add(1);
            for (int i = 0; i < n; i++)
            {
                nodes.Add(((int)nodes[i] - a[i]) * 2);
                // Console.WriteLine(nodes);
            }

            // PrintValues(nodes, n + 1);
            if ((int)nodes[n] < a[n])
            {
                Console.WriteLine(-1);
                return;
            }

            if ((int)nodes[n] > a[n])
            {
                for (int i = n; i > 0; i--)
                {
                    int d = (int)nodes[n] - a[n];
                    int hf = (int)nodes[i] / 2;
                    // Console.WriteLine("{0} , {1} , {2}", i, d, hf);
                    if (d <= hf)
                    {
                        for (int j = i; j <= n; j++)
                        {
                            nodes[j] = (int)nodes[j] - d;
                        }
                        i = 0;
                    }
                    else
                    {
                        for (int j = i; j <= n; j++)
                        {
                            nodes[j] = (int)nodes[j] - hf;
                        }
                        // PrintValues(nodes, n + 1);
                    }
                }
            }

            // PrintValues(nodes, n + 1);
            int sum = 0;
            foreach (int i in nodes)
            {
                sum += i;
            }

            Console.WriteLine(sum);
        }

        public static void PrintValues(IEnumerable myList, int myWidth)
        {
            int i = myWidth;
            foreach (Object obj in myList)
            {
                if (i <= 0)
                {
                    i = myWidth;
                    Console.WriteLine();
                }
                i--;
                Console.Write("{0,8}", obj);
            }
            Console.WriteLine();
        }

        public static int CountChar(string s, char c)
        {
            return s.Length - s.Replace(c.ToString(), "").Length;
        }

        public static int Countbit(BitArray bits)
        {
            int count = 0;
            foreach (bool bit in bits) if (bit) count++;
            return count;
        }

        //Main
        public Solver() { }
        Scanner cin;
        public static int Main()
        {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            new Solver().Solve();
            Console.Out.Flush();
            return 0;
        }

        //Util
        static bool IsOdd(long num) { return (num & 1) == 1; }
        static bool IsEven(long num) { return (num & 1) == 0; }
        static long GCD(long a, long b) { return b != 0 ? GCD(b, a % b) : a; }
    }

    class Scanner
    {
        string[] s; int i;
        readonly char[] cs = new char[] { ' ' };
        public Scanner() { s = new string[0]; i = 0; }
        public string[] scan => Console.ReadLine().Split();
        public int[] scanint => Array.ConvertAll(scan, int.Parse);
        public long[] scanlong => Array.ConvertAll(scan, long.Parse);
        public double[] scandouble => Array.ConvertAll(scan, double.Parse);
        public string next
        {
            get
            {
                if (i < s.Length) return s[i++];
                string st = Console.ReadLine();
                while (st == "") st = Console.ReadLine();
                s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
                i = 0;
                return next;
            }
        }
        public int nextint => int.Parse(next);
        public long nextlong => long.Parse(next);
        public double nextdouble => double.Parse(next);
    }

    class Node<T>
    {
        private int m_height;
        private Node<T> m_parent;
        public T Item { get; private set; }
        public Node(T item)
        {
            m_height = 0;
            m_parent = this;
            Item = item;
        }
        public Node<T> Find()
        {
            if (m_parent == this) { return this; }

            Node<T> parent = m_parent.Find();
            m_parent = parent;
            return parent;
        }
        public static void Unite(Node<T> a, Node<T> b)
        {
            Node<T> p = a.Find();
            Node<T> q = b.Find();
            if (p.m_height < q.m_height)
            {
                p.m_parent = q;
                q.m_height = Math.Max(p.m_height + 1, q.m_height);
            }
            else
            {
                q.m_parent = p;
                p.m_height = Math.Max(q.m_height + 1, p.m_height);
            }
        }
    }
}