Majestic Gourmet University

Given proposed FC and IC lab slots with teacher conflicts, seat limits, and timing rules, choose a valid set of labs using the fewest distinct starting days.

Hard9GraphBFSGreedyImplementationNo attempts yetTime limit2sMemory limit512 MB

Problem

Majestic Gourmet University awards the Majestic International Cooking Degree to a large number of students every year.

Every student takes two mandatory classes, French Cuisine (FC) and Italian Cuisine (IC). Two disjoint groups of teachers run them, the FC teachers and the IC teachers. Both classes run as weekly cooking labs and have a limited number of seats, KFCK_{FC} students per FC lab and KICK_{IC} students per IC lab. Several lab instances of each class therefore run every week, and each instance has its own weekly timeslot and its own assigned teacher. One more complication exists. Some of the teachers are in open conflict with each other because they disagree on teaching methods.

Every year the heads of the FC department and the IC department propose a number of weekly lab instances, and Bob from the Planning Office picks a subset of these propositions so that every student gets a valid timetable, which means:

  • Every student attends exactly one weekly FC lab and one weekly IC lab. A student cannot attend two labs whose timeslots overlap, and cannot attend two labs separated by less than 5 minutes, because switching classrooms takes time.
  • The seat limits hold: at most KFCK_{FC} students in an FC lab, and at most KICK_{IC} students in an IC lab.
  • If FC teacher A is in conflict with IC teacher B, then no student of a lab taught by A attends a lab taught by B.
  • Bob also wants as many free days as possible. Among all valid choices, the starting times of the picked labs must fall on the fewest possible days of the week. Those days do not have to be consecutive.

Timeslots are compared over the whole week. A lab that starts on day dd at hh hours mm minutes runs from minute 1440(d1)+60h+m1440(d-1) + 60h + m to minute 1440(d1)+60h+m+60L1440(d-1) + 60h + m + 60L, where LL is the duration of that class in hours. A student can attend two labs exactly when one of them starts at least 5 minutes after the other one ends.

Some teachers may take no lab instance at all, and one teacher may be in charge of several labs.

Help Bob and report the smallest possible number of days.

Input

Every line holds integers separated by single spaces.

The first line holds SS, the number of registered students.

The second line holds NFCN_{FC}, KFCK_{FC}, DFCD_{FC} and TFCT_{FC}: the number of FC lab propositions, the seat limit of every FC lab, the duration in hours of every FC lab, and the number of FC teachers.

Each of the next NFCN_{FC} lines describes one proposed FC lab slot with four integers dd, hh, mm and tt: the day of the week, the starting hour, the starting minute, and the FC teacher in charge.

The next line holds NICN_{IC}, KICK_{IC}, DICD_{IC} and TICT_{IC} with the same meanings for IC.

Each of the next NICN_{IC} lines describes one proposed IC lab slot in the same format.

The next line holds CC, the number of conflicts among teachers.

Each of the last CC lines holds two integers ii and jj, meaning that FC teacher ii is in conflict with IC teacher jj.

The limits are:

  • 1S110001 \le S \le 11000
  • 1KFC,KIC641 \le K_{FC}, K_{IC} \le 64
  • 1NFC,NIC,TFC,TIC10001 \le N_{FC}, N_{IC}, T_{FC}, T_{IC} \le 1000
  • 1DFC,DIC81 \le D_{FC}, D_{IC} \le 8
  • 0CTFC×TIC0 \le C \le T_{FC} \times T_{IC}
  • 1d61 \le d \le 6 for every lab slot
  • 8h208 \le h \le 20 and 0m590 \le m \le 59 for every lab slot
  • 0t<TFC0 \le t < T_{FC} for an FC slot, and 0t<TIC0 \le t < T_{IC} for an IC slot
  • 0i<TFC0 \le i < T_{FC} and 0j<TIC0 \le j < T_{IC} for every conflict

Two propositions for the same teacher never overlap.

Output

Print one integer on a single line. Print 0 if no valid choice exists. Otherwise print the smallest number of days of the week that carry the starting times of the picked lab slots.