Frog

Find the minimum total squared jump cost for a frog to travel from the lower bank to the upper bank across axis-aligned logs.

Medium7Shortest pathGeometryGraphHeapNo attempts yetTime limit1sMemory limit512 MB

Problem

In a hot, dry summer a hungry frog travels toward the land of flies, where water and food are plentiful. On the way it has to cross a river. The frog normally swims, but right now it is so hungry and tired that it can only walk and jump. Logs float on the river, and the frog can walk along a log or jump from one log to another.

Walking costs almost no energy, so the distance walked does not count. A jump of distance xx costs x2x^2 energy. The frog can jump a distance of at most l\sqrt{l} at a time.

The river and both banks lie on an n×mn \times m grid. (a,b)(c,d)(a, b) - (c, d) is the segment whose endpoints are (a,b)(a, b) and (c,d)(c, d). Each bank is a polyline through its vertices in the given order, and each log is a single segment. Every segment is parallel to the xx axis or to the yy axis. No two logs share a common part, and no log shares a common part with a bank.

The frog moves to any position on the same segment without spending energy. A bank is one connected polyline, so the frog also moves freely along a whole bank. The frog starts on the lower bank, and the trip ends the moment it reaches the upper bank.

Figure 1 shows an example on an 8×98 \times 9 grid. The lower bank consists of 7 segments, the upper bank consists of 11 segments, and the logs are (0,3)(2,3)(0,3)-(2,3), (4,2)(6,2)(4,2)-(6,2), (3,5)(6,5)(3,5)-(6,5), (7,4)(7,6)(7,4)-(7,6) in that order.

Figure 1. Four logs on the river.

With l=5l = 5, stepping on log 1 and then on log 3 costs 12+(5)2+12=71^2 + (\sqrt{5})^2 + 1^2 = 7 energy, and no route costs less. With l=4l = 4 the frog cannot cross the river.

Write a program that computes the minimum energy the frog needs to cross the river.

Input

The first line has two integers nn and mm, the size of the grid (3n,m50003 \le n, m \le 5\,000).

The second line has four integers uu, vv, ww, ll (2u,v,w2max(n,m)2 \le u, v, w \le 2\max(n, m), 1lmin((n1)2,(m1)2)1 \le l \le \min((n-1)^2, (m-1)^2)). The lower bank has uu vertices, the upper bank has vv vertices, and ww logs float on the river. The frog jumps a distance of at most l\sqrt{l} at a time.

Each of the next uu lines has two integers xx and yy (0x<n0 \le x < n, 0y<m0 \le y < m), one vertex (x,y)(x, y) of the lower bank. The vertices come in clockwise order, and the bottommost vertex, leftmost among those, comes first.

Each of the next vv lines has two integers xx and yy in the same format, one vertex of the upper bank. The vertices come in counterclockwise order, and the topmost vertex, leftmost among those, comes first.

On a bank, the segment joining two neighboring vertices is parallel to the xx axis or to the yy axis.

Each of the last ww lines has four integers x1x_1, y1y_1, x2x_2, y2y_2 (0x1,x2<n0 \le x_1, x_2 < n, 0y1,y2<m0 \le y_1, y_2 < m) describing one log, the segment (x1,y1)(x2,y2)(x_1, y_1) - (x_2, y_2). Either x1=x2x_1 = x_2 or y1=y2y_1 = y_2 holds.

The lower bank and the upper bank do not meet.

Output

Print on one line the minimum energy the frog needs to cross the river. If the frog cannot cross the river, print -1.