import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import java.util.StringTokenizer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.ToIntBiFunction;

public class Mark {

    public static void main(String[] args) throws IOException {
        FastReader fr = new FastReader();
        PrintWriter pw = new PrintWriter(System.out);

        int N = fr.nextInt(), M = fr.nextInt();
        List<Integer> a = new ArrayList<>();
        List<Integer> b = new ArrayList<>();

        for (int i = 0; i < N; ++i) a.add(to(fr.next()));
        for (int i = 0; i < M; ++i) b.add(to(fr.next()));
        int S = fr.nextInt();

        int ans = Integer.MAX_VALUE;
        for (int x : a)
            for (int y : b)
                if (y - x >= S)
                    ans = Math.min(ans, y-x);

        pw.println(ans == Integer.MAX_VALUE ? -1 : ans);

        pw.close();
    }

    static int to(String s)
    {
        String[] t = s.split(":");
        return 60*60*Integer.parseInt(t[0]) + 60*Integer.parseInt(t[1]) + Integer.parseInt(t[2]);
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            st = null;
            return str;
        }
    }

}