World Trip#3293

World Trip

5 seconds
256 megabytes
Very hard

Kita_masa is planning a trip around the world. This world has countries and the country has M_i cities. Kita_masa wants to visit every city exactly once, and return back to the starting city.

In this world, people can travel only by airplane. There are two kinds of airlines: domestic and international lines. Since international airports require special facilities such as customs and passport control, only a few cities in each country have international airports.

You are given a list of all flight routes in this world and prices for each route. Now it's time to calculate the cheapest route for Kita_masa's world trip!

Input

The first line contains two integers and , which represent the number of countries and the number of airline routes respectively. The second line contains integers, where the -th integer represents the number of cities M_i in the country . The third line also contains integers, where the -th integer represents the number of international airports F_i in the country . Each of the following lines contains five integers: [Country 1] [City 1] [Country 2] [City 2] [Price]. This means that there is a bi-directional flight route between [City 1] in [Country 1] and [City 2] in [Country 2], and its price is [Price].

Note that cities in each country are numbered from 1, and only cities whose city number is smaller or equal to F_i have international airports. That is, if there is a flight route between the city n_1 in the country c_1 and the city n_2 in the country c_2, it must be n_1 \leq F_{c_1} and n_2 \leq F_{c_2}. You can assume that there's no flight route which departs from one city and flies back to the same city, and that at most one flight route exists in this world for each pair of cities.

The following constraints hold for each dataset:

  • 1 \leq N \leq 15

  • 1 \leq M_i \leq 15

  • 1 \leq F_i \leq 4

  • \text{sum}(F_i) \leq 15

  • 1 \leq \text{Price} \leq 10{,}000

Output

Print a line that contains a single integer representing the minimum price to make a world trip.

If such a trip is impossible, print -1 instead.

Examples

Example 1

Loading
Loading